题目为:使用有多个变量的线性回归来预测房屋的价格。ex1data2.txt 房屋价格的训练集. 第一列为房屋面积,第二列为房屋中的房间数量,第三列为实际的价格。
预测X_p=[1650 3];的价格
方法一:采用梯度下降法
1. 首先由于x1和x2相差太多,所以需要使用feature scalling. 这里采用mean derivation. 或者是max-min
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;
disp(size(X_norm));
m = size(X,1);
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu = mean(X);
sigma = std(X);
disp('mu'),disp(mu);
disp('sigma'),disp(sigma);
for i=1:m;
X_norm(i,:) = (X(i,:)-mu )./ sigma;
end;
%disp('X_norm'),disp(X_norm);
2. 为X在最前面加上一列,构成最后需要使用的X,然后初始化alpha = 0.01; num_iters = 400;theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient d