2014-09-20 75 views
0

我嘗試使用邏輯迴歸多項式功能,幸運的是它對我來說工作得很好,而且我能夠繪製決策曲線。我已經使用map_feature函數來獲得多項式特徵。 (我把安德魯教授關於邏輯迴歸的註釋轉化爲正則化):http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html繪製高斯內核邏輯迴歸決策曲線

現在我試圖用高斯內核來取代多項式特徵。幸運的是,我的成本函數(j_theta)可以正常工作,並在每次迭代後都會減少,並獲得最終的theta值。 ,我現在面臨的問題是如何繪製這裏的決策邊界

I am using Octave to develop the algorithms and plot the graphs.. 

下面是詳細信息到我的數據集大小

原始數據集:

Data Set (x): [20*3] where the first column is the intercept or the bias column 

1.00 2.0000 1.0000 
1.00 3.0000 1.0000 
1.00 4.0000 1.0000 
1.00 5.0000 2.0000 
1.00 5.0000 3.0000 
. 
. 
. 

數據設定Gaussian Kernal實施後的新功能

Data set (f) : [20*21] the first column is the intercept column with all values as 1 

1.0000e+000 1.0000e+000 6.0653e-001 1.3534e-001 6.7379e-003 . . . . . . . . 
1.0000e+000 6.0653e-001 1.0000e+000 6.0653e-001 8.2085e-002 . . . . . . . . 
1.0000e+000 1.3534e-001 6.0653e-001 1.0000e+000 3.6788e-001 
1.0000e+000 6.7379e-003 8.2085e-002 3.6788e-001 1.0000e+000 
.    . 
.    . 
.    . 
.    . 
.    . 

成本函數圖,我在我的新功能的數據集(F)應用梯度下降後得到的是:

enter image description here

因此,我把我的新THETA值:

theta: [21*1] 
3.8874e+000 
1.1747e-001 
3.5931e-002 
-8.5937e-005 
-1.2666e-001 
-1.0584e-001 
. 
. 
. 

的問題,我現在面對的是如何構建我的決策曲線在具有新特徵數據集和theta值的原始數據集上。我不知道該如何着手。

我會很高興,如果我得到一些線索,或教程,或鏈接,可以幫助我解決我的問題。

感謝您的幫助。謝謝

回答

1

引用的安德魯的note實際上包含如何繪製決策邊界的一個很好的例子。另請參閱this stackoverflow後。要遵循的基本步驟如下:

  1. 根據輸入數據的範圍或特徵向量X選擇分辨率。
  2. 創建由分辨率內的每個點創建的網格。
  3. 訪問網格中的每個點,使用您學到的邏輯迴歸模型,預測得分。
  4. 使用得分作爲Z變量(等高線圖上的高度),繪製輪廓曲線。

在下面的示例代碼,我們假定一個二維特徵空間的每個範圍從-1到200我們選擇1.5的步長大小,然後在網格中的每個點,我們稱之爲模型predictor - map_feature(u,v) x theta獲得積分。最後通過在matlab中調用contour函數繪製該圖。

在此繪製決策邊界將比在線性迴歸中繪製 最佳擬合曲線要棘手。您將需要通過繪製輪廓來繪製線條的陰影。這可以通過在代表 原始$ u $和$ v $輸入的點網格上評估$ \ theta^Tx $完成,然後繪製其中 $ \ theta^Tx $計算爲零的線。 Matlab/Octave的繪圖實現如下。

% Define the ranges of the grid 
u = linspace(-1, 1.5, 200); 
v = linspace(-1, 1.5, 200); 

% Initialize space for the values to be plotted 
z = zeros(length(u), length(v)); 

% Evaluate z = theta*x over the grid 
for i = 1:length(u) 
    for j = 1:length(v) 
     % Notice the order of j, i here! 
     z(j,i) = map_feature(u(i), v(j))*theta; 
    end 
end 

% Because of the way that contour plotting works 
% in Matlab, we need to transpose z, or 
% else the axis orientation will be flipped! 
z = z' 
% Plot z = 0 by specifying the range [0, 0] 
contour(u,v,z, [0, 0], 'LineWidth', 2) 

enter image description here

+0

嗨Greeness,我看到了帖子,其實劇情是適用於2D,但是當我考慮Logistic迴歸或使用高斯核SVM我的特點變成訓練組數。也就是說,雖然我的原始特徵只有兩個,但當我使用高斯核時,我的新特徵集轉換爲訓練集的數目。因此我的theta是從m(不是訓練集)維的新特徵集中學習的。我想知道是否有某種方法可以將數據轉換回2D特徵集並獲得決策邊界。 – Sam 2014-10-24 07:15:31