from sklearn.model_selection import KFold
kf = KFold(len(y_train), 5, random_state=1) scores = [] for iteration, indices in enumerate(kf, start=1): X_train=X_train.reset_index(drop=True) y_train=y_train.reset_index(drop=True) lr=LR(random_state=1) lr.fit(X_train.iloc[indices[0],:],y_train.iloc[indices[0]].values.ravel()) y_pred = lr.predict_proba(X_train.iloc[indices[1],:].values)[:, 1] score = metrics.roc_auc_score(y_train.iloc[indices[1]].values, y_pred) scores.append(score) print('迭代次數 ', iteration,': 得分 = ', score) print('平均得分 ', np.mean(scores))
1、ModuleNotFoundError: No module named 'sklearn.cross_validation

sklearn已經將cross_validation合並到model_selection
from sklearn.model_selection import KFold
2、TypeError: shuffle must be True or False; got 5
添加shuffle=False,刪掉第一個參數位的值
kf=KFold(5,random_state=1,shuffle=False)
shuffle並不是必須的,可以刪掉
3、TypeError: 'KFold' object is not iterable
for iteration, indices in enumerate(kf.split(y_train), start=1):