我正在尝试使用从http://www.analyticsvidhya.com/blog/2016/03/complete-guide-parameter-tuning-xgboost-with-codes-python/改编的以下代码在XGBC分类器上执行多类分类问题的交叉验证
import numpy as npimport pandas as pdimport xgboost as xgbfrom xgboost.sklearn import XGBClassifIErfrom sklearn.preprocessing import LabelEncoderfrom sklearn import cross_valIDation,metricsfrom sklearn.grID_search import gridsearchcvdef modelFit(alg,X,y,useTrainCV=True,cvFolds=5,early_stopPing_rounds=50): if useTrainCV: xgbParams = alg.get_xgb_params() xgTrain = xgb.DMatrix(X,label=y) cvresult = xgb.cv(xgbParams,xgTrain,num_boost_round=alg.get_params()['n_estimators'],nfold=cvFolds,stratifIEd=True,metrics={'mlogloss'},early_stopPing_rounds=early_stopPing_rounds,seed=0,callbacks=[xgb.callback.print_evaluation(show_stdv=False),xgb.callback.early_stop(3)]) print cvresult alg.set_params(n_estimators=cvresult.shape[0]) # Fit the algorithm alg.fit(X,eval_metric='mlogloss') # Predict dtrainPredictions = alg.predict(X) dtrainPredProb = alg.predict_proba(X) # Print model report: print "\nModel Report" print "Classification report: \n" print(classification_report(y_val,y_val_pred)) print "Accuracy : %.4g" % metrics.accuracy_score(y,dtrainPredictions) print "Log Loss score (Train): %f" % metrics.log_loss(y,dtrainPredProb) feat_imp = pd.SerIEs(alg.booster().get_fscore()).sort_values(ascending=False) feat_imp.plot(kind='bar',Title='Feature importances') plt.ylabel('Feature importance score')# 1) Read training setprint('>> Read training set')train = pd.read_csv(trainfile)# 2) Extract target attribute and convert to numericprint('>> Preprocessing')y_train = train['OutcomeType'].valuesle_y = LabelEncoder()y_train = le_y.fit_transform(y_train)train.drop('OutcomeType',axis=1,inplace=True)# 4) Extract features and target from training setX_train = train.values# 5) First classifIErxgb = XGBClassifIEr(learning_rate =0.1,n_estimators=1000,max_depth=5,min_child_weight=1,gamma=0,subsample=0.8,colsample_bytree=0.8,scale_pos_weight=1,objective='multi:softprob',seed=27)modelFit(xgb,X_train,y_train)
其中y_train包含从0到4的标签.但是,当我运行此代码时,我从xgb.cv函数xgboost.core.XGBoostError得到以下错误:参数num_class的值0应该大于等于1.在XGBoost doc上我读了在多类情况下,xgb从目标向量中的标签中推断出类的数量,所以我不明白发生了什么.最佳答案您必须将参数’num_class’添加到xgb_param字典中.参数说明和上面提供的链接中的注释中也提到了这一点. 总结
以上是内存溢出为你收集整理的XGBClassifier上的交叉验证,用于python中的多类分类全部内容,希望文章能够帮你解决XGBClassifier上的交叉验证,用于python中的多类分类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)