# feature names in LogisticRegression()

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.

• I think the model just returns the coef_ in the same order as your input features, so just print them out one by one Mar 15 '18 at 21:16
• 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) Mar 16 '18 at 0:18

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())


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!

• @ keramat - does this means coefficients corresponds to the features in alphabetically sorted in ascending order? Dec 8 '19 at 12:46
• Never mind, found the answer (same as the comments to the original post) Dec 8 '19 at 13:27