多元线性回归其实方法和单变量线性回归差不多,我们这里直接给出算法:
computeCostMulti函数
function J = computeCostMulti(X, y, theta) m = length(y); % number of training examples J = 0; predictions = X * theta; J = 1/(2*m)*(predictions - y)' * (predictions - y);end
gradientDescentMulti函数
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters) m = length(y); % number of training examples J_history = zeros(num_iters, 1); feature_number = size(X,2); t