簡體   English   中英

使用 gridsearchcv 進行邏輯回歸的特征重要性

[英]Feature importance using gridsearchcv for logistic regression

我已經像這樣訓練了邏輯回歸 model:

reg = LogisticRegression(random_state = 40)
cvreg = GridSearchCV(reg, param_grid={'C':[0.05,0.1,0.5],
                                      'penalty':['none','l1','l2'],
                                      'solver':['saga']},
                     cv = 5)
cvreg.fit(X_train, y_train)

現在為了顯示該功能的重要性,我嘗試了這段代碼,但我沒有得到 plot 中的系數名稱:

from matplotlib import pyplot

importance = cvreg.best_estimator_.coef_[0]
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()

在此處輸入圖像描述

顯然,plot 提供的信息不多。 如何將系數的名稱添加到 x 軸?

coeff 的重要性在於:

cvreg.best_estimator_.coef_
array([[1.10303023e+00, 7.48816905e-01, 4.27705027e-04, 6.01404570e-01]])

系數對應於X_train的列,因此傳入X_train名稱而不是range(len(importance))

假設X_train是 pandas dataframe:

import matplotlib.pyplot as plt

features = X_train.columns
importance = cvreg.best_estimator_.coef_[0]

plt.bar(features, importance)
plt.show()

請注意,如果X_train只是一個沒有列名的 numpy 數組,您將必須根據自己的數據字典定義features列表。

示例輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM