sklearn 和 statsmodels 的逻辑回归结果不匹配
Logistic regression results of sklearn and statsmodels don't match
我尝试使用 sklearn 和 statsmodels 库进行逻辑回归。他们的结果很接近,但不一样。比如sklearn得到的(slope, intercept)对是(-0.84371207, 1.43255005),而statsmodels得到的对是(-0.8501, 1.4468)。为什么以及如何使它们相同?
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 默认使用 L2 正则化,而 statsmodels 不使用。尝试在 sklearn 模型参数中指定 penalty= 'none'
并重新运行。
有关 sklearn 中逻辑回归的更多信息,请参阅文档:
https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html.
我尝试使用 sklearn 和 statsmodels 库进行逻辑回归。他们的结果很接近,但不一样。比如sklearn得到的(slope, intercept)对是(-0.84371207, 1.43255005),而statsmodels得到的对是(-0.8501, 1.4468)。为什么以及如何使它们相同?
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 默认使用 L2 正则化,而 statsmodels 不使用。尝试在 sklearn 模型参数中指定 penalty= 'none'
并重新运行。
有关 sklearn 中逻辑回归的更多信息,请参阅文档: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html.