from sklearn.model_selection import KFold
X=train_data kf = KFold(n_splits=6)
modelsave=[] #用来保存模型
for train_index, test_index in kf.split(X): model=train_t(X.reindex(train_index).reset_index(drop=True),X.reindex(test_index).reset_index(drop=True)) modelsave.append(model) 进行预测 PREDS=[] for i in modelsave: preds = i.predict(xgb_test2, ntree_limit=i.best_ntree_limit) PREDS.append(preds) preds1=np.mean(PREDS,axis=0) #取平均