无法将大小为0的数组重塑为形状

时间:2020-06-25 10:44:16

标签: python arrays pandas

我正在使用的程序经过更正后,我的代码出现错误:

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)

我收到此错误:

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)

我试图定义数组,但仍然无法正常工作。...

2 个答案:

答案 0 :(得分:0)

您两次读取()文件。第一次读取后,光标位于底部。因此,如果您再次阅读,它不会返回任何内容。

您的对象为空,因此无法调整大小。

有关更多信息,请单击here

答案 1 :(得分:0)

问题在于,在应该从文件(all_pixels = np.frombuffer(f.read(), dtype=np.uint8)中获取数据的行中,对f.read()的调用未读取任何内容,从而导致了一个空数组,您不能重塑,原因很明显。

根本原因是没有任何参数的file.read()将读取/使用打开文件中的所有字节。因此,在下一个file.read()调用中,您位于文件的末尾,没有任何内容。

相反,您似乎希望将前16个字节作为标头读取,并将其余的作为数据读取。

要这样做,您应该用要为标头读取的字节数替换对.read()的第一次调用。

这将确保您仅读取前几个字节,其余的将由随后的f.read()调用读取:

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:
        header = f.read(16)  # read the header bytes
        # read the header, information into a bunch of variables:
        _ignored, n_images, image_columns, image_rows = struct.unpack('>IIII', bytearray(header))
        print(_ignored, n_images, image_columns, image_rows)
        print(header)
        # read all the pixels into a long numpy array:
        data = f.read()  # read the data bytes
        all_pixels = np.frombuffer(data, 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)