1. 程式人生 > 實用技巧 >一元線性迴歸例題 - 餐館擴建

一元線性迴歸例題 - 餐館擴建

一元線性迴歸例題 - 餐館擴建

IIn this part of this exercise, you will implement linear regression with one variable to predict profits for a food truck. Suppose you are the CEO of a restaurant franchise and are considering different cities for opening a new outlet. The chain already has trucks in various cities and you have data for profits and populations from the cities.You would like to use this data to help you select which city to expand to next.

1. 選擇模型

進行擬合的第一步應該觀察樣本,選擇合適的模型進行擬合。

首先載入訓練集,將訓練集分為X、Y兩個集合。

data = load('~/Desktop/ex1data1.txt');
X = data(:,1);
y = data(:,2);
m = length(y);   % 樣本數量

然後繪製訓練集的影象,用來選擇模型。這裡新建一個函式用來繪製訓練集的影象

function plotData(x,y)
figure;   % 新建畫布
plot(x,y,'ks','MarkerSize',6);
title("Training set");
xlabel("Population of City in 10,000s");
ylabel("Profit in $10,000s");
end

呼叫plotData後得到函式影象如下

根據訓練集影象,選擇一元線性模型進行擬合。

2. 梯度下降

確定擬合模型後,採用梯度下降法進行引數擬合。

X = [ones(m,1), data(:,1)];   % 增加 x0 屬性
theta = zeros(2,1);   % 根據訓練集影象初始化引數為[2,1]
iterations = 1500;
alpha = 0.01;

這裡單獨將梯度下降的過程封裝成函式

function theta = gradientDescent(X,y,theta,alpha,num_iters)
m = length(y);
for iter = 1:num_iters
	theta = theta - alpha * 1/m * X' * (X * theta - y);   % 向量化計算
end
end

3. 繪製圖像

在主函式中呼叫 gradientDescent 函式進行擬合,並繪製最終的\(h_\theta(x)\)

theta = gradientDescent(X,y,theta,alpha,iterations);

還可以繪製\(J(\theta)\)的三維影象,\(J(\theta)\)的等高線來進一步理解梯度下降。

function J = computeCost(X,y,theta)
	m = length(y);
	J = 0;
	p = X * theta - y;
	J = 1/(2*m) * sum(p .* p);
end
% h(x)
testx = 1:1:30;
testy = theta(1) + theta(2) * testx;
hold on;
plot(testx,testy);
hold off;

% Grid over which we will calculate J
theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);

% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_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);
    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';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');

% Contour plot
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', 2);

4. 補充技巧:特徵歸一化

function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X 
%   FEATURENORMALIZE(X) returns a normalized version of X where
%   the mean value of each feature is 0 and the standard deviation
%   is 1. This is often a good preprocessing step to do when
%   working with learning algorithms.

% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));

% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
%               of the feature and subtract it from the dataset,
%               storing the mean value in mu. Next, compute the 
%               standard deviation of each feature and divide
%               each feature by it's standard deviation, storing
%               the standard deviation in sigma. 
%
%               Note that X is a matrix where each column is a 
%               feature and each row is an example. You need 
%               to perform the normalization separately for 
%               each feature. 
mu = mean(X); % X:m*2 , mu: 2 vector mu(1) : X_1) mu(2) : X_2
sigma = std(X);
X_norm(:,1) = (X_norm(:,1) - mu(1)) / sigma(1);
X_norm(:,2) = (X_norm(:,2) - mu(2)) / sigma(2);
% Hint: You might find the 'mean' and 'std' functions useful.
%       
% =========================================================

end

[X, mu, sigma] = featureNormalize(X);