Import and reshape MNIST data, numpy

Question:

I want to reshape the MNIST dataset from shape (70000, 784) to (70000, 28, 28), the following code is tryed, but it gets a TypeError:

TypeError: only integer scalar arrays can be converted to a scalar index

df = pd.read_csv('images.csv', sep=',', header=None)
x_data = np.array(df)

x_data = x_data.reshape(x_data[0], 28, 28)

This works, but is slow

data = np.array(df)
x_data = []

for d in data:
    x_data.append(d.reshape(28,28))

x_data = np.array(x_data)

How should this be with numpy.reshape() and without looping?
Manny thanks!

Asked By: lowz

||

Answers:

I think, the problem with the second one is because ur using a for loop it can take more time. So i would suggest you can try this

import tensorflow as tf


#load the data

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', validation_size=0)

#considering only first 2 data points
img = mnist.train.images[:2]
x = tf.reshape(img, shape=[-1, 28, 28, 1]) # -1 refers to standard feature which is equivalent to 28*28*1 here

Ideally i got the shape for x as (2, 28, 28, 1). Hope this helps!!

Answered By: Mohanrac

For MNIST dataset, you may use following to convert your dataset into 3D,

train = pd.read_csv("images.csv")
data = data.values.reshape(-1,28,28,1)

assuming you have data as pandas dataframe and first label column is already dropped.

Answered By: Ashish Malhotra

Datasets.fetch_openml returns pair values includes features and target of mnist data.

Then we reshape the a certain row of feature in (28,28) 2-D array.

And as these features are the pixel intensity we can plot this 2-D array to visualise.

    pixel_values,targets=datasets.fetch_openml(                  
    'mnist_784',                 
    version=1,
    return_X_y=True
                            
)

single_image=pixel_values[1:2].values.reshape(28,28)

plt.imshow(single_image,cmap='gray')
Answered By: saty035