machine-learning - 如何在 sklearn::LGBMClassifier() 中的 LightGBM 分类器的 feature_importances_ 中将 'gain' 设置为特征重要性度量

标签 machine-learning scikit-learn feature-selection gbm

我正在 LightGBM 中使用 LGBMClassifer 构建二元分类器模型,如下所示:

 # LightGBM model
        clf = LGBMClassifier(
            nthread=4,
            n_estimators=10000,
            learning_rate=0.005,
            num_leaves= 45,
            colsample_bytree= 0.8,
            subsample= 0.4,
            subsample_freq=1,
            max_depth= 20,
            reg_alpha= 0.5,
            reg_lambda=0.5,
            min_split_gain=0.04,
            min_child_weight=.05
            random_state=0,
            silent=-1,
            verbose=-1)

下一步,根据训练数据拟合我的模型

     clf.fit(train_x, train_y, eval_set=[(train_x, train_y), (valid_x, valid_y)], 
                eval_metric= 'auc', verbose= 100, early_stopping_rounds= 200)

    fold_importance_df = pd.DataFrame()
    fold_importance_df["feature"] = feats   
    fold_importance_df["importance"] = clf.feature_importances_

输出:

feature                      importance
feature13                     1108
feature21                     1104
feature11                     774
  • 到这里一切都很好,现在我正在研究基于此模型的特征重要性度量。因此,我使用 feature_importance_() 函数来获取该值(但默认情况下,它根据 split 提供功能重要性)

  • 虽然 split 让我了解哪个功能在 split 中使用了多少次,但我认为 gain 会让我更好地理解功能的重要性.

  • LightGBM 增强类的 Python API https://lightgbm.readthedocs.io/en/latest/Python-API.html?highlight=importance提及:

     feature_importance(importance_type='split', iteration=-1)
    
    
     Parameters:importance_type (string, optional (default="split")) – 
     If “split”, result contains numbers 
     of times the feature is used in a model. If “gain”, result contains 
     total gains of splits which use the feature.
     Returns:   result – Array with feature importances.
     Return type:   numpy array`
    

然而,LightGBM LGBMClassifier()Sklearn API 没有提及任何内容 Sklearn API LGBM ,这个函数只有这个参数:

feature_importances_
array of shape = [n_features] – The feature importances (the higher, the more important the feature).
  • 我的问题是如何从 sklearn 版本(即基于 gainLGBMClassifier())获取特征重要性?

最佳答案

feature_importance()是原始LGBM中Booster对象的一个​​方法。

sklearn API 通过 API Docs 中给出的属性 booster_ 公开训练数据上的底层 Booster。 。

因此,您可以首先访问此助推器对象,然后以与原始 LGBM 相同的方式调用 feature_importance()

clf.booster_.feature_importance(importance_type='gain')

关于machine-learning - 如何在 sklearn::LGBMClassifier() 中的 LightGBM 分类器的 feature_importances_ 中将 'gain' 设置为特征重要性度量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118772/

相关文章:

python - 如何处理sklearn中的score方法?

python - 使用 Python 的随机森林特征重要性图表

python - 如何将随机森林中选定的特征转换为新列表

python - 如何在numpy中计算超过num_classes - 1 LDA方向?

python - 通用句子编码器,降低向量维度

python - LabelEncoder vs. Pandas 分类 vs. 枚举?

python - 多重预测

python - Scikit-learn 的特征选择回归

java - 自然语言处理 - 文本分类的特征

html - 解析 html 文件时 Fonduer max_storage_temp_tutorial 错误