I was doing Coursera—machine learning(Andrew Ng)第二周编程作业
reading this
线性回归–Octave实现
Coursera机器学习-Week 2-编程作业:Linear Regression
Coursera—machine learning(Andrew Ng)第二周编程作业
I don’t really get this
sigma = std(X, 1, 1);
despite it says …still don’t get it
well my friends say it’s 样本方差是n-1
统计学
>> help std
'std' is a function from the file E:\Octave\Octave-4.4.1\share\octave\4.4.1\m\statistics\std.m
-- std (X)
-- std (X, OPT)
-- std (X, OPT, DIM)
Compute the standard deviation of the elements of the vector X.
The standard deviation is defined as
std (X) = sqrt ( 1/(N-1) SUM_i (X(i) - mean(X))^2 )
where N is the number of elements of the X vector.
If X is a matrix, compute the standard deviation for each column
and return them in a row vector.
The argument OPT determines the type of normalization to use.
Valid values are
0:
normalize with N-1, provides the square root of the best
unbiased estimator of the variance [default]
1:
normalize with N, this provides the square root of the second
moment around the mean
If the optional argument DIM is given, operate along this
dimension.
See also: var, bounds, mad, range, iqr, mean, median.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
But it seems the code can be simple like this..
I don’t know whether I am right here though..
function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X
% FEATURENORMALIZE(X) returns a normalized version of X where
% the mean value of each feature is 0 and the standard deviation
% is 1. This is often a good preprocessing step to do when
% working with learning algorithms.
% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
% of the feature and subtract it from the dataset,
% storing the mean value in mu. Next, compute the
% standard deviation of each feature and divide
% each feature by it's standard deviation, storing
% the standard deviation in sigma.
%
% Note that X is a matrix where each column is a
% feature and each row is an example. You need
% to perform the normalization separately for
% each feature.
%
% Hint: You might find the 'mean' and 'std' functions useful.
%
mu = mean(X);
sigma = std(X);
disp('mu'),disp(mu);
disp('sigma'),disp(sigma);
sigma= std(X);
X_norm = (X-mu) ./ sigma;
disp('X_norm'),disp(X_norm);
% ============================================================
end
There are two ways to write a cost function
1.
J = 1/(2*m)*((X*theta)-y)'*((X*theta)-y);
2.
h= zeros(m,1);
h = X*theta;
J = (1/(2*m))*sum((h-y).^2);