简体   繁体   中英

How to print the column names along with the column co-efficients for Logistic Regression in Python?

logreg = LogisticRegression()

logreg.fit(X_train,y_train)

print(logreg.coef_ )
print(X.columns)

How can I zip the logreg.coef_ long with their column names? Thank you :)

Here is one way to do it:

# Create lists
column_labels = X.columns.tolist()
coef = logreg.coef_.squeeze().tolist()

# Zip together
labels_coef = list(zip(column_labels, coef))

# Verify the result
print(labels_coef)

Coefficients and features in zip

print(list(zip(X_train.columns.tolist(),logreg.coef_[0])))

Coefficients and features in DataFrame

pd.DataFrame({"Feature":X_train.columns.tolist(),"Coefficients":logreg.coef_[0]})

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM