Python Sklearn Logistic回归模型错误拟合

Python Sklearn Logistic回归模型错误拟合,第1张

概述对于逻辑回归,我试图从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, 1.75, 2, 2.25, 2.5, 2.7 对于逻辑回归,我试图从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,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.0206Hours        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]
总结

以上是内存溢出为你收集整理的Python Sklearn Logistic回归模型错误拟合全部内容,希望文章能够帮你解决Python Sklearn Logistic回归模型错误拟合所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1193960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存