吳恩達機器學習配套編程練習第一週(Octave/Matlab)
本文僅供個人學習記錄,儘量寫的詳細,可供參考
(注:吳恩達課程的練習只需要再他所給的一個個函數文件中增添代碼,最後運行‘主函數(ex.m)’文件即可
%% Machine Learning Online Class - Exercise 1: Linear Regression
% Instructions
% ------------
%
% This file contains code that helps you get started on the
% linear exercise. You will need to complete the following functions
% in this exericse:
%
% warmUpExercise.m
% plotData.m
% gradientDescent.m
% computeCost.m
% gradientDescentMulti.m
% computeCostMulti.m
% featureNormalize.m
% normalEqn.m
%
% For this exercise, you will not need to change any code in this file,
% or any other files other than those mentioned above.
%
% x refers to the population size in 10,000s
% y refers to the profit in $10,000s
%
註釋介紹如圖,大概就包括了上面幾個Octave函數文件,本次分爲兩個任務,第一個是單變量的線性線性迴歸和多變量線性迴歸,其主文件對應作業包中的ex1.m和ex1_multi.m
首先第一步利用他所提供的數據文件,進行圖片繪製
%% ======================= Part 2: Plotting =======================
fprintf('Plotting Data ...\n')
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
% Plot Data
% Note: You have to complete the code in plotData.m
plotData(X, y);
讀取文件用X存第一列數據,y存第二列,m爲訓練集的個數,最後調用plotData函數,如圖
function plotData(x, y)
figure; % open a new figure window
plot(x, y, 'rx', 'MarkerSize', 10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y axis label
xlabel('Population of City in 10,000s'); % Set the x axis label
end
其中可能不明白意思我解釋一下plot中 ‘rx’表示用紅色的叉叉來描點,然後markersize標記大小爲10
畫出來大概這樣
接着我們要運行梯度下降算法來找出一條直線擬合這些數據,來看主函數體中的第二步要求我們做什麼
%% =================== Part 3: Gradient descent ===================
fprintf('Running Gradient Descent ...\n')
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters
% Some gradient descent settings
iterations = 1500;
alpha = 0.01;
% compute and display initial cost
computeCost(X, y, theta)
% run gradient descent
theta = gradientDescent(X, y, theta, alpha, iterations);
% print theta to screen
fprintf('Theta found by gradient descent: ');
fprintf('%f %f \n', theta(1), theta(2));
其中我們只需要修改computeCost和gradientDescent函數即可
function J = computeCost(X, y, theta)
J = 0;
J = sum((X * theta - y).^2) / (2*m); % X(79,2) theta(2,1)
end
對應公式應該不難寫出
再看gradientDescent函數
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
theta(1) = theta(1) - alpha / m * sum(X * theta_s - y);
theta(2) = theta(2) - alpha / m * sum((X * theta_s - y) .* X(:,2));
J_history(iter) = computeCost(X, y, theta);
end
J_history %打印顯示
end
同樣根據課上所講的梯度下降公式,對應公式直接寫出來,進行num_iters次的迭代不斷更新theta並且再J_history中記錄下每次迭代的Cost。接着我們開始繪圖
% Plot the linear fit
hold on; % keep previous plot visible
plot(X(:,2), X*theta, 'g-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure
成功擬合訓練集數據的直線大概長這樣
接着我們就需要用兩個新數據來預測下
% Predict values for population sizes of 35,000 and 70,000
predict1 = [1, 3.5] *theta;
fprintf('For population = 35,000, we predict a profit of %f\n',
predict1*10000);
predict2 = [1, 7] * theta;
fprintf('For population = 70,000, we predict a profit of %f\n',
predict2*10000);
fprintf('Program paused. Press enter to continue.\n');
pause;
運行之後得到結果
預測數據成功
最後我們用三維圖像和等高線來可視化J函數(數學意義上的函數)看看,注意函數的參數應該是theta0和theta1
%% ============= Part 4: Visualizing J(theta_0, theta_1) =============
fprintf('Visualizing J(theta_0, theta_1) ...\n')
% Grid over which we will calculate J
theta0_vals = linspace(-10, 10, 100);%linespace講-10到10分成100等分,也就是一個1X100的行向量
theta1_vals = linspace(-1, 4, 100);%類似
% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));%初始化一個空的J_vals矩陣
% Fill out 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);%對每一個theta0 theta1進行代價計算並記錄
end
end
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';%轉置,surf函數的要求。
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');
% Contour plot 等高線繪製,看看就行了,吳恩達的作業要求也只是要我們看懂就行,如果函數不懂的可以用help XXX來查閱
figure;
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))
xlabel('\theta_0'); ylabel('\theta_1');
hold on;
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 20);
我把解釋寫在了代碼中,最後繪圖結果:
單變量的線性迴歸就這樣了,其實多變量的線性迴歸很類似