5

I am following a machine learning video on youtube at https://www.youtube.com/watch?v=lbFEZAXzk0g. The tutorial is in python2 so I need to convert it into python3. Here is the section of the code I am having an error with:

def load_mnist_images(filename):
    if not os.path.exists(filename):
        download(filename)
    with gzip.open(filename,'rb') as file:
        data = numpy.frombuffer(file.read(),numpy.uint8, offset=16)
        data = data.reshape(-1,1,28,28)
        return data/numpy.float32(256)

I am getting this error: ValueError: cannot reshape array of size 9992 into shape (1,28,28). How do I fix this? In the tutorial it was working. Also, if I have any other errors please tell me.

Varun Rajkumar
  • 306
  • 1
  • 4
  • 12
  • 3
    One issue is that the dimensions do not divide neatly. You can only reshape an array of one size to another size if the new size has the same number of elements as the old size. In this case, you are attempting to resize an array of dimension [9992] into an array of size [?,1,28,28]. 1x28 x 28 is 784, and 9992/784 = 12.74.. not a round number. Thus, there is no neat integer size for dimension 0 such that the reshape operation can neatly divide the input array elements into an output 4-dimensional array of size [?,1,28,28] – DerekG Feb 02 '20 at 17:48

5 Answers5

5

Your input does not have the same number of elements as your output array. Your input is size 9992. Your output is size [? x 1 x 28 x 28] since the -1 indicates that the reshape command should determine how many indices along this dimension are necessary to fit your array. 28x28x1 is 784, so any input you want to reshape to this size must be neatly divisible by 784 so it fits in the output shape. 9992 is not divisible by 784, so it is throwing a ValueError. Here is a minimal example to illustrate:

import numpy as np

data = np.zeros(2352) # 2352 is 784 x 3
out = data.reshape((-1,1,28,28)) # executes correctly -  out is size [3,1,28,28]

data = np.zeros(9992) # 9992 is 784 x 12.745 ... not integer divisible
out = data.reshape((-1,1,28,28)) # throws ValueError: cannot reshape array of size 9992 into shape (1,28,28)

So, if you don't want a ValueError, you need to reshape the input into a differently sized array where it fits correctly.

DerekG
  • 2,058
  • 1
  • 8
  • 17
1

the reshape has the following syntax

data.reshape(shape)

shapes are passed in the form of tuples (a, b). so try,

data.reshape((-1, 1, 28, 28))
psnbaba
  • 81
  • 3
  • 2
    It still gives the error. Can somebody edit this post? – Varun Rajkumar Feb 02 '20 at 18:19
  • 1
    as mentioned by @DerekG, for a shape (-1, a, b, c) the division oldshape/(a*b*c) should be an integer. – psnbaba Feb 03 '20 at 19:25
  • If all element not occupied by mattrix then it gives Value Error. This error simply saying Number of element on old and new dataset are going to different in count which is can't shaped. – Nilesh Jul 23 '20 at 11:43
1

Try like this

import numpy as np

x_train_reshaped=np.reshape(x_train,(60000, 28, 28))
x_test_reshaped=np.reshape(x_test,(10000, 28, 28))
David Buck
  • 3,594
  • 33
  • 29
  • 34
0

I had similar problems but not with images, it looks similar in a way, so here is how I did mine You might need to resize the data first: the data in the code below is your size =9992

datas= np.array([data], order='C')
datas.resize((1,28,28))
datas.shape
temi
  • 21
  • 7
0

You have to balance index and column labels e.g. 6x6=36

DataFrame(np.random.rand(36).reshape((6,9)),
index=['row1','row2','row3','row4','row5','row6',],
columns = ['colmn1','colmn2','colmn3','colmn4','colmn5','colmn6',])

or 4×9=36 would have:

DataFrame(np.random.rand(36).reshape((4,9)),
index=['row1','row2','row3','row4',],
columns = ['colmn1','colmn2','colmn3','colmn4','colmn5','colmn6','colmn7','colmn8','colmn9',])