开发者社区> 问答> 正文

如何将数据集分割/划分为训练和测试数据集,例如进行交叉验证?

将NumPy数组随机分为训练和测试/验证数据集的好方法是什么?与Matlab中的cvpartition或crossvalind函数类似。 问题来源于stack overflow

展开
收起
保持可爱mmm 2020-02-08 13:31:25 857 0
1 条回答
写回答
取消 提交回答
  • 如果要将数据集分成两半,可以使用numpy.random.shuffle,或者numpy.random.permutation需要跟踪索引:

    import numpy

    x is your dataset

    x = numpy.random.rand(100, 5) numpy.random.shuffle(x) training, test = x[:80,:], x[80:,:] 要么

    import numpy

    x is your dataset

    x = numpy.random.rand(100, 5) indices = numpy.random.permutation(x.shape[0]) training_idx, test_idx = indices[:80], indices[80:] training, test = x[training_idx,:], x[test_idx,:] 有多种方法可以重复分区同一数据集以进行交叉验证。一种策略是从数据集中重复采样:

    import numpy

    x is your dataset

    x = numpy.random.rand(100, 5) training_idx = numpy.random.randint(x.shape[0], size=80) test_idx = numpy.random.randint(x.shape[0], size=20) training, test = x[training_idx,:], x[test_idx,:] 最后,sklearn包含几种交叉验证方法(k折,nave -n-out等)。它还包括更高级的“分层抽样”方法,该方法可创建相对于某些功能平衡的数据分区,例如,确保训练和测试集中的正例和负例比例相同。

    2020-02-08 13:31:39
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载
F2etest — 多浏览器兼容性测试整体解决方案 立即下载