機器學習之多變量線性迴歸

機器學習之多變量線性迴歸

  • 描述
  • 代價函數
  • 梯度下降
  • 特徵值縮放
  • 正規方程
  • 可視化
  • 使用正規方程求解最優值

簡述

  • 多變量指的是輸入特徵有多個。
  • 根據吳恩達老師的機器學習課程推薦,機器學習研究最好使用octave/matlab作為工具,效率更高,方便移植。
  • 本文使用octave進行學習。
機器學習之多變量線性迴歸

代價函數

computeCostMulti.m 用來計算多個輸入特徵時的代價函數:

機器學習之多變量線性迴歸

機器學習之多變量線性迴歸

function J = computeCostMulti (X, y, theta)
m = length(y);
J = 0;
J = sum((X * theta - y).^2) / (2*m);
endfunction

梯度下降

gradientDescentMulti.m 梯度下降函數:

機器學習之多變量線性迴歸

機器學習之多變量線性迴歸

和單變量是不一樣的,但是也適用於單變量。以下做梯度下降的同時用J_history記錄了每次代價函數J(θ)的值。

function [theta, J_history] = gradientDescentMulti (X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1 : num_iters
\ttheta = theta - alpha / m * X' * (X * theta - y);
\tJ_history(iter) = computeCostMulti(X, y, theta);
end
endfunction

特徵值縮放

featureNormalize.m 為了優化梯度下降的收斂速度,採用特徵縮放的技巧,使各特徵值的範圍儘量一致。

function [X_norm, mu, sigma] = featureNormalize (X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu = mean(X);
sigma = std(X);
X_norm = (X - repmat(mu, size(X, 1), 1)) ./ repmat(sigma, size(X, 1), 1);
endfunction

正規方程

normalEqn.m 對於一些線性迴歸問題來說,正規方程法給出了一個更好的解決問題的方式,但只適用線性模型,且矩陣需可逆:

機器學習之多變量線性迴歸

function [theta] = normalEqn (X, y)
theta = zeros(size(X, 2), 1);
theta = inv(X' * X) * X' * y;
endfunction

可視化

綜合上述的函數,對數據進行可視化操作。

1. 加載數據並把數據進行特徵縮放處理,獲取數據ex1data2.txt (https://github.com/peedeep/Coursera/blob/master/ex1/ex1data2.txt)進入網址下載。

%% Exercise 1: Linear regression with multiple variables
%% ================ 1.Feature Normalization ================
clear; close all; clc
fprintf('Loading data ...\\n');
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
fprintf('First 10 examples from the dataset: \\n');
fprintf(' x = [%.0f %.0f], y = %0.f \\n', [X(1:10, :), y(1:10,:)]');
fprintf('Program paused. Press enter to continue.\\n');
pause;
fprintf('Normalizing Features ...\\n');
[X mu sigma] = featureNormalize(X);
X = [ones(m, 1), X];

2. 調用方法進行梯度下降,並畫出隨著迭代次數的變化J(θ)的值

%% ================ 2.Gradient Descent ================
fprintf('Running gradient descent...\\n');
alpha = 0.01;
num_iters = 8500;
theta = zeros(3, 1);
[theta J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);

figure;
plot(1: numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
fprintf('theta from gradient descent\\n');
fprintf('theta: %f\\n', theta);
fprintf('\\n');
price = [1 (([1650 3] - mu) ./ sigma)] * theta;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house(using gradient descent):\\n $%f\\n'], price);
fprintf('Program paused. Press enter to continue.\\n');
pause;
機器學習之多變量線性迴歸

3. 用三維圖演示出訓練數據真實值與預測值

機器學習之多變量線性迴歸

使用正規方程求解最優值

%% ================ 3.Normal Equations ================
data = csvread('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
X = [ones(m, 1), X];
theta = normalEqn(X, y);
fprintf('Theta computed from the normal equations: \\n');
fprintf(' %f \\n', theta);
fprintf('\\n');
price = [1 1650 3] * theta;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house(using normal equations):\\n $%f\\n'], price);

結果使用梯度下降得到的 θ與正規方程得到的 θ進行預測的時候結果是一樣的!


分享到:


相關文章: