python - 如何将数据集拆分为特定的数字?
问题描述
我正在使用 keras MNIST 数据集,该数据集带有 60k 图像的训练集和 10k 图像的测试集。在我的作业中,系统提示我将训练集进一步拆分为 50k 用于训练和 10k 用于验证。我有点不确定如何执行此操作和/或处理此操作,因为我不必像以前那样将数据集拆分为特定数字。这是我到目前为止的代码:
import numpy as np
import scipy
import matplotlib.pyplot as plt
from keras.datasets import mnist
from util import func_confusion_matrix
# load (downloaded if needed) the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# transform each image from 28 by28 to a 784 pixel vector
pixel_count = x_train.shape[1] * x_train.shape[2]
x_train = x_train.reshape(x_train.shape[0], pixel_count).astype('float32')
x_test = x_test.reshape(x_test.shape[0], pixel_count).astype('float32')
# normalize inputs from gray scale of 0-255 to values between 0-1
x_train = x_train / 255
x_test = x_test / 255
解决方案
你可以直接使用 sklearn 包:
import numpy as np
import scipy
import matplotlib.pyplot as plt
from keras.datasets import mnist
from util import func_confusion_matrix
# load (downloaded if needed) the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
from sklearn.model_selection import train_test_split
x_train,x_val,y_train,y_val = train_test_split(x_train,y_train,test_size=0.1)
# here test_size=0.1 means you are selecting 10% of data for split.
现在您拥有 x_train,x_test,x_val 和 y_train,y_test,y_val 变量中的所有数据。
推荐阅读
- php - PHP,找到 XML 的元素并用它做一些事情
- spring-boot - BeforeAll 方法没有被触发
- c# - 如何在我的代码中找到错误的位置
- python - AttributeError:“张量”对象没有“下”属性
- firebase - 已查看 Firebase 文档,关于选择 Firestore 和 Cloud Functions 位置仍有几个问题
- speech-recognition - Kaldi解码中需要语音
- php - Laravel - 资源计数月份日期
- reactjs - 如何根据收到的道具使用 React Router 设置动态路由?
- php - 有没有办法压缩通过 php 创建的 html 输出?
- jquery - 在 Firefox 中添加 JQuery datepicker 后如何隐藏 HTML 日历?