本筆記主要記錄學習《機器學習》的總結體會。若有理解不到位的地方,歡迎你們指出,我會努力改正。算法
在學習《機器學習》時,我主要是經過Andrew Ng教授在mooc上提供的《Machine Learning》課程,不得不說Andrew Ng老師在講授這門課程時,真的很用心,特別是編程練習,這門課真的很nice,在此謝謝Andrew Ng老師的付出。同時也謝過告知這個平臺的小夥伴。本文在寫的過程當中,多有借鑑Andrew Ng教授在mooc提供的資料,再次感謝。編程
轉載請註明出處:http://blog.csdn.net/u010278305
機器學習
什麼是機器學習?我認爲機器學習就是,給定必定的信息(如一間房子的面子,一幅圖片每一個點的像素值等等),經過對這些信息進行「學習」,得出一個「學習模型「,這個模型能夠在有該類型的信息輸入時,輸出咱們感興趣的結果。比如咱們若是要進行手寫數字的識別,已經給定了一些已知信息(一些圖片和這些圖片上的手寫數字是多少),咱們能夠按如下步驟進行學習:函數
一、將這些圖片每一個點的像素值與每一個圖片的手寫數字值輸入」學習系統「。學習
二、經過」學習過程「,咱們獲得一個」學習模型「,這個模型能夠在有新的手寫數字的圖片輸入時,給出這張圖片對應手寫數字的合理估計。
測試
什麼是線性迴歸?個人理解就是,用一個線性函數對提供的已知數據進行擬合,最終獲得一個線性函數,使這個函數知足咱們的要求(如具備最小平方差,隨後咱們將定義一個代價函數,使這個目標量化),以後咱們能夠利用這個函數,對給定的輸入進行預測(例如,給定房屋面積,咱們預測這個房屋的價格)。以下圖所示:.net
假設咱們最終要的獲得的假設函數具備以下形式:scala
其中,x是咱們的輸入,theta是咱們要求得的參數。code
代價函數以下:blog
咱們的目標是使得此代價函數具備最小值。
爲此,咱們還須要求得代價函數關於參量theta的導數,即梯度,具備以下形式:
有了這些信息以後,咱們就能夠用梯度降低算法來求得theta參數。過程以下:
其實,爲了求得theta參數,有更多更好的算法能夠選擇,咱們能夠經過調用matlab的fminunc函數實現,而咱們只需求出代價與梯度,供該函數調用便可。
根據以上公式,咱們給出代價函數的具體實現:
function J = computeCostMulti(X, y, theta) %COMPUTECOSTMULTI Compute cost for linear regression with multiple variables % J = COMPUTECOSTMULTI(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; % Instructions: Compute the cost of a particular choice of theta % You should set J to the cost. hThetaX=X*theta; J=1/(2*m)*sum((hThetaX-y).^2); end
什麼是邏輯迴歸?相比於線性迴歸,邏輯迴歸只會輸出一些離散的特定值(例如斷定一封郵件是否爲垃圾郵件,輸出只有0和1),並且對假設函數進行了處理,使得輸出只在0和1之間。
假設函數以下:
代價函數以下:
梯度函數以下,觀察可知,形式與線性迴歸時同樣:
有了這些信息,咱們就能夠經過fminunc求出最優的theta參數,咱們只需給出代價與梯度的計算方式,代碼以下:
function [J, grad] = costFunction(theta, X, y) %COSTFUNCTION Compute cost and gradient for logistic regression % J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the % parameter for logistic regression and the gradient of the cost % w.r.t. to the parameters. % Initialize some useful values m = length(y); % number of training examples % You need to return the following variables correctly J = 0; grad = zeros(size(theta)); % Instructions: Compute the cost of a particular choice of theta. % You should set J to the cost. % Compute the partial derivatives and set grad to the partial % derivatives of the cost w.r.t. each parameter in theta % % Note: grad should have the same dimensions as theta % hThetaX=sigmoid(X * theta); J=1/m*sum(-y.*log(hThetaX)-(1-y).*log(1-hThetaX)); grad=(1/m*(hThetaX-y)'*X)'; end
其中,sigmod函數以下:
function g = sigmoid(z) %SIGMOID Compute sigmoid functoon % J = SIGMOID(z) computes the sigmoid of z. % You need to return the following variables correctly g = zeros(size(z)); % Instructions: Compute the sigmoid of each value of z (z can be a matrix, % vector or scalar). e=exp(1); g=1./(1+e.^-z); end
有時,會出現」過擬合「的狀況,即求得的參數可以很好的擬合訓練集中的數據,但在進行預測時,明顯與趨勢不符,比如下圖所示:
此時,咱們須要進行正則化處理,對參數進行懲罰,使得除theta(1)以外的theta值均保持較小值。
進行正則化以後的代價函數以下:
進行正則化以後的梯度以下:
下面給出正則化以後的代價與梯度值得代碼:
function [J, grad] = costFunctionReg(theta, X, y, lambda) %COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization % J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using % theta as the parameter for regularized logistic regression and the % gradient of the cost w.r.t. to the parameters. % Initialize some useful values m = length(y); % number of training examples % You need to return the following variables correctly J = 0; grad = zeros(size(theta)); % Instructions: Compute the cost of a particular choice of theta. % You should set J to the cost. % Compute the partial derivatives and set grad to the partial % derivatives of the cost w.r.t. each parameter in theta hThetaX=sigmoid(X * theta); theta(1)=0; J=1/m*sum(-y.*log(hThetaX)-(1-y).*log(1-hThetaX))+lambda/(2*m)*sum(theta.^2); grad=(1/m*(hThetaX-y)'*X)' + lambda/m*theta; end
對於線性迴歸,正則化的過程基本相似。
至於如何選擇正則化時的常數lambda,咱們能夠將數據分爲訓練集、交叉驗證集和測試集三部分,在不一樣lambda下,先用訓練集求出參數theta,以後求出訓練集與交叉驗證集的代價,經過分析得出適合的lambda。以下圖所示:
轉載請註明出處:http://blog.csdn.net/u010278305