Exercise 1:Linear Regression---實現一個線性回歸
在本次練習中,需要實現一個單變量的線性回歸。假設有一組歷史數據<城市人口,開店利潤>,現需要預測在哪個城市中開店利潤比較好?
歷史數據如下:第一列表示城市人口數,單位為萬人;第二列表示利潤,單位為10,000$
5.5277 9.1302
8.5186 13.6620
7.0032 11.8540
.....
......
用Matlab畫出的圖形如下:首先加載數據,將data中的第一列數據保存到X中,將data中的所有行的第2列數據保存到y中
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);
plotData.m代碼如下:執行plot函數畫圖;xlabel、ylabel分別給X軸和Y軸標記提示信息。
function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure
% PLOTDATA(x,y) plots the data points and gives the figure axes labels of
% population and profit.
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the
% "figure" and "plot" commands. Set the axes labels using
% the "xlabel" and "ylabel" commands. Assume the
% population and revenue data have been passed in
% as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
% appear as red crosses. Furthermore, you can make the
% markers larger by using plot(..., 'rx', 'MarkerSize', 10);
figure; % open a new figure window
plot(x,y,'rx','MarkerSize',10); ylabel('Profit in $10,000s');
xlabel('Population of City in 10,000s');
% ============================================================
end
畫出來的圖形如下:

①假設函數(hypothesis function)
在給定一些樣本數據(training set)后,采用某種學習算法(learning algorithm)對樣本數據進行訓練,得到了一個模型或者說是假設函數。
當需要預測新數據的結果時,將新數據作為假設函數的輸入,假設函數計算后得到結果,這個結果就作為預測值。

假設函數的表示形式一般如下:θ 稱為模型的參數(或者是:權重weights),x就是輸入變量(input variables or feature variables)

可以看出,假設函數h(x)是關於x的函數,只要確定了 θ ,就求得了假設函數 (θ 也可視為一個向量)。那么對於新的輸入樣本x,就可以預測該樣本的結果y了。
上面假設函數是從0到n求和,也就是說:對於每個輸入樣本x,將它看成一個向量,每個x中有n+1個 features。比如預測房價,那輸入的樣本 x(房子的大小,房子所在的城市,衛生間個數,陽台個數.....一系列的特征)
關於分類問題和回歸問題:假設函數的輸出結果y(predicted y)有兩種表示形式:離散的值和連續的值。比如本文中講到的預測利潤,這個結果就是屬於連續的值;再比如說根據歷史的天氣情況預測明天的天氣(下雨 or 不下雨),那預測的結果就是離散的值(discrete values)
因此,若hypothesis function輸出是連續的值,則稱這類學習問題為回歸問題(regression problem),若輸出是離散的值,則稱為分類問題(classification problem)
②代價函數(cost function)
學習過程就是確定假設函數的過程,或者說是:求出 θ 的過程。
現在先假設 θ 已經求出來了,就需要判斷求得的這個假設函數到底好不好?它與實際值的偏差是多少?因此,就用代價函數來評估。


一般地,用 m 來表示訓練樣本的數目(size of training set),x(i) 表示第 i 個樣本,y(i) 表示第i個樣本的預測結果。
從上圖可看出:代價函數與“最小均方差”的理念非常相似。J(θ)是 θ 函數。
顯然,“代價函數越小,模型就越好”。因此,目標就是:找到一組合適的 θ ,使得代價函數取最小值。
如果我們找到了 θ ,那不就求得了 假設函數了?也就求得一個模型--linear regression model.
那如何找 θ 呢?就是下面提到的梯度下降算法(Gradient descent algorithm)
③梯度下降算法(Gradient descent algorithm)
梯度下降算法的本質就是求偏導數,令偏導數等於0,解出 θ

首先從一個初始 θ 開始,然后 for 循環執行上面公式,當偏導數等於0時,θj 就不會再更新了,此時就得到一個最終θj 值。
整個偏導數的運算過程如下:

④假設函數、代價函數和梯度下降算法的向量表示
假設函數的向量表示如下:

代價函數的表示如下:

使用梯度下降算法求解 θ 的向量表示如下:

證明過程如下:


⑤Matlab語言表示 代價函數和梯度下降算法
梯度下降算法表示如下:(gradientDescent.m)
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
theta = theta - (alpha/m)*X'*(X*theta-y); % theta 就是用上面的向量表示法的 matlab 語言實現 % ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
代價函數表示如下:(computeCost.m)
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
J = sum((X*theta-y).^2)/(2*m); % =========================================================================
end
運行上面的Matlab程序,求得的線性回歸模型如下圖:

它的一個預測結果如下:
For population = 35,000, we predict a profit of 4519.767868
For population = 70,000, we predict a profit of 45342.450129
代價函數J(θ0 , θ1)的圖形如下:(因為我們只有一個特征變量(population)--不考慮x0)。因此,共有兩個模型參數,θ0對應着x0, θ1對應着x1

兩個變量:θ0 , θ1,因此畫出來的圖形可用三維空間來表示。
原文:http://www.cnblogs.com/hapjin/p/6079012.html
