我认为逻辑回归可以用于回归(获得0和1之间的数字,例如使用逻辑回归来预测0和1之间的概率)和分类。问题是,在我们提供培训数据和目标后,逻辑回归似乎可以自动判断我们是在进行回归还是进行分类?
例如,在下面的示例代码中,logistic回归计算出我们只需要输出为3个类0, 1, 2
中的一个,而不是0
和2
之间的任何数字?只是好奇逻辑回归如何自动判断出它是回归(输出是连续范围)还是分类(输出是离散的)问题?
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()
答案 0 :(得分:1)
Logistic回归通常使用交叉熵成本函数,该函数根据二进制误差对损失进行建模。此外,逻辑回归的输出通常在决策边界处遵循sigmoid,这意味着虽然决策边界可能是线性的,但输出(通常被视为表示边界两侧的两个类之一的点的概率)转换以非线性方式。这将使您的回归模型从0到1成为非常特殊的非线性函数。在某些情况下这可能是合乎需要的,但通常不太可取。
您可以将逻辑回归视为提供表示在班级中的概率的幅度。如果考虑具有两个独立变量的二元分类器,则可以绘制一个表面,其中决策边界是概率为0.5的拓扑线。在分类器确定类别的情况下,表面要么处于平台(概率= 1),要么处于低位区域(概率= 0)。从低概率区域到高概率区域的转换通常遵循S形函数。
你可以看看Andrew Ng的Coursera课程,该课程有一套关于逻辑回归的课程。 This是第一个课程。我有一个github repo,它是该类输出的R版本here,您可能会发现它有助于更好地理解逻辑回归。