Machine learning吴恩达第二周coding作业(选做)
原创
©著作权归作者所有:来自51CTO博客作者fish04的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.Feature Normalization:
归一化的处理
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.
%
for i=1:size(X,2),
mu(i)=mean(X(:,i));
sigma(i)=std(X(:,i));
X_norm(:,i)=(X_norm(:,i)-mu(i))/sigma(i);
end;
% ============================================================
end
2. Computing Cost (for Multiple Variables) && Gradient Descent (for Multiple Variables)
由于我们单变量的时候就是用矩阵形式处理的,所以代码与单变量相同;
3.Normal Equations
正规方程就比较简单了;
function [theta] = normalEqn(X, y)
%NORMALEQN Computes the closed-form solution to linear regression
% NORMALEQN(X,y) computes the closed-form solution to linear
% regression using the normal equations.
theta = zeros(size(X, 2), 1);
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the code to compute the closed form solution
% to linear regression and put the result in theta.
%
% ---------------------- Sample Solution ----------------------
theta=inv(X'*X)*X'*y;
% -------------------------------------------------------------
% ============================================================
end
EPFL - Fighting