2

只有一個數字特徵時,使用scikit-learnLogisticRegression求解器的正確方法是什麼?只有一個數字特徵的邏輯迴歸

我跑了一個簡單的例子,我發現很難解釋。任何人都可以請解釋我在這裏做錯了嗎?

import pandas 
import numpy as np 
from sklearn.linear_model import LogisticRegression 

X = [1, 2, 3, 10, 11, 12] 
X = np.reshape(X, (6, 1)) 
Y = [0, 0, 0, 1, 1, 1] 
Y = np.reshape(Y, (6, 1)) 

lr = LogisticRegression() 

lr.fit(X, Y) 
print ("2 --> {0}".format(lr.predict(2))) 
print ("4 --> {0}".format(lr.predict(4))) 

這是腳本結束運行時得到的輸出。 不應該將4的預測值設爲0,因爲根據高斯分佈4更接近根據測試集被分類爲0的分佈。

2 --> [0] 
4 --> [1] 

什麼是邏輯迴歸當你只有一列數字數據時所需要的方法?

回答

4

你用正確方式處理一個單一的功能,但你錯誤地認爲,僅僅因爲4接近,它也將被預測爲0類的功能,

可以沿繪製你的訓練數據乙狀結腸功能,假設y=0.5分類閾值,並利用學習係數和攔截從你的迴歸模型:

import numpy as np 
import matplotlib.pyplot as plt 
from sklearn.linear_model import LogisticRegression 

X = [1, 2, 3, 10, 11, 12] 
X = np.reshape(X, (6, 1)) 
Y = [0, 0, 0, 1, 1, 1] 
Y = np.reshape(Y, (6, 1)) 

lr = LogisticRegression() 
lr.fit(X, Y) 

plt.figure(1, figsize=(4, 3)) 
plt.scatter(X.ravel(), Y, color='black', zorder=20) 

def model(x): 
    return 1/(1 + np.exp(-x)) 

X_test = np.linspace(-5, 15, 300) 
loss = model(X_test * lr.coef_ + lr.intercept_).ravel() 

plt.plot(X_test, loss, color='red', linewidth=3) 
plt.axhline(y=0, color='k', linestyle='-') 
plt.axhline(y=1, color='k', linestyle='-') 
plt.axhline(y=0.5, color='b', linestyle='--') 
plt.axvline(x=X_test[123], color='b', linestyle='--') 

plt.ylabel('y') 
plt.xlabel('X') 
plt.xlim(0, 13) 
plt.show() 

這裏是雙曲線函數看起來像你的情況:

enter image description here

放大一點:

enter image description here

爲特定模式,X值時Y處於0.5分類閾值是某處3.1613.227之間。

那麼爲什麼4 - 您可以通過比較lossX_test陣列(你可以使用一些功能的優化方法來得到一個確切的數值,如果你想X_test[123]與上限相關聯的X值)檢查此被預測爲類1是因爲4高於開往時Y == 0.5

可以進一步與以下顯示此:

print ("2 --> {0}".format(lr.predict(2))) 
print ("3 --> {0}".format(lr.predict(3))) 
print ("3.1 --> {0}".format(lr.predict(3.1))) 
print ("3.3 --> {0}".format(lr.predict(3.3))) 
print ("4 --> {0}".format(lr.predict(4))) 

這將打印出以下:

2 --> [0] 
3 --> [0] 
3.1 --> [0] # Below threshold 
3.3 --> [1] # Above threshold 
4 --> [1] 
+0

我稍後才意識到這一點。但是這些圖表確實將這一點推向了家庭。 :) – rgk