【问题标题】:XGBoost plot importance has no property max_num_featuresXGBoost 绘图重要性没有属性 max_num_features
【发布时间】:2017-07-16 18:03:57
【问题描述】:

xgboost 的 plotting API 声明:

xgboost.plot_importance(booster, ax=None, height=0.2, xlim=None, ylim=None, title='Feature importance', xlabel='F score', ylabel='Features', importance_type='weight', max_num_features=None, grid=True, **kwargs)¶

根据拟合树绘制重要性。

参数:

booster (Booster, XGBModel or dict) – Booster or XGBModel instance, or dict taken by Booster.get_fscore()
...
max_num_features (int, default None) – Maximum number of top features displayed on plot. If None, all features will be displayed.

然而,在我的实现中,运行:

booster_ = XGBClassifier(learning_rate=0.1, max_depth=3, n_estimators=100, 
                      silent=False, objective='binary:logistic', nthread=-1, 
                      gamma=0, min_child_weight=1, max_delta_step=0, subsample=1, 
                      colsample_bytree=1, colsample_bylevel=1, reg_alpha=0,
                      reg_lambda=1, scale_pos_weight=1, base_score=0.5, seed=0)

booster_.fit(X_train, y_train)

from xgboost import plot_importance
plot_importance(booster_, max_num_features=10)

返回:

AttributeError: Unknown property max_num_features

在没有参数 max_num_features 的情况下运行它时,会正确绘制整个功能集(在我的情况下是巨大的,大约 10k 个功能)。 有什么想法吗?

提前致谢。

详情:

> python -V
  Python 2.7.12 :: Anaconda custom (x86_64)

> pip freeze | grep xgboost
  xgboost==0.4a30

【问题讨论】:

    标签: python feature-selection xgboost


    【解决方案1】:

    尝试将您的 xgboost 库升级到 0.6。它应该可以解决问题。 要升级软件包,请尝试以下操作:

    $ pip install -U xgboost
    

    如果出现错误,请尝试以下操作:

    $ brew install gcc@5
    $ pip install -U xgboost
    

    (参考这个https://github.com/dmlc/xgboost/issues/1501)

    【讨论】:

    • 是的! XGboost 没有最好的文档,但在弄清楚它是有效的之后。我会接受你的回答,因为它现在更相关(有点忘了问过这个问题)。
    【解决方案2】:

    在另行通知之前,我已经用这个脚本(至少部分解决了):

    def feat_imp(df, model, n_features):
    
        d = dict(zip(df.columns, model.feature_importances_))
        ss = sorted(d, key=d.get, reverse=True)
        top_names = ss[0:n_features]
    
        plt.figure(figsize=(15,15))
        plt.title("Feature importances")
        plt.bar(range(n_features), [d[i] for i in top_names], color="r", align="center")
        plt.xlim(-1, n_features)
        plt.xticks(range(n_features), top_names, rotation='vertical')
    
     feat_imp(filled_train_full, booster_, 20)
    

    【讨论】:

    • 使用 XGBRegressor,我得到 feature_importances_ not found 错误。
    • @xgdgsc 您可能需要更新 xgboost。 feature_importances_ 显然是他们最新 API 的一部分。有关更多信息,请参阅此帖子:stackoverflow.com/questions/38212649/…
    【解决方案3】:

    尽管文档标题为 webpage(“Python API 参考 - xgboost 0.6 文档”),但它不包含 xgboost 的 0.6 版本的文档。相反,它似乎包含最新的 git master 分支的文档。

    xgboost 的 0.6 版本是在 Jul 29 2016 上发布的:

    This is a stable release of 0.6 version
    
    @tqchen tqchen released this on Jul 29 2016 · 245 commits to master since this release
    

    添加plot_importance()的max_num_features的提交是在Jan 16 2017上进行的:

    作为进一步的检查,让我们检查一下 0.60 版本的压缩包:

    pushd /tmp
    curl -SLO https://github.com/dmlc/xgboost/archive/v0.60.tar.gz
    tar -xf v0.60.tar.gz 
    grep num_features xgboost-0.60/python-package/xgboost/plotting.py
    # .. silence.
    

    因此,这似乎是 xgboost 项目的文档错误。

    【讨论】:

      【解决方案4】:

      只是在这里添加一些东西。我仍然有这个错误,我相信其他人也有。因此,在此问题得到解决之前,这里还有另一种实现相同目标的方法:

      max = 50
      xgboost.plot_importance(dict(sorted(bst.get_fscore().items(), reverse = True, key=lambda x:x[1])[:max]), ax = ax, height = 0.8)
      

      由于您也可以将 dict 传递给绘图,因此您基本上可以获得 fscore,以相反的顺序对项目进行排序,选择所需数量的顶级特征然后转换回 dict。

      我希望这可以帮助其他有相同问题的人尝试仅绘制一个特定数字的特征,从顶部特征开始,而不是全部绘制。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-26
        • 2019-01-25
        • 2023-03-22
        • 1970-01-01
        • 2017-02-21
        • 2019-06-15
        • 2016-07-04
        • 2017-01-15
        相关资源
        最近更新 更多