0

I want to calculate the odds ratio between 2 features, but I got different results as follows:

import numpy as np
x= [1,2,3,2,1]
y= [1,0,1,0,1]
x1 = np.array(x).reshape(-1,1)
y1 = np.array(y).reshape(-1,1)

# method1
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(x1,y1)
np.exp(clf.coef_)

# method2 
import statsmodels.api as sm
res = sm.Logit(y1, x1).fit()
np.exp(res.params)

I got 0.79 for the first one and 1.11 for the second. I am not sure where I was wrong and really appreciate your help. Thanks.

jpf
  • 1,447
  • 12
  • 22
abc196998
  • 47
  • 5
  • 2
    This questions is repeated, please see: https://stackoverflow.com/questions/62005911/coefficients-for-logistic-regression-scikit-learn-vs-statsmodels – Rafael Valero Jan 30 '21 at 21:15
  • 1
    Indeed. Running `clf = LogisticRegression(fit_intercept=False, penalty='none')` will solve your issue. – amiola Jan 30 '21 at 21:20
  • 2
    Does this answer your question? [Coefficients for Logistic Regression scikit-learn vs statsmodels](https://stackoverflow.com/questions/62005911/coefficients-for-logistic-regression-scikit-learn-vs-statsmodels) – Rafael Valero Jan 30 '21 at 21:32

0 Answers0