将NumPy数组随机分成训练和测试/验证数据集的好方法是什么?类似于Matlab中的cvpartition或crossvalind函数. 如果要将数据集分成两半,则可以使用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,:]
repeatedly partition the same data set for cross validation有很多种方法.一种策略是从数据集重新采样,重复:
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包含several cross validation methods(k-fold,leave-n-out,…).它还包括更先进的“stratified sampling”方法,这些方法创建了与某些功能相关的数据分区,例如,以确保在训练和测试集中有相同比例的正面和负面示例.