I want to do some data augmentation on MNIST and therefore must manually label the Y set. This can be automated if MNIST is unshuffled, however mnist.load_data() appears to be returned in a shuffled state. How do I load this with Keras or TensorFlow in an unshuffled state?

UPDATE: It's trivial to unshuffle manually with:

perms=np.where(Y==0)
x[perms] #all X that are 0


Nevertheless would be nice if Keras had a way to import MNIST directly without shuffle.

• Welcome to the site! If you managed to answer your own question, you should post it as an answer (you're allowed to answer your own question) so that it may benefit others in the future. – I_Play_With_Data Mar 7 '19 at 20:39

I do not believe that Keras returns the MNIST data shuffled. You can see that it is not the case below.

First I will define a function which we will use to plot the MNIST data nicely.

import matplotlib.pyplot as plt
%matplotlib inline

# utility function for showing images
def show_imgs(x_test, decoded_imgs=None, n=10):
plt.figure(figsize=(20, 4))
for i in range(n):
ax = plt.subplot(2, n, i+1)
plt.imshow(x_test[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

if decoded_imgs is not None:
ax = plt.subplot(2, n, i+ 1 +n)
plt.imshow(decoded_imgs[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()


Then let's reload and plot the MNIST dataset 4 separate times. We will see that the order of these is unchanged

for i in range(4):
(x_train, y_train), (x_test, y_test) = mnist.load_data()
show_imgs(x_train, x_test)
print('Training labels: ', y_train[0:10])
print('Testing labels : ', y_test[0:10])


Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

• To confirm I meant I needed the numbers ordered from 0 to 9 for manual classification labeling. – user4779 Mar 8 '19 at 13:31
• @user4779, ah in that case you will need to sort them yourself. – JahKnows Mar 9 '19 at 3:55