xgbClassifier特征图不能正常显示feature names

xgboost使用的时候经常想看一看特征的重要程度, 结果
feature1

from xgboost import plot_importance
from matplotlib import pyplot
import pickle

# load model from file
model = pickle.load(open("xgbmodel/xgb-model-0927", "rb"))
# plot feature importance
plot_importance(model)
pyplot.show()

改变了函数使用还是这样
feature2

import matplotlib.pyplot as plt
import pickle


# load model from file
loaded_model = pickle.load(open("xgbmodel/xgb-model-0927", "rb"))

feat_imp = pd.Series(loaded_model.get_booster().get_score(importance_type='weight')).sort_values(ascending=False)
feat_imp.plot(kind='bar', title='Feature Importances')
plt.ylabel('Feature Importance Score')
plt.show()

原因是: XGBClassifier本身就暂时没有这个办法

This is the expected behaviour- sklearn.OneHotEncoder.transform() returns a numpy 2d array instead of the input pd.DataFrame (i assume that’s the type of your dataset). So it is not a bug, but a feature. It doesn’t look like there is a way to pass feature names manually in the sklearn API (it is possible to set those in xgb.Dmatrix creation in the native training API).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据你提供的引用内容,你遇到的问题是 `'XGBClassifier' object has no attribute 'feature_importantces_'`。这个错误表示你在使用`XGBClassifier`对象时,调用了不存在的属性`feature_importances_`。可能的原因是你的`XGBClassifier`对象没有这个属性。 要解决这个问题,你可以检查以下几点: 1. 确保你导入了正确的库和模块。`XGBClassifier`是来自XGBoost库的分类器模型,你需要确保正确导入了XGBoost库。 2. 检查你的`XGBClassifier`对象是否正确初始化。确保你使用了正确的参数并正确地训练了模型。如果模型没有被正确训练,可能导致它不具备`feature_importances_`属性。 3. 确认你的XGBoost版本是否支持`feature_importances_`属性。根据引用中的链接,`feature_importances_`属性在sklearn版本0.24.x及更高版本中可用。如果你的XGBoost版本较早,你可以考虑升级到支持该属性的版本。 总结来说,要解决 `'XGBClassifier' object has no attribute 'feature_importances_'` 错误,你可以检查导入的库、正确初始化模型以及确认XGBoost版本是否支持该属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [TensorFlow2.0使用DenseFeature作为Functional API第一层时所遇到的问题](https://download.csdn.net/download/weixin_38563552/14035547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [AttributeError: ‘TfidfVectorizer‘ object has no attribute ‘get_feature_names_out](https://blog.csdn.net/weixin_46211269/article/details/128889789)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值