你用正確方式處理一個單一的功能,但你錯誤地認爲,僅僅因爲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()
這裏是雙曲線函數看起來像你的情況:
放大一點:
爲特定模式,X
值時Y
處於0.5分類閾值是某處3.161
和3.227
之間。
那麼爲什麼4 - 您可以通過比較loss
和X_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]
我稍後才意識到這一點。但是這些圖表確實將這一點推向了家庭。 :) – rgk