网易云课堂 Machine Learning 编程作业 1:liner regression
一.单变量线性回归
1. warmUpExercise.m:Output the 5 by 5 identity matrix
A = eye(5);
2.plotData.m:plot the data
figure;
plot(x, y, 'rx', 'MarkerSize', 10);
ylabel('Profit in $10,000s');
xlabel('Population of City in 10,000s');
3.computeCost.m
J ( θ ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 J(\theta)=\frac{1}{2 m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right)^{2} J(θ)=2m1i=1∑m(hθ(x(i))−y(i))2
J = sum((X * theta - y).^2) / (2*m);
4 .gradientDescent.m:梯度下降法,对参数进行迭代
θ j : = θ j − α 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) , ( j = 0 , 1 ) \theta_{j} :=\theta_{j}-\alpha \frac{1}{m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right) x_{j}^{(i)},(j=0,1) θj:=θj−αm1i=1∑m(hθ(x(i))−y(i))xj(i),(j=0,1)
theta(1) = theta(1) - alpha / m * sum(X * theta_s - y);
theta(2) = theta(2) - alpha / m * sum((X * theta_s - y) .* X(:,2));
theta_s=theta;
二 .多变量线性回归
1. featureNormalize.m:数据归一化
z = x − μ σ z=\frac{x-\mu}{\sigma} z=σx−μ
mu = mean(X); % mean value
sigma = std(X); % standard deviation
X_norm = (X - repmat(mu,size(X,1),1)) ./ repmat(sigma,size(X,1),1);
2.computeCostMulti.m:计算代价
J = sum((X * theta - y).^2) / (2*m);
3.gradientDescentMulti.m:计算梯度
∂ ∂ θ j J ( θ ) = 1 2 m [ 2 ( h θ ( x ( 1 ) ) − y ( 1 ) ) x j ( 1 ) + 2 ( h θ ( x ( 2 ) ) − y ( 2 ) ) x j ( 2 ) + … ] = 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) \begin{aligned} \frac{\partial}{\partial \theta_{j}} J(\theta) &=\frac{1}{2 m}\left[2\left(h_{\theta}\left(x^{(1)}\right)-y^{(1)}\right) x_{j}^{(1)}+2\left(h_{\theta}\left(x^{(2)}\right)-y^{(2)}\right) x_{j}^{(2)}+\ldots\right] \\ &=\frac{1}{m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right) x_{j}^{(i)} \end{aligned} ∂θj∂J(θ)=2m1[2(hθ(x(1))−y(1))xj(1)+2(hθ(x(2))−y(2))xj(2)+…]=m1i=1∑m(hθ(x(i))−y(i))xj(i)
θ j : = θ j − α 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) , ( j = 0 , 1 ) \theta_{j} :=\theta_{j}-\alpha \frac{1}{m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right) x_{j}^{(i)},(j=0,1) θj:=θj−αm1i=1∑m(hθ(x(i))−y(i))xj(i),(j=0,1)
不适用循环,直接使用矩阵乘法
θ = θ − α 1 m X T ( X θ − Y ) \theta=\theta-\alpha \frac{1}{m} X^{T}(X \theta-Y) θ=θ−αm1XT(Xθ−Y)
theta = theta - alpha / m * X' * (X * theta - y);
4.根据迭代n次后得到的θ \thetaθ,计算1650平方英尺、3个卧室的房子价钱
price = [1 (([1650 3]-mu) ./ sigma)] * theta ;
三.使用正规方程进行求解
θ = ( X T X ) − 1 X T Y \theta=\left(X^{T} X\right)^{-1} X^{T} Y θ=(XTX)−1XTY
theta = pinv( X' * X ) * X' * y;
第一次使用markdown编辑,还有很多需要改进和学习的地方