Linear Regression:
线性回归课程中的例子是房价预测属于监督学习
预测函数
表示通过x获取对房价的期望。
![](https://i-blog.csdnimg.cn/blog_migrate/37c36e2e175f7ff0ee7807ab7e4b2e29.png)
Cost Function:
计算期望值与实际值的方差。
使J尽可能取得最小值。
Gradient Descent:
梯度下降法通过该点出各方向的偏导获取下一步的方向,alpha决定下一步的大小。
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
theta = theta - alpha/m*X'*(X*theta - y);%
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
注意:选取适合的alpha
theta 同时更新
新公式:
(对J求偏导后展开)
Feature Scaling
对输入数据进行标准化。提高梯度下降的效率。
Si可以为标准差或者最大值与最小值的差
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu(1,1)=mean(X(:,1));
mu(1,2)=mean(X(:,1));
sigma(1,1)=std(X(:,1));
sigma(1,2)=std(X(:,2));
X(:,1)=(X(:,1)-mu(1,1))/sigma(1,1)
X(:,2)=(X(:,2)-mu(1,2))/sigma(2,2)
end
Normal Equation:
正规方程
令J的导数等于0
XTX · θ = XTY
(XTX)-1(XTX) · θ = (XTX)-1XT
θ = (XTX)-1XTY
hθ(x)=θ0+θ1
∗x
hθ(x)=θ0+θ1∗x
hθ
(
x
)