Python Sklearn Logistic回归模型错误拟合

时间:2017-06-25 08:18:00

标签: python scikit-learn

对于逻辑回归,我试图从Wikipedia logistic regression页面重现结果。所以,我的代码如下所示:

import numpy as np
from sklearn.linear_model import LogisticRegression

x = np.array([0.5, 0.75, 1, 1.25, 1.5, 1.75, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 4, 4.25, 4.5, 4.75, 5, 5.5])
y = np.array([0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1])

logistic = LogisticRegression()
logistic.fit(x[:, None], y)

但是如何获得拟合模型的摘要,具体如下:

            Coefficient  Std.Error  z-value  P-value (Wald)
Intercept   −4.0777      1.7610     −2.316    0.0206
Hours        1.5046      0.6287      2.393    0.0167

这就是维基百科页面对拟合模型的看法。如果我尝试使用系数和截距的打印,我将收到类似的内容:

print(logistic.coef_)
print(logistic.intercept_)
  

[[0.61126347]]

     

[ - 1.36550178]

这显然是不同的。

问题是,为什么我的结果与维基百科页面上的结果不同?

2 个答案:

答案 0 :(得分:4)

维基百科示例不包括模型参数的正则化,但sklearn的LogisticRegression默认使用L2正则化。 Set the inverse regularization strength, C, to a very high value to use no regularization,例如,

logistic = LogisticRegression(penalty='l2', C=1e4)
logistic.fit(x[:, None],y)

print(logistic.coef_)
print(logistic.intercept_)

# [[ 1.50459727]]
# [-4.07757136]

答案 1 :(得分:3)

sklearn中没有R类型的摘要报告。

对于分类任务,存在一个函数:sklearn.metrics.classification_report,它可以计算几种类型的(预测)分数。

要获得R样式摘要报告,请查看statsmodels库。

相关问题