Finding coefficients for logistic regression in python

牧云@^-^@ 提交于 2021-01-21 10:25:07

问题


I'm working on a classification problem and need the coefficients of the logistic regression equation. I can find the coefficients in R but I need to submit the project in python. I couldn't find the code for learning coefficients of logistic regression in python. How to get the coefficient values in python?


回答1:


sklearn.linear_model.LogisticRegression is for you. See this example:

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)
clf = LogisticRegression(random_state=0).fit(X, y)

print(clf.coef_, clf.intercept_)



回答2:


The statsmodels library would give you a breakdown of the coefficient results, as well as the associated p-values to determine their significance.

Using an example of x1 and y1 variables:

x1_train, x1_test, y1_train, y1_test = train_test_split(x1, y1, random_state=0)

logreg = LogisticRegression().fit(x1_train,y1_train)
logreg

print("Training set score: {:.3f}".format(logreg.score(x1_train,y1_train)))
print("Test set score: {:.3f}".format(logreg.score(x1_test,y1_test)))

import statsmodels.api as sm
logit_model=sm.Logit(y1,x1)
result=logit_model.fit()
print(result.summary())

Example results:

Optimization terminated successfully.
         Current function value: 0.596755
         Iterations 7
                           Logit Regression Results                           
==============================================================================
Dep. Variable:             IsCanceled   No. Observations:                20000
Model:                          Logit   Df Residuals:                    19996
Method:                           MLE   Df Model:                            3
Date:                Sat, 17 Aug 2019   Pseudo R-squ.:                  0.1391
Time:                        23:58:55   Log-Likelihood:                -11935.
converged:                       True   LL-Null:                       -13863.
                                        LLR p-value:                     0.000
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
const         -2.1417      0.050    -43.216      0.000      -2.239      -2.045
x1             0.0055      0.000     32.013      0.000       0.005       0.006
x2             0.0236      0.001     36.465      0.000       0.022       0.025
x3             2.1137      0.104     20.400      0.000       1.911       2.317
==============================================================================



回答3:


Have a look at the statsmodels library's Logit model.

You would use it like this:

from statsmodels.discrete.discrete_model import Logit
from statsmodels.tools import add_constant

x = [...] # Obesrvations
y = [...] # Response variable

x = add_constant(x)
print(Logit(y, x).fit().summary())



回答4:


Luffy, please remember to always share your code and your attempts so we can know what you tried and help you out. Regardless of that, I think you are looking for this:

import numpy as np
from sklearn.linear_model import LogisticRegression

X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) #Your x values, for a 2 variable model.
#y = 1 * x_0 + 2 * x_1 + 3 #This is the "true" model
y = np.dot(X, np.array([1, 2])) + 3 #Generating the true y-values
reg = LogisticRegression().fit(X, y) #Fitting the model given your X and y values.
reg.coef_ #Prints an array of all regressor values (b1 and b2, or as many bs as your model has)
reg.intercept_  #Prints value for intercept/b0 
reg.predict(np.array([[3, 5]])) #Predicts an array of y-values with the fitted model given the inputs



回答5:


Provided that your X is a Pandas DataFrame and clf is your Logistic Regression Model you can get the name of the feature as well as its value with this line of code:

pd.DataFrame(zip(X_train.columns, np.transpose(clf.coef_)), columns=['features', 'coef']) 



回答6:


a little correction last answer:

pd.DataFrame(zip(X_train.columns, np.transpose(clf.coef_.tolist()[0])), columns=['features', 'coef'])


来源:https://stackoverflow.com/questions/57924484/finding-coefficients-for-logistic-regression-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!