sklearn.cross_validation与sklearn.model_selection

sklearn.cross_validation与sklearn.model_selection都可以做k折交叉验证,此处只是记录它们的用法,因为不同的python版本,可能含有的库不一样。
sklearn.cross_validation:
例子:
from sklearn.cross_validation import StratifiedKFold
for i, (tr, va) in enumerate(StratifiedKFold(y, n_folds=5, random_state=2018)):
print('stack:%d/%d' % ((i + 1), 5))
train_x,train_y=train.ix[tr],y[tr]
val_x,val_y=train.ix[va],y[va]

【sklearn.cross_validation与sklearn.model_selection】sklearn.model_selection:
例子:
from sklearn.model_selection import KFold,StratifiedKFold
skf=StratifiedKFold(n_splits=5,shuffle=True,random_state=42)
for i,(tr,va) in enumerate(skf.split(train,y)):
print('fold:',i+1,'training')
train_x,train_y=train.ix[tr],y[tr]
val_x,val_y=train.ix[va],y[va]
注:如果train是DataFrame类型,那么要有.ix或.loc,如果为数组类型,把.ix去掉即可!

    推荐阅读