什么是将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折,离开正出,...)。它还包含更高级的"stratified sampling"方法,这些方法可以创建与某些功能相平衡的数据分区,例如确保训练和测试集中正负示例的比例相同。
我写了一个函数为我自己的项目要做到这一点(它不使用numpy的,虽然):
def partition(seq, chunks):
"""Splits the sequence into equal sized chunks and them as a list"""
result = []
for i in range(chunks):
chunk = []
for element in seq[i:len(seq):chunks]:
chunk.append(element)
result.append(chunk)
return result
如果您希望块被随机分组,通过它在之前刚刚改组列表
还有一个选择,只需要使用scikit学习。作为scikit's wiki describes,你可以使用如下指令:
from sklearn.model_selection import train_test_split
data, labels = np.arange(10).reshape((5, 2)), range(5)
data_train, data_test, labels_train, labels_test = train_test_split(data, labels, test_size=0.20, random_state=42)
这样就可以在保持同步的数据你想分割为训练和测试的标签。
你也可以考虑分层划分为训练和测试集。启动分区也会随机生成训练和测试集,但保留原始分类比例。这使得训练和测试集更好地反映了原始数据集的属性。
import numpy as np
def get_train_test_inds(y,train_proportion=0.7):
'''Generates indices, making random stratified split into training set and testing sets
with proportions train_proportion and (1-train_proportion) of initial sample.
y is any iterable indicating classes of each observation in the sample.
Initial proportions of classes inside training and
testing sets are preserved (stratified sampling).
'''
y=np.array(y)
train_inds = np.zeros(len(y),dtype=bool)
test_inds = np.zeros(len(y),dtype=bool)
values = np.unique(y)
for value in values:
value_inds = np.nonzero(y==value)[0]
np.random.shuffle(value_inds)
n = int(train_proportion*len(value_inds))
train_inds[value_inds[:n]]=True
test_inds[value_inds[n:]]=True
return train_inds,test_inds
y = np.array([1,1,2,2,3,3])
train_inds,test_inds = get_train_test_inds(y,train_proportion=0.5)
print y[train_inds]
print y[test_inds]
此代码输出:
[1 2 3]
[1 2 3]
谢谢!命名有些误导,'value_inds'是真正的索引,但输出不是索引,只是掩码。 – greenoldman 2017-09-02 12:53:50
刚一说明。如果您想训练,测试和验证集,你可以这样做:
from sklearn.cross_validation import train_test_split
X = get_my_X()
y = get_my_y()
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
x_test, x_val, y_test, y_val = train_test_split(x_test, y_test, test_size=0.5)
这些参数将给予70%的培训,以及各15%,测试和Val套。希望这可以帮助。
下面是在分层方式
% X = data array
% y = Class_label
from sklearn.cross_validation import StratifiedKFold
skf = StratifiedKFold(y, n_folds=5)
for train_index, test_index in skf:
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
由于pberkes将数据分割成n = 5倍的回答代码。我只是修改,以避免(1)更换,一边品尝(2)复制实例发生在训练和测试:
training_idx = np.random.choice(X.shape[0], int(np.round(X.shape[0] * 0.8)),replace=False)
training_idx = np.random.permutation(np.arange(X.shape[0]))[:np.round(X.shape[0] * 0.8)]
test_idx = np.setdiff1d(np.arange(0,X.shape[0]), training_idx)
由于sklearn.cross_validation
模块被弃用,你可以使用:
import numpy as np
from sklearn.model_selection import train_test_split
X, y = np.arange(10).reshape((5, 2)), range(5)
X_trn, X_tst, y_trn, y_tst = train_test_split(X, y, test_size=0.2, random_state=42)
- 1. SSAS - 分类 - 如何将数据分割为:训练集 - 验证集 - 测试集
- 2. 如何分割MoviesLens数据集进行交叉验证?
- 3. 如何将文档分割为训练集和测试集?
- 4. 如何将数据分割为R中的训练和验证?
- 5. 将数据分割为训练和测试
- 6. 在R中将数据集分割为训练,交叉验证和测试数据集ifelse返回意外结果
- 7. 在MATLAB中将数据拆分为训练/测试数据集?
- 8. R:如何将数据帧分成训练,验证和测试集?
- 9. 在Postgres中将数据集分成训练和测试集
- 10. 在Matlab中训练数据集,验证数据集,测试数据集
- 11. 随机分割训练和测试数据
- 12. 将分区数据集拆分为训练和测试(训练数据每个类有200个示例)
- 13. 如何在MATLAB中训练大数据集以进行分类
- 14. 根据python中的训练和测试集中的时间戳分割每个用户的数据集
- 15. 如何在Apache Spark中训练Matrix分解模型MLlib的ALS使用训练,测试和验证数据集
- 16. 将数据分区创建为训练,测试和验证 - 拆分R
- 17. 我应该如何将大的(〜50Gb)数据集分成训练,测试和验证集?
- 18. 正确分割数据集
- 19. 如何集拆分到训练集和验证集
- 20. 星火交叉验证与培训,测试和验证集
- 21. 分割训练数据以训练n个模型的最佳数量
- 22. 将不同类的图像分割成Python中的训练和测试集
- 23. 训练数据集
- 24. 基于训练集的数据分类
- 25. Python:如何将数据采样到测试和训练数据集?
- 26. Pyspark - 从数据框创建训练集和测试集
- 27. 如何将图像数据集分为火车和测试集?
- 28. Shoul我用k-fold-cross验证将我的数据分成训练/测试/验证集?
- 29. 培训验证面部表情数据集的测试集拆分
- 30. Lua/Torch:将MNIST数据集拆分为训练和测试,每个集合使用相同数量的标签
感谢这些解决方案。但是,使用randint的最后一种方法是否可以为测试和训练集提供相同的索引? – ggauravr 2013-11-05 22:21:28