4
$\begingroup$

I want to know feature names that a LogisticRegression() Model has used along with their corresponding weights in scikit-learn. I can access to weights using coef_, but i did not know how can pair them with their corresponding weights.

$\endgroup$
2
  • 3
    $\begingroup$ I think the model just returns the coef_ in the same order as your input features, so just print them out one by one $\endgroup$
    – Yang Song
    Mar 15, 2018 at 21:16
  • $\begingroup$ It's in the order of the columns by default... Also to get feature Importance from LR, take the absolute value of coefficients and apply a softmax on the same(be careful, some silver already do so in-built) $\endgroup$
    – Aditya
    Mar 16, 2018 at 0:18

2 Answers 2

1
$\begingroup$

We if you're using sklearn's LogisticRegression, then it's the same order as the column names appear in the training data. see below code.

#Train with Logistic regression
from sklearn.linear_model import LogisticRegression
from sklearn import metrics

model = LogisticRegression()
model.fit(X_train,Y_train)
#Print model parameters - the names and coefficients are in same order
print(model.coef_)
print(X_train.columns)

You may also verify using another library as below

import statsmodels.api as sm
logit_model=sm.Logit(Y_train,X_train)
result=logit_model.fit()
print(result.summary2())
$\endgroup$
0
$\begingroup$

I made a scenario:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model.logistic import LogisticRegression
from sklearn.metrics import accuracy_score

max_features = 100
tfidf = TfidfVectorizer(max_features=max_features)#stop_words='english',)# norm = None)#)

#Simple
texts_train = ['positive sample', 'again positive', 'negative sample', 'again negative']
target_train = [1,1,0,0]
texts_test = ['negative', 'positive']
target_test = [0,1]
texts_train1 = tfidf.fit_transform(texts_train)
texts_test1 = tfidf.transform(texts_test)
classifier = LogisticRegression()
classifier.fit(texts_train1, target_train)
predictions = classifier.predict(texts_test1)

print('accuracy (simple):', accuracy_score(target_test, predictions))
tfidf.get_feature_names()

['again', 'negative', 'positive', 'sample']

classifier.coef_

array([[ 0. , -0.56718183, 0.56718183, 0. ]]) that makes sense!

$\endgroup$
2
  • $\begingroup$ @ keramat - does this means coefficients corresponds to the features in alphabetically sorted in ascending order? $\endgroup$
    – KGhatak
    Dec 8, 2019 at 12:46
  • $\begingroup$ Never mind, found the answer (same as the comments to the original post) $\endgroup$
    – KGhatak
    Dec 8, 2019 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.