运行 LGBM 分类器模型,我可以使用 lgbm.plot_importance 来绘制最重要的特征,但我更喜欢有这些特征的列表,有人知道如何去做吗?
最佳答案
lightgbm.Booster
对象有一个方法.feature_importance()
,可用于访问特征重要性。
该方法返回一个数组,其中每个功能都有一个重要性值,并根据 importance_type
的值支持两种类型的重要性:
"gain"
= "使用此功能的所有分割的累积增益""split"
= "使用此功能的拆分次数"
您可以使用以下代码来探索这一点。我使用 lightgbm==3.3.0
、numpy==1.21.0
、pandas==1.2.3
和 运行此命令scikit-learn==0.24.1
,使用Python 3.8。
import lightgbm as lgb
import pandas as pd
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
data = lgb.Dataset(X, label=y)
# train model
bst = lgb.train(
params={"objective": "binary"},
train_set=data,
num_boost_round=10
)
# compute importances
importance_df = (
pd.DataFrame({
'feature_name': bst.feature_name(),
'importance_gain': bst.feature_importance(importance_type='gain'),
'importance_split': bst.feature_importance(importance_type='split'),
})
.sort_values('importance_gain', ascending=False)
.reset_index(drop=True)
)
print(importance_df)
这是输出的示例。
feature_name importance_gain importance_split
0 Column_22 1051.204456 8
1 Column_23 862.363854 10
2 Column_27 262.272097 19
3 Column_7 161.842017 13
4 Column_21 66.431762 24
这意味着,例如,功能 Column_21
比其他顶级功能在更多拆分中使用,但这些拆分提供的改进比使用 Column_22< 的 8 次拆分的影响要小得多
.
关于python - 有没有办法打印 Light GBM 分类器模型最重要特征的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69441258/