2017-02-26 41 views
4

xgboost的plotting API状态:XGBoost情节重要性没有财产max_num_features

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 

回答

5

试图将xgboost库升级到0.6。它应该解决问题。 要升级包,试试这个:

$ pip install -U xgboost 

如果你得到一个错误,试试这个:

$ brew install [email protected] 
$ pip install -U xgboost 

(请参阅本https://github.com/dmlc/xgboost/issues/1501

+0

是的! XGboost没有最好的文档,但找出它的工作后。我会接受你的回答,因为它现在更加相关(有人问过这个问题)。 –

1

直至另行通知我此脚本解决了这个问题(至少部分地):

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) 

enter image description here

+0

用XGBRegressor,我得到'feature_importances_'找不到错误。 – xgdgsc

+0

@xgdgsc您可能需要更新xgboost。 feature_importances_显然是他们最新的API的一部分。看到这篇文章的更多信息:http://stackoverflow.com/questions/38212649/feature-importance-with-xgbclassifier –

2

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

的0.6版本的xgboost被做了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发行tar包:

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. 

因此,这似乎是一个文档bug与t他xgboost项目。

1

只是在这里添加。我仍然有这个错误,我相信其他人也有。因此,直到这个问题在这里解决的是另一种方式来实现同样的事情:

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

,你也可以通过一个字典的情节,你基本上得到fscore,逆向排序的项目,选择所需的顶级功能的数量然后转换回字典。

我希望这可以帮助任何其他人试图从他们的重要性开始形成顶级功能,而不是绘制所有的唯一一个证书编号功能相同的问题。