2017-02-26 38 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,逆向排序的項目,選擇所需的頂級功能的數量然後轉換回字典。

我希望這可以幫助任何其他人試圖從他們的重要性開始形成頂級功能,而不是繪製所有的唯一一個證書編號功能相同的問題。