【问题标题】:How to print out the index of the split?如何打印出拆分的索引?
【发布时间】:2020-03-19 11:28:49
【问题描述】:

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

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..

【问题讨论】:

    标签: python scikit-learn k-fold


    【解决方案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')
    

    供参考:docs

    【讨论】:

      【解决方案2】:

      您可以使用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')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-05
        • 1970-01-01
        • 2014-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-24
        相关资源
        最近更新 更多