簡體   English   中英

scikit如何學習找出用於分類或回歸的邏輯回歸

[英]how scikit learn figure out logistic regression for classification or regression

我認為邏輯回歸可以用於回歸分析(獲取0到1之間的數字,例如,使用邏輯回歸來預測0到1之間的概率)和分類。 問題是,似乎在我們提供了訓練數據和目標之后,邏輯回歸可以自動確定我們是進行回歸還是進行分類?

例如,在下面的示例代碼中,邏輯回歸分析得出,我們只需要輸出為0, 1, 2之3類之一即可,而不是02之間的任何數字? 好奇Logistic回歸如何自動確定是進行回歸(輸出是連續范圍)還是分類(輸出是離散)問題?

http://scikit-learn.org/stable/auto_examples/linear_model/plot_iris_logistic.html

print(__doc__)


# Code source: Gaël Varoquaux
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause

import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model, datasets

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2]  # we only take the first two features.
Y = iris.target

h = .02  # step size in the mesh

logreg = linear_model.LogisticRegression(C=1e5)

# we create an instance of Neighbours Classifier and fit the data.
logreg.fit(X, Y)

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, m_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure(1, figsize=(4, 3))
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors='k', cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())

plt.show()

Logistic回歸通常使用交叉熵成本函數,該函數根據二進制誤差對損失進行建模。 此外,邏輯回歸的輸出通常在決策邊界處遵循S形,這意味着盡管決策邊界可能是線性的,但輸出(通常被視為表示邊界兩側的兩個類別之一的點的概率)以非線性方式。 這將使您的從0到1的回歸模型成為一個非常特殊的非線性函數。 在某些情況下這可能是合乎需要的,但通常可能不合要求。

您可以將邏輯回歸視為提供一個幅度,該幅度表示是否在某個班級中。 如果考慮具有兩個自變量的二元分類器,則可以描繪出一個表面,其中決策邊界是概率為0.5的拓撲線。 如果分類器是該類別中的某個分類器,則該表面位於平台上(概率= 1)或位於低窪地區(概率= 0)。 從低概率區域到高概率區域的過渡通常遵循S型函數。

您可能會看到Andrew Ng的Coursera課程,其中有一組關於邏輯回歸的課程。 是第一堂課。 我有一個github存儲庫,它是該類輸出的R版本, 在這里 ,您可能會發現它有助於更​​好地理解邏輯回歸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM