前言:
本節來練習下logistic regression相關內容,參考的資料為網頁:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex4/ex4.html。這里給出的訓練樣本的特征為80個學生的兩門功課的分數,樣本值為對應的同學是否允許被上大學,如果是允許的話則用’1’表示,否則不允許就用’0’表示,這是一個典型的二分類問題。在此問題中,給出的80個樣本中正負樣本各占40個。而這節采用的是logistic regression來求解,該求解后的結果其實是一個概率值,當然通過與0.5比較就可以變成一個二分類問題了。
實驗基礎:
在logistic regression問題中,logistic函數表達式如下:
這樣做的好處是可以把輸出結果壓縮到0~1之間。而在logistic回歸問題中的損失函數與線性回歸中的損失函數不同,這里定義的為:
如果采用牛頓法來求解回歸方程中的參數,則參數的迭代公式為:
其中一階導函數和hessian矩陣表達式如下:
當然了,在編程的時候為了避免使用for循環,而應該直接使用這些公式的矢量表達式(具體的見程序內容)。
一些matlab函數:
find:
是找到的一個向量,其結果是find函數括號值為真時的值的下標編號。
inline:
構造一個內嵌的函數,很類似於我們在草稿紙上寫的數學推導公式一樣。參數一般用單引號弄起來,里面就是函數的表達式,如果有多個參數,則后面用單引號隔開一一說明。比如:g = inline('sin(alpha*x)','x','alpha'),則該二元函數是g(x,alpha) = sin(alpha*x)。
實驗結果:
訓練樣本的分布圖以及所學習到的分類界面曲線:
損失函數值和迭代次數之間的曲線:
最終輸出的結果:
可以看出當一個小孩的第一門功課為20分,第二門功課為80分時,這個小孩不允許上大學的概率為0.6680,因此如果作為二分類的話,就說明該小孩不會被允許上大學。
實驗代碼(原網頁提供):
% Exercise 4 -- Logistic Regression clear all; close all; clc x = load('ex4x.dat'); y = load('ex4y.dat'); [m, n] = size(x); % Add intercept term to x x = [ones(m, 1), x]; % Plot the training data % Use different markers for positives and negatives figure pos = find(y); neg = find(y == 0);%find是找到的一個向量,其結果是find函數括號值為真時的值的編號 plot(x(pos, 2), x(pos,3), '+') hold on plot(x(neg, 2), x(neg, 3), 'o') hold on xlabel('Exam 1 score') ylabel('Exam 2 score') % Initialize fitting parameters theta = zeros(n+1, 1); % Define the sigmoid function g = inline('1.0 ./ (1.0 + exp(-z))'); % Newton's method MAX_ITR = 7; J = zeros(MAX_ITR, 1); for i = 1:MAX_ITR % Calculate the hypothesis function z = x * theta; h = g(z);%轉換成logistic函數 % Calculate gradient and hessian. % The formulas below are equivalent to the summation formulas % given in the lecture videos. grad = (1/m).*x' * (h-y);%梯度的矢量表示法 H = (1/m).*x' * diag(h) * diag(1-h) * x;%hessian矩陣的矢量表示法 % Calculate J (for testing convergence) J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h));%損失函數的矢量表示法 theta = theta - H\grad;%是這樣子的嗎? end % Display theta theta % Calculate the probability that a student with % Score 20 on exam 1 and score 80 on exam 2 % will not be admitted prob = 1 - g([1, 20, 80]*theta) %畫出分界面 % Plot Newton's method result % Only need 2 points to define a line, so choose two endpoints plot_x = [min(x(:,2))-2, max(x(:,2))+2]; % Calculate the decision boundary line,plot_y的計算公式見博客下面的評論。 plot_y = (-1./theta(3)).*(theta(2).*plot_x +theta(1)); plot(plot_x, plot_y) legend('Admitted', 'Not admitted', 'Decision Boundary') hold off % Plot J figure plot(0:MAX_ITR-1, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', 8) xlabel('Iteration'); ylabel('J') % Display J J
參考資料: