0
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn import metrics
from sklearn.model_selection import GridSearchCV

xgb_train = xgb.DMatrix(X_train, label=y_train)

xgb_model = XGBClassifier(objective='multi:softmax', n_estimators=100, learning_rate=0.3, max_depth=4, subsample=0.8, n_iter_no_change=2, verbosity=1)
xgb_param = xgb_model.get_xgb_params()
xgb_param['num_class'] = 7
cvresult = xgb.cv(xgb_param, xgb_train, num_boost_round=xgb_model.get_params()['n_estimators'], nfold=5, early_stopping_rounds=10, verbose_eval=True)
xgb.set_params(n_estimators=cvresult.shape[0])
predictions, accuracy, metrics_report = train_test_model(xgb_model, X_train, X_test, y_train, y_test)
print('accuracy: {}'.format(accuracy))
print(metrics_report)
plot_confusion_matrix(xgb_model, X_test, y_test, display_labels=labels, xticks_rotation='vertical', cmap="BuPu")

错误如下

AttributeError Traceback(最近一次调用最后一次)在 10 xgb_param['num_class'] = 7 11 cvresult = xgb.cv(xgb_param, xgb_train, num_boost_round=xgb_model.get_params()['n_estimators'], nfold=5, early_stopping_rounds=10, verbose_eval=True) ---> 12 xgb.set_params(n_estimators=cvresult.shape[0]) 13 预测,准确度,metrics_report = train_test_model(xgb_model, X_train, X_test, y_train, y_test) 14 print('accuracy: {}' .格式(准确性))

AttributeError:模块“xgboost”没有属性“set_params”

4

0 回答 0