赞
踩
- def modelfit(alg, dtrain, dtest, predictors,useTrainCV=True, cv_folds=3, early_stopping_rounds=50):
-
- if useTrainCV:
- xgb_param = alg.get_xgb_params()
- xgtrain = xgb.DMatrix(dtrain[predictors], label=dtrain[target])
- xgtest = xgb.DMatrix(dtest[predictors].values)
- cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
- metrics='auc', early_stopping_rounds=early_stopping_rounds)
- print (cvresult.shape[0])
- alg.set_params(n_estimators=cvresult.shape[0])
-
- #Fit the algorithm on the data
- alg.fit(dtrain[predictors], dtrain['Disbursed'],eval_metric='auc')
-
- #Predict training set:
- dtrain_predictions = alg.predict(dtrain[predictors])
- dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]
-
- #Print model report:
- print ("\nModel Report")
- print ("Accuracy : %.4g" % metrics.accuracy_score(dtrain['Disbursed'].values, dtrain_predictions))
- print ("AUC Score (Train): %f" % metrics.roc_auc_score(dtrain['Disbursed'], dtrain_predprob))
- feat_imp = pd.Series(alg.get_booster().get_fscore()).sort_values(ascending=False)#booster所有弱分类器、准确率+召回率+F值、排序
- feat_imp.plot(kind='bar', title='Feature Importances')
- plt.ylabel('Feature Importance Score')
解决方法:
将代码中的 alg.booster() 改成alg.get_booster() 即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。