单变量和多变量从矩阵运算角度讲是一样的,直接看多变量线性回归练习题。
1. featureNormalize.m (原值减去平均值再除以标准差)
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu = mean(X, 1);
sigma = std(X, 1);
n = size(X, 2);
for j=1:n,
X_norm(:, j) = (X(:, j) - mu(j)) / sigma(j);
endfor
end
2. computeCostMulti.m (两种计算方法)
function J = computeCostMulti(X, y, theta)
m = length(y); % number of training examples
J = 0;
% X size: m * (n+1), theta size: (n+1) * 1, => m * 1;
% Y size: m * 1
J = 1 / (2 * m) * (X * theta - y)' * (X * theta - y)
% p = X * theta;
% J = 1 / (2 * m) * sum((p - y) .^ 2)
end
3. gradientDescentMulti.m (梯度下降,单变量多变量都一样)
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% p size: m * 1, X size: m * (n+1), theta size: (n+1) * 1
p = X * theta;
% theta size: (n+1) * 1, (p-y) size: m * 1, X size: m * (n+1)
theta = theta - alpha / m * ((p - y)' * X)';
% Save the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
end
end
4. normalEqn.m (正规方程计算,不需迭代梯度下降)
function [theta] = normalEqn(X, y)
theta = zeros(size(X, 2), 1);
theta = pinv(X' * X) * X' * y;
end
5. ex1_multi.m
alpha = 0.1;
num_iters = 400;
% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
% Recall that the first column of X is all-ones. Thus, it does
% not need to be normalized.
input_x = [1650 3];
norm_x = (input_x - mu) ./ sigma;
norm_x = [1, norm_x];
price = norm_x * theta; % You should change this
% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
input_x = [1, 1650, 3];
price = input_x * theta; % You should change this
梯度下降和正规方程最后预测1650 se-ft 和3 br house 结果y是差不多的:
Theta computed from gradient descent:
340412.659574
109447.795586
-6578.353971
Predicted price of a 1650 sq-ft, 3 br house (using gradient descent):
$293081.464529
Program paused. Press enter to continue.
Solving with normal equations...
Theta computed from the normal equations:
89597.909542
139.210674
-8738.019112
Predicted price of a 1650 sq-ft, 3 br house (using normal equations):
$293081.464335