機器學習實戰:單變數線性迴歸的實現

來源:互聯網
上載者:User

一、演算法實現
由前面的理論,我們知道了用梯度下降解決線性迴歸的公式:梯度下降解決線性迴歸思路: 演算法實現:ComputeCost函數:
function J = computeCost(X, y, theta)m = length(y); % number of training examplesJ = 0;predictions = X * theta;J = 1/(2*m)*(predictions - y)'*(predictions - y);end

gradientDescent函數:

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)% X is m*(n+1) matrix % y is m*1% theta is (n+1)*1 matrix% alpha is a number % num_iters is number of iteratorsm = length(y); % number of training examplesJ_history = zeros(num_iters, 1);  %cost function的值的變化過程%預先定義了迭代的次數for iter = 1:num_iterstemp1 = theta(1) - (alpha / m) * sum((X * theta - y).* X(:,1));temp2 = theta(2) - (alpha / m) * sum((X * theta - y).* X(:,2));theta(1) = temp1;theta(2) = temp2;J_history(iter) = computeCost(X, y, theta);endend
二、資料視覺效果我們通過演算法實現能夠求出函數h(x),但是我們還需要將資料視覺效果:(1)畫出訓練集的散佈圖+擬合後的直線;(2)畫出J(theta)為z軸,theta0為x軸,theta1為y軸的三維曲線;(3)畫出(2)的三維曲線的等高線圖; 1.畫散佈圖+擬合的直線

描述:給定ex1data1.txt,檔案中有兩列資料,每一列代表一個維度,第一列代表X,第二列代表Y,用Octave畫出散布圖(Scalar Plot),資料的形式如下:

6.1101,17.592

5.5277,9.1302

8.5186,13.662

7.0032,11.854

5.8598,6.8233

8.3829,11.886

........

答:(1)data = load('ex1data1.txt');             %匯入該檔案,並賦予data變數(2)X = data( : , 1 );Y = data( : , 2);    %將兩列分別賦予X和Y(3)X = [ones(size(X,1),1),X];                  %在X的左邊添加一列1(4)plot(X,Y,'rx','MarkerSize', 4);            %畫圖,將X向量作為X軸,Y向量作為Y軸,每個點用“x”表示,‘r’表示紅點,每個點的大小為4;(5)axis([4 24 -5 25]);                             %調整x和y軸的起始座標和最高座標;(6)xlabel('x');                                         %給x軸標號為‘x’;(7)ylabel('y');                                        %給y軸標號為‘y’;最後見:經過計算,算出theta值:[theta,J_history] = gradientDescent(X, y, theta, alpha, num_iters);即可通過:plot(X(:,2), X*theta);             %畫出最後擬合的直線以上呈現了線性迴歸的結果;以下兩種都是可視化J(theta);2.Surface Plot描述:資料如上一題一樣,我們想要繪製出對於這些資料的cost function,我們將繪製出三維圖形和contour plot;我們如果要繪製cost function,我們必須預先寫好cost function的公式:function J = computeCost(X, y, theta)    m = length(y);     J = 0;    predictions = X * theta;    J = 1/(2*m)*sum((predictions - y) .^ 2);end實現:(1)theta0_vals = linspace(-10, 10, 100);                  %從-10到10之間取100個數組成一個向量(2)theta1_vals = linspace(-1, 4, 100);                      %從-1到4之間取100個數組成一個向量(3)J_vals = zeros(length(theta0_vals), length(theta1_vals));   %初始化J_vals矩陣,對於某個theta0和theta1,J_vals都有對應的cost function值;(4)計算每個(theta0,theta1)所對應的J_vals;for i = 1:length(theta0_vals)    for j = 1:length(theta1_vals)  t = [theta0_vals(i); theta1_vals(j)];      J_vals(i,j) = computeCost(X, y, t);    endend(5)figure;                      %建立一個圖(6)surf(theta0_vals,theta1_vals,J_vals);  %x軸為theta0_vals,y軸為theta1_vals,z軸為J_vals;(7)xlabel('\theta_0');   %添加x軸標誌(8)ylabel('\theta_1');   %添加y軸標誌 此圖而且可以轉動;2.Contour Plot
實現:(1)theta0_vals = linspace(-10, 10, 100);                  %從-10到10之間取100個數組成一個向量(2)theta1_vals = linspace(-1, 4, 100);                      %從-1到4之間取100個數組成一個向量(3)J_vals = zeros(length(theta0_vals), length(theta1_vals));   %初始化J_vals矩陣,對於某個theta0和theta1,J_vals都有對應的cost function值;(4)計算每個(theta0,theta1)所對應的J_vals;for i = 1:length(theta0_vals)    for j = 1:length(theta1_vals)  t = [theta0_vals(i); theta1_vals(j)];      J_vals(i,j) = computeCost(X, y, t);    endend(5)figure;                      %建立一個圖(6)contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20));  %畫等高線圖(7)xlabel('\theta_0'); ylabel('\theta_1');如果我們想要在等高線圖上畫出線性迴歸的theta0與theta1的結果,則可以:plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);

 

 

4.畫圖查看Learning Rate是否合理
 我們在gradientDescent函數中返回的值裡有J_history向量,此向量記錄了每次迭代後cost function的值,因此我們只需要將x軸為迭代的次數,y軸為cost function的值,即可畫圖:(1)[theta,J_history] = gradientDescent(X, y, theta, alpha, num_iters);(2)figure;        (3)plot(1:length(J_history), J_history, '-b', 'LineWidth', 2);   (4)xlabel('Number of iterations');(5)ylabel('Cost J'); 當然,我們也可以將不同的alpha值都畫在一張圖上,可以比較取各個alpha時,cost function的變化趨勢; (1)alpha=0.01;(2)[theta,J1] = gradientDescent(X, y, zeros(3,1), alpha, num_iters);(3)alpha=0.03;(4)[theta,J2] = gradientDescent(X, y, zeros(3,1), alpha, num_iters);(5)alpha=0.1;(6)[theta,J3] = gradientDescent(X, y, zeros(3,1), alpha, num_iters);(7)plot(1:numel(J1), J1, '-b', 'LineWidth', 2);(8)plot(1:numel(J2), J2, '-r', 'LineWidth', 2);(9)plot(1:numel(J3), J3, '-k', 'LineWidth', 2);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.