我可以使用numpy数组生成折叠以进行交叉验证吗? [英] Can I use a numpy array to generate folds for cross validation?

查看:94
本文介绍了我可以使用numpy数组生成折叠以进行交叉验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用numpy数组为k倍交叉验证任务构建折叠.取出测试切片很容易,但是我无法弄清楚如何返回数组的其余部分,而忽略了测试切片.有没有一种有效的方法可以做到这一点?

I would like to use a numpy array to build folds for a k-folds cross validation task. Taking out the test slice is easy, but I can't figure out how to return the remainder of the array, with the test slice omitted. Is there an efficient way to do this?

examples = range(50)
classes = range(50)
data = np.array(zip(classes,examples))
test_slice = data[5:10]
train_on_remainder = ??

推荐答案

您可以这样设置:

test_slice, remainder = np.split(data.copy(), [test_size], axis=0)
# run test
remainder[:test_size], test_slice = test_slice, remainder[:test_size].copy()
# run test
remainder[test_size:2*test_size], test_slice = test_slice, remainder[test_size:2*test_size].copy()

# etc.

我认为您可以减少复制而获得它.

I don't think you can have it with much less copying.

工作原理:

.      full set:            | 0 | 1 | 2 | 3 | 4 | 5 |
       split (full copy)       / \
       tst / rem         | 0 |     | 1 | 2 | 3 | 4 | 5 |
         run trial
                             | 1 | 2 | 3 | 4 | 5 |
       swap tst and           ^ |
       first segment:         | v
       (partial copy)        | 0 |

       tst / rem         | 1 |     | 0 | 2 | 3 | 4 | 5 |
         run trial
                             | 0 | 2 | 3 | 4 | 5 |
       swap tst and               ^ |
       second segment:            | v
       (partial copy)            | 1 |

       tst / rem         | 2 |     | 0 | 1 | 3 | 4 | 5 |
         run trial
                             | 0 | 1 | 3 | 4 | 5 |
       swap tst and                   ^ |
       third segment:                 | v
       (partial copy)                | 2 |

等如您所见,它几乎在改变折痕.保存许多完整副本.

etc. As you can see it is almost literally shifting the fold. Saving many full copies.

这篇关于我可以使用numpy数组生成折叠以进行交叉验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆