Coursera機器學習-吳恩達
% 1.對特徵進行特徵縮放:
%首先讀取訓練集數據,有兩列特徵
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
%進行特徵縮放
[X mu sigma] = featureNormalize(X);
% 函數featureNormalize:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
[m n] = size(X);
for i = 1:n
mu(i) = mean(X_norm(:,i)); % 算每一列的平均值
sigma(i) = std(X_norm(:,i)); % 算每一列的標準差
% 計算公式:(每個特徵 - 特徵的平均值) / 特徵的標準差
X_norm(:,i) = (X_norm(:,i) - mu(i)) / sigma(i);
endfor
end
% 2.多元特徵代價函數J:
function J = computeCostMulti(X, y, theta)
m = length(y);
J = 0;
%計算J,公式如下圖:
J = 1/(2*m) * ( X * theta - y)' * (X * theta - y);
end
% 3.theta使用梯度下降算法更新:
% 選擇學習速率和迭代次數
alpha = 0.01;
num_iters = 400;
% 初始化並計算theta
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
% 函數gradientDescentMulti:
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
% 計算J
theta = theta - alpha/m * X' * (X * theta - y);
% 每次迭代均保存代價函數J:
J_history(iter) = computeCostMulti(X, y, theta);
end
end
% 3.用圖表展示代價函數J和迭代次數的關係:
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
% 4.預測一下,如用特徵縮放處理訓練集,則測試集也需要特徵縮放
price = 0;
% 測試集也需特徵縮放,假設函數h = X'* theta
price = [1 (1650-mu(1))/sigma(1) (3-mu(1))/sigma(1)] * theta;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
'(using gradient descent):\n $%f\n'], price);
% 5.使用正則化函數處理
data = csvread('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
X = [ones(m, 1) X];
% 計算theta,用正則化函數
theta = normalEqn(X, y);
% 函數normalEqn:
function [theta] = normalEqn(X, y)
theta = zeros(size(X, 2), 1);
% 計算theta,正則化公式如下:
theta = pinv(X' * X) * X' * y;
end
% 6.預測一波房價
% 正則化後訓練集不用特徵縮放;假設函數h = X' * theta
price = 0; % You should change this
price = [1 1650 3] * theta;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
'(using normal equations):\n $%f\n'], price);