XGBclassifier
在管道中使用 xgboost(或)时如何确定特征重要性XGBregressor
?
AttributeError: 'Pipeline' object has no attribute 'get_fscore'
这里提供的答案是相似的,但我不明白。
XGBclassifier
在管道中使用 xgboost(或)时如何确定特征重要性XGBregressor
?
AttributeError: 'Pipeline' object has no attribute 'get_fscore'
这里提供的答案是相似的,但我不明白。
正如我发现的,有两种方法可以确定特征重要性:首先:
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())
您应该首先从管道中获取XGBClassifier
or元素。XGBRegressor
您可以通过获取第 n 个元素或指定名称来执行此操作。
clf = XGBClassifier()
pipe = Pipeline([('other', other_element), ('xgboost', clf)])
要获得XGBClassifier
您可以:
clf
如果您仍然有参考,请使用pipe.named_steps['xgboost']
pipe.steps[1]
其次,对于 xgboost 的 sklearn 实现,似乎没有实现重要性。请参阅此 github 问题。将其添加到您的XGBClassifier
或XGBRegressor
还提供了一个解决方案。归结为自己将方法添加到类中。