gpt4 book ai didi

feature-selection - XGBoost:xgb.importance 特征图

转载 作者:行者123 更新时间:2023-12-04 09:02:16 25 4
gpt4 key购买 nike

当我尝试使用以下代码时出现以下错误。

******代码******

    importance = bst.get_fscore(fmap='xgb.fmap')
importance = sorted(importance.items(), key=operator.itemgetter(1))

******错误******

  File "scripts/xgboost_bnp.py", line 225, in <module>
importance = bst.get_fscore(fmap='xgb.fmap')
File "/usr/lib/python2.7/site-packages/xgboost/core.py", line 754, in get_fscore
trees = self.get_dump(fmap)
File "/usr/lib/python2.7/site-packages/xgboost/core.py", line 740, in get_dump
ctypes.byref(sarr)))
File "/usr/lib/python2.7/site-packages/xgboost/core.py", line 92, in _check_call
raise XGBoostError(_LIB.XGBGetLastError())
xgboost.core.XGBoostError: can not open file "xgb.fmap"

最佳答案

出现此错误是因为您使用可选参数 fmap 调用 get_fscore,声明每个特征的特征重要性应从名为 的特征映射文件中获取xgb.fmap,它在你的文件系统中不存在。

这是一个返回排序后的特征名称及其重要性的函数:

import xgboost as xgb
import pandas as pd

def get_xgb_feat_importances(clf):

if isinstance(clf, xgb.XGBModel):
# clf has been created by calling
# xgb.XGBClassifier.fit() or xgb.XGBRegressor().fit()
fscore = clf.booster().get_fscore()
else:
# clf has been created by calling xgb.train.
# Thus, clf is an instance of xgb.Booster.
fscore = clf.get_fscore()

feat_importances = []
for ft, score in fscore.iteritems():
feat_importances.append({'Feature': ft, 'Importance': score})
feat_importances = pd.DataFrame(feat_importances)
feat_importances = feat_importances.sort_values(
by='Importance', ascending=False).reset_index(drop=True)
# Divide the importances by the sum of all importances
# to get relative importances. By using relative importances
# the sum of all importances will equal to 1, i.e.,
# np.sum(feat_importances['importance']) == 1
feat_importances['Importance'] /= feat_importances['Importance'].sum()
# Print the most important features and their importances
print feat_importances.head()
return feat_importances

关于feature-selection - XGBoost:xgb.importance 特征图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com