加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python Sklearn Logistic回归模型错误拟合

发布时间:2020-12-20 12:01:12 所属栏目:Python 来源:网络整理
导读:对于逻辑回归,我试图从Wikipedia logistic regression页面重现结果.所以,我的代码如下所示: import numpy as npfrom sklearn.linear_model import LogisticRegressionx = np.array([0.5,0.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,4,4.25,4.5,4.75,5
对于逻辑回归,我试图从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,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,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]

这显然是不同的.

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

解决方法

维基百科示例不包括模型参数的正则化,但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[:,y)

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

# [[ 1.50459727]]
# [-4.07757136]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读