开发者社区> 问答> 正文

从MNIST Dataset更改列车和测试集的大小

我正在使用MNIST和Keras来了解CNN。我正在Keras API下载手写数字的MNIST数据库,如下所示。数据集已经分为60,000个图像用于训练,10,000个图像用于测试
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
我如何加入培训和测试集,然后将它们分为70%用于培训,30%用于测试?

展开
收起
一码平川MACHEL 2019-01-23 15:06:55 2369 0
1 条回答
写回答
取消 提交回答
  • 没有这样的论点mnist.load_data。相反,您可以numpy通过sklearn(或numpy)拆分来连接数据:

    from keras.datasets import mnist
    import numpy as np
    from sklearn.model_selection import train_test_split

    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x = np.concatenate((x_train, x_test))
    y = np.concatenate((y_train, y_test))

    train_size = 0.7
    x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=train_size, random_seed=2019)
    为可重复性设置随机种子。

    via numpy(如果你不使用sklearn):

    do the same concatenation

    np.random.seed(2019)
    train_size = 0.7
    index = np.random.rand(len(x)) < train_size # boolean index
    x_train, x_test = x[index], x[~index] # index and it's negation
    y_train, y_test = y[index], y[~index]
    您将获得大约所需大小的数组(~210xx而不是21000测试大小)。

    编辑看起来像这个函数的
    源代码mnist.load_data只是从分割为60000/10000测试的url中获取此数据,因此只有一个连接解决方​​法。

    2019-07-17 23:26:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

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