Python Forum
cannot reshape array of size 0 into shape - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: cannot reshape array of size 0 into shape (/thread-27630.html)



cannot reshape array of size 0 into shape - Roro - Jun-14-2020

After a correction on the program i am using, i get an error on my code :
import numpy as np
import gzip
import struct


def load_images(filename):
    # Open and unzip the file of images :
    with gzip.open(filename, 'rb') as f:
        # read the header, information into a bunch of variables:
        _ignored, n_images, image_columns, image_rows = struct.unpack('>IIII', bytearray(f.read()[:16]))
        print(_ignored, n_images, image_columns, image_rows)
        print(f.read()[:16])
        # read all the pixels into a long numpy array :
        all_pixels = np.frombuffer(f.read(), dtype=np.uint8)
        print(all_pixels)
        print(all_pixels.shape)
        print(all_pixels.ndim)
        # reshape the array into a matrix where each line is an image:
        images_matrix = all_pixels.reshape(n_images, image_columns * image_rows)
Error:
load_images("\\MNIST\\train-images-idx3-ubyte.gz") 2051 60000 28 28 b'' [] (0,) 1 Traceback (most recent call last): File "<input>", line 1, in <module> File "<input>", line 19, in load_images ValueError: cannot reshape array of size 0 into shape (60000,784)
I tried to defined the array, but still not working....


RE: cannot reshape array of size 0 into shape - buran - Jun-14-2020

you try to use f.read() several times.
After first use, you are at the end of the file.
As you can see from there on when you try to print on line 12 you get b'', also on line 15 you get [], shape is `(0,) and so on..


RE: cannot reshape array of size 0 into shape - Roro - Jun-14-2020

Thanks, I've added
file.seek(16)


and it seems OK.