python:交叉验证中的报错
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):