我对以下代码有问题
这是代码
# simulate splitting a dataset of 25 observations into 5 folds
from sklearn.model_selection import KFold
kf = KFold(n_splits=5, random_state=None, shuffle=False)
# print the contents of each training and testing set
print('{} {:^61} {}'.format('Iteration',
'Training set observations',
'Testing set observations'))
for iteration, data in enumerate(kf, start=1):
print('{:^9} {} {!s:^25}'.format(iteration, data[0], data[1]))
类型错误:"KFold"对象不可迭代
TypeError Traceback (most recent call last)
<ipython-input-21-13995db0f7c7> in <module>()
5 # print the contents of each training and testing set
6 print('{} {:^61} {}'.format('Iteration', 'Training set
observations', 'Testing set observations'))
----> 7 for iteration, data in enumerate(kf, start=1):
8 print('{:^9} {} {!s:^25}'.format(iteration, data[0], data[1]))
TypeError: 'KFold' object is not iterable
类 "cross_validation" 中有一个参数 "y"(要拆分为 K 折叠的样本):
类sklearn.cross_validation。StratifiedKFold(y, n_folds=3, shuffle=False, random_state=None)[source]
这个参数对我来说还不够model_selection
# simulate splitting a dataset of 25 observations into 5 folds
from sklearn.model_selection import KFold
kf = KFold(n_splits=5, random_state=None, shuffle=False)
Vec = np.arange(0,26)
# print the contents of each training and testing set
print('{} {:^61} {}'.format('Iteration',
'Training set observations',
'Testing set observations'))
for iteration, data in enumerate(kf.split(Vec), start=1):
print('{:^9} {} {!s:^25}'.format(iteration, data[0], data[1]))