python - 使用 train_test_split 的一个命令创建数据集的多分割

标签 python numpy machine-learning scikit-learn

  • 我的数据集有 42000
  • 我需要将数据集分为训练、交叉验证和测试 集,分割比例为60%、20% 和20%。这是根据 Andrew Ng 教授在他的 ml-class 讲座中的建议。
  • 我意识到 scikit-learn 有一个方法 train_test_split去做这个。但是我无法让它工作,所以我在一个线性命令中将拆分为 0.6, 0.2, 0.2

我做的是

# split data into training, cv and test sets
from sklearn import cross_validation
train, intermediate_set = cross_validation.train_test_split(input_set, train_size=0.6, test_size=0.4)
cv, test = cross_validation.train_test_split(intermediate_set, train_size=0.5, test_size=0.5)


# preparing the training dataset
print 'training shape(Tuple of array dimensions) = ', train.shape
print 'training dimension(Number of array dimensions) = ', train.ndim
print 'cv shape(Tuple of array dimensions) = ', cv.shape
print 'cv dimension(Number of array dimensions) = ', cv.ndim
print 'test shape(Tuple of array dimensions) = ', test.shape
print 'test dimension(Number of array dimensions) = ', test.ndim

然后得到结果

training shape(Tuple of array dimensions) =  (25200, 785)
training dimension(Number of array dimensions) =  2
cv shape(Tuple of array dimensions) =  (8400, 785)
cv dimension(Number of array dimensions) =  2
test shape(Tuple of array dimensions) =  (8400, 785)
test dimension(Number of array dimensions) =  2
features shape =  (25200, 784)
labels shape =  (25200,)

如何在一个命令中完成这项工作?

最佳答案

阅读train_test_split的源代码及其同伴类 ShuffleSplit并使其适应您的用例。这不是一个很大的功能,应该不会很复杂。

关于python - 使用 train_test_split 的一个命令创建数据集的多分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13346318/

相关文章:

python - 从 BeautifulSoup 结果中获取表单 "action"

python - 从一组 3D 点中采样 N 个点,使最小距离最大化

machine-learning - 数据去相关

python - LSTM 预测时形状错误

python - 在 self._compute_kernel(X) 中引发 ValueError ("X.shape[0] should be equal to X.shape[1]")

python - 如何在 Python 中连接 VPN/Proxy?

python - Trie 字典中的前缀搜索

python - 使用 Python 进行 2D 热传导

python - 离散优化 - 从分数矩阵的每一行和每一列中精确选择 N 个项目

python - 为抽样创建混合概率分布