關於XGB.booster()報錯TypeError: 'str' object is not callable的解決方法
阿新 • • 發佈:2018-12-04
當使用XGB想得到特徵重要性時報錯,程式碼及報錯如下,
model = XGBRegressor( learning_rate = 0.1, n_estimators = 300, max_depth = 7, min_child_weight = 3, subsample = 0.8, colsample_bytree = 0.8, seed = 0 ) model.fit( X_train, y_train, eval_metric='mae', eval_set=[(X_train, y_train), (X_valid, y_valid)], early_stopping_rounds=20 ) y_pred = model.predict( X_valid ) feat_imp = pd.Series(model.booster().get_fscore()) feat_imp = feat_imp.sort_values( ascending=False ) feat_imp[:10].plot( kind='bar', title='Feature Importances' ) plt.ylabel( 'Feature Importance Score' ) plt.show()
遇到這個問題,開始我是佷懵的。
參考https://stackoverflow.com/questions/38212649/feature-importance-with-xgbclassifier上的內容後找到解決方法。
此時我們只需要將model.booster()改為model.get_booster()即可。