0

所以我有一些 8 列和多行的数据,我想用 5 个拆分执行 K-Fold 拆分。我已经做到了,但是我现在要做的是对于每个拆分,打印出它所在的拆分编号。请参阅代码以获得更好的解释。

kf = KFold(n_splits=5) #Define the split - into 5 folds 

#Define empty arrays for each technique
kf_train = []
kf_test = []

#Iterate through each feature in 
for kf_train, kf_test in kf.split(df):
    print('Split # ????')
    for col_name, col_data in df.iteritems():
        print('Feature: ', col_name)
        print('Mean: ', np.mean(col_data))
        print('Standard Deviation: ', np.std(col_data))
        print('\n')

所以它说print('Split # ????')的就是我遇到问题的地方。为了获得以下输出,我应该写什么:

Split #1
Feature: XXX
Mean: 3.3
Std: 3.3

Split #2
etc..
4

2 回答 2

1

您可以使用enumerate它为您提供索引加上值

# Iterate through each feature in 
for idx, kf_vals in enumerate(kf.split(df)):
    print('Split #%s' % idx)
    kf_train, kf_test = kf_vals
    for col_name, col_data in df.iteritems():
        print('Feature: ', col_name)
        print('Mean: ', np.mean(col_data))
        print('Standard Deviation: ', np.std(col_data))
        print('\n')
于 2020-03-19T11:32:05.740 回答
1

添加一个enumerate应该可以解决您的问题:

for i, (kf_train, kf_test) in enumerate(kf.split(df)):
    print('Split #{}'.format(i))
    for col_name, col_data in df.iteritems():
        print('Feature: ', col_name)
        print('Mean: ', np.mean(col_data))
        print('Standard Deviation: ', np.std(col_data))
        print('\n')

供参考:文档

于 2020-03-19T11:36:00.947 回答