在管道中使用 xgboost 时如何确定特征重要性?

数据挖掘 Python scikit-学习 xgboost
2021-10-09 18:46:33

XGBclassifier在管道中使用 xgboost(或)时如何确定特征重要性XGBregressor

AttributeError: 'Pipeline' object has no attribute 'get_fscore'

这里提供的答案是相似的,但我不明白。

2个回答

正如我发现的,有两种方法可以确定特征重要性:首先:

print(grid_search.best_estimator_.named_steps["clf"].feature_importances_)

结果:

[ 0.14582562  0.08367272  0.06409663  0.07631433  0.08705109  0.03827286
  0.0592836   0.05025916  0.07076083  0.0699278   0.04993521  0.07756387
  0.05095335  0.07608293]

第二:

print(grid_search.best_estimator_.named_steps["clf"].booster().get_fscore())

结果:

{'f2': 1385, 'f11': 1676, 'f12': 1101, 'f6': 1281, 'f9': 1511, 'f7': 1086, 'f5': 827, 'f0': 3151, 'f10': 1079, 'f1': 1808, 'f3': 1649, 'f13': 1644, 'f8': 1529, 'f4': 1881}

第三:

print(grid_search.best_estimator_.named_steps["clf"].get_booster().get_fscore())

获取对 xgboost 对象的引用

您应该首先从管道中获取XGBClassifieror元素。XGBRegressor您可以通过获取第 n 个元素或指定名称来执行此操作。

clf = XGBClassifier()
pipe = Pipeline([('other', other_element), ('xgboost', clf)])

要获得XGBClassifier您可以:

  • clf如果您仍然有参考,请使用
  • 按名称索引管道: pipe.named_steps['xgboost']
  • 按位置索引管道: pipe.steps[1]

了解重要性

其次,对于 xgboost 的 sklearn 实现,似乎没有实现重要性。请参阅此 github 问题将其添加到您的XGBClassifierXGBRegressor还提供了一个解决方案。归结为自己将方法添加到类中。