简体   繁体   English

sklearn 和 statsmodels 的逻辑回归结果不匹配

[英]Logistic regression results of sklearn and statsmodels don't match

I tried to do logistic regression using both sklearn and statsmodels libraries.我尝试使用 sklearn 和 statsmodels 库进行逻辑回归。 Their result is close, but not the same.他们的结果很接近,但又不一样。 For example, the (slope, intercept) pair obtained by sklearn is (-0.84371207, 1.43255005), while the pair obtained by statsmodels is (-0.8501, 1.4468).比如sklearn得到的(slope,intercept)pair是(-0.84371207, 1.43255005),而statsmodels得到的pair是(-0.8501, 1.4468)。 Why and how to make them same?为什么以及如何使它们相同?

import pandas as pd
import statsmodels.api as sm
from sklearn import linear_model

# Part I: sklearn logistic

url = "https://github.com/pcsanwald/kaggle-titanic/raw/master/train.csv"
titanic_train = pd.read_csv(url)

train_X = pd.DataFrame([titanic_train["pclass"]]).T
train_Y = titanic_train["survived"]

model_1 = linear_model.LogisticRegression(solver = 'lbfgs')
model_1.fit(train_X, train_Y)

print(model_1.coef_) # print slopes
print(model_1.intercept_ ) # print intercept

# Part II: statsmodels logistic

train_X['intercept'] = 1
model_2=sm.Logit(train_Y,train_X, method='lbfgs')
result=model_2.fit()
print(result.summary2())

Sklearn uses L2 regularisation by default and statsmodels does not. Sklearn 默认使用 L2 正则化,而 statsmodels 不使用。 Try specifying penalty= 'none' in the sklearn model parameters and rerun.尝试在 sklearn 模型参数中指定penalty= 'none'并重新运行。

See the documentation for more information on logistic regression in sklearn: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html .有关 sklearn 中逻辑回归的更多信息,请参阅文档: https ://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM