简体   繁体   中英

How do I select only a specific number of samples from the MNIST dataset provided by Keras?

I'm currently training a Convolutional Neural Network on the MNIST data set using Keras. I'm loading the data set using the format

(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

But to reduce iterating through all the data, I want to select only the first 10000 samples from each class 0-9 for X_train and similarly from Y_train . How can I do this?

The MNIST dataset says it returns:

Return:

    2 tuples:
        X_train, X_test: uint8 array of grayscale image data with shape (nb_samples, 28, 28).
        y_train, y_test: uint8 array of digit labels (integers in range 0-9) with shape (nb_samples,).

So you need to slice just the parts you want to keep. I believe the syntax for pandas/numpy is something like:

X_train = X_train[:10000,:,:]
X_test = X_test[:10000,:,:]
y_train = y_train[:10000]
y_test  = y_test[:10000]
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train[:1000,:,:]
x_test = x_test[:500,:,:]
y_train = y_train[:1000]
y_test  = y_test[:500]


print(len(x_train))
print(len(y_train))
print(len(x_test))
print(len(y_test))

#output

> 1000 
> 1000 
> 500 
> 500

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM