多变量线性回归
均值归一化
代价函数
梯度下降
练习1
数据集
x1:the size of the house (in square feet)
x2 : the number of bedrooms
y :he price of the house
特征缩放(归一化)
在面对多维特征问题的时候,我们要保证这些特征都具有相近的尺度,这将帮助梯度下降算法更快地收敛。
方法:将所有特征的尺度都尽量缩放到-1到1之间
均值归一化
为训练集均值
为训练集的方差或极差
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));%average
sigma = zeros(1, size(X, 2));%standard deviation
mu = mean(X_norm);
sigma = std(X_norm);
for i = 1:m
X_norm(i,:) = (X_norm(i,:) - mu)./sigma;
end
mean(A):返回值为该矩阵的各列向量的均值
mean(A,2):返回值为该矩阵的各行向量的均值
std (x, flag,dim):
flag表示标准偏差是除以n还是除以n-1
除以n-1;
除以n
dim表示维数
按照列分
是按照行分
若是三维的矩阵,dim==3就按照第三维来分数据
代价函数
%代价函数
%computeCost.m
function J = computeCost(X, y, theta)
m = length(y);
J = 0;
h = X*theta;
J = sum(h-y).^2/(2*m);
end
梯度下降
%梯度下降
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
h = X*theta;
theta(1) = theta(1) -(alpha/m)*sum(h-y);
theta(2) = theta(2) -(alpha/m)*sum((h-y).*X(:,2));
theta(3) = theta(3) -(alpha/m)*sum((h-y).*X(:,3));
J_history(iter) = computeCostMulti(X, y, theta);
end
end
正规方程
function [theta] = normalEqn(X, y)
theta = zeros(size(X, 2), 1);
theta = pinv(X'*X)*X'*y;
end