-1

I want to use a portion randomly from the MNIST dataset. Can you help me please? Now the output shape (i.e. Out) is 60000 but I want to get about 2000:

import matplotlib.pyplot as plt
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784) / 255
x_test = x_test.reshape(10000, 784) / 255

x_train.shape  # Out: (60000, 748)
today
  • 32,602
  • 8
  • 95
  • 115

1 Answers1

2

Just take a slice of x_train:

new_x_train = x_train[:2000]

If the data in x_train is ordered (i.e. digits of class 1, then class 2, etc.), then you should first shuffle the data and then slice it:

import numpy as np
indices = np.arange(x_train.shape[0])
np.random.shuffle(indices)
x_train = x_train[indices]

Read more about slicing in numpy documentation.

today
  • 32,602
  • 8
  • 95
  • 115