0

As an example, I use a small data frame and calculate logistic regression using either statsmodels or scikit-learn:

import pandas as pd
 
x1 = [148,85,182,49]
x2 = [50,31,32,21]
x3 = [72,66,64,66]
y = [1,0,1,1]

df = pd.DataFrame({"x1":x1,"x2":x2,"x3":x3,"y":y})

import statsmodels.api as sm

lr = sm.Logit(endog = y, exog = X)
res = lr.fit()
res.summary() 

The output is very well organized into a data frame form: enter image description here

Is there a way to show outputs from scikit-learn in a similar form?

Actually, the only possibility I found is to use print, which gives a "no-decent" presentation:

from sklearn import linear_model
from sklearn.linear_model import LogisticRegression

data = df.to_numpy()
X = data[: , 0:3].astype('float')
y = data[: , 3].astype('int')
lr = LogisticRegression(solver = "lbfgs")
model = lr.fit(X, y)
print(f"intercept: {model.intercept_} coeficients: {model.coef_}")


intercept: [-45.25601233] coeficients: [[ 0.06092424 -0.3317025   0.75962957]]
Andrew
  • 926
  • 2
  • 17
  • 24
  • 4
    This question is already answered here: https://stackoverflow.com/q/26319259/4574633 – Antoine Dubuis Jun 07 '21 at 09:12
  • 2
    Does this answer your question? [How to get a regression summary in Python scikit like R does?](https://stackoverflow.com/questions/26319259/how-to-get-a-regression-summary-in-python-scikit-like-r-does) – afsharov Jun 07 '21 at 11:39
  • @afsharov thing is, linked question is from 2014 (we sure there is nothing added in scikit-learn since then?), and 3 of the answers there suggest to use statsmodels instead (which OP has done already). – desertnaut Jun 07 '21 at 12:03
  • 1
    @desertnaut I also quickly checked but with a negative result. The accepted answer also suggests a reason why, as `scikit-learn` "is ... for predictive modeling". So the hopes are low that there is such fuctionality now. We can of course still gather all available parameters of the fitted estimator and put them in e.g. a pandas Dataframe to print "in a pleasant way". But as of now, there is no pre-implemented in `scikit-learn` method to get such summaries, as far as I know. – afsharov Jun 07 '21 at 13:10
  • @afsharov cool, I am convinced - closing as duplicate. – desertnaut Jun 07 '21 at 13:31
  • OK. Say there is no way in scikit-learn (pity!). The link indicated by Antoine is nevertheless "reasonably" useful. I will try to manipulate the indications given (the list of `print(...)` ) and may derive something suitable from this. Thank you for your research. – Andrew Jun 07 '21 at 14:42

0 Answers0