【问题标题】:How to print a simple list of feature importance in when using Logistic Regression?使用逻辑回归时如何打印特征重要性的简单列表?
【发布时间】:2021-12-22 14:12:36
【问题描述】:

我正在使用此处找到的数据集:https://www.kaggle.com/pavansubhasht/ibm-hr-analytics-attrition-dataset

我的代码是:

from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression

log_reg_model = LogisticRegression(max_iter=1000, solver = "newton-cg")
log_reg_model = RFE(log_reg_model, 45) # using RFE to get the top 45 most important features
log_reg_model.fit(X_train_SMOTE, y_train_SMOTE) # fitting data
y_pred = log_reg_model.predict(X_test)
print("Model accruracy score: {}".format(accuracy_score(y_test, y_pred)))
print(classification_report(y_test, y_pred))

我正在尝试按顺序打印出最重要的特征,就像在随机森林分类中使用 feature_importances_ 函数时一样。

以上使用 LR 是否可行?我在 Stack Overflow 上看到了类似的问题,但没有显示功能名称及其重要性的答案。

【问题讨论】:

    标签: python scikit-learn logistic-regression


    【解决方案1】:

    为此,您可以使用称为shapI definitely would recommend reading about SHAP before diving right into the code 的方法,因为这对于您和其他人准确了解您所呈现的内容非常重要。

    但是,如何在您的实施中发挥作用的一个示例是:

    from sklearn.feature_selection import RFE
    from sklearn.linear_model import LogisticRegression
    import shap
    
    log_reg_model = LogisticRegression(max_iter=1000, solver = "newton-cg")
    # log_reg_model = RFE(log_reg_model, 45) # using RFE to get the top 45 most important features
    log_reg_model.fit(X_train_SMOTE, y_train_SMOTE) # fitting data
    y_pred = log_reg_model.predict(X_test)
    print("Model accruracy score: {}".format(accuracy_score(y_test, y_pred)))
    print(classification_report(y_test, y_pred))
    
    explainer = shap.LinearExplainer(log_reg_model, X_train_SMOTE)
    shap_values = explainer.shap_values(X_test[:150])
    
    shap.summary_plot(shap_values, feature_names = X_train_SMOTE.columns)
    

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 2014-08-06
      • 2020-04-14
      • 2021-06-19
      • 2018-12-29
      • 2016-03-07
      • 2015-10-10
      • 1970-01-01
      • 2016-04-04
      相关资源
      最近更新 更多