我目前正在使用Andrew NG在Coursera上的机器学习课程的第2周,我遇到了一个我无法解决的问题。
基于数据集,第一列是房子大小,第二列是房子的数量,第三列是它的价格,我需要在规范化数据后使用线性回归和梯度下降预测新房价格。
但是,我的预测得到了一个巨大的数字,我无法找到计算错误的位置。
我使用以下内容:
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
for i = 1:size(X, 2);
mu(1, i) = mean(X(:, i)), % Getting the mean of each row.
sigma(1, i) = std(X(:, i)), % Getting the standard deviation of each row.
for j = 1:size(X, 1);
X_norm(j, i) = (X(j, i) .- mu(1, i)) ./ sigma(1, i);
end;
end;
规范化功能的代码(X是数据集矩阵):
m = length(y);
J = 0;
predictions = X * theta;
sqErrors = (predictions - y).^2;
J = (1/(2*m)) * sum(sqErrors);
计算当前费用的代码:
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% Getting the predictions for our firstly chosen theta values.
predictions = X * theta;
% Getting the error difference of the hypothesis(h(x)) and real results(y).
diff = predictions - y;
% Getting the number of features.
features_num = size(X, 2);
% Applying gradient descent for each feature.
for i = 1:features_num;
theta(i, 1) = theta(i, 1) - (alpha / m) * sum(diff .* X(:, i))
end;
% Saving the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
计算梯度下降的代码:
{{1}}
在预测1650平方英尺和3间卧室的房子时得到的价格: 182329818.366117