简体   繁体   English

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

[英]Cannot reshape array of size 0 into shape

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)

I get this 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....我试图定义数组,但仍然无法正常工作......

You read() the file twice.您两次读取()文件。 After the first read, the cursor is placed at the bottom.第一次读取后,cursor 放在底部。 So if you read again,it does not return anything.因此,如果您再次阅读,它不会返回任何内容。

Your object is empty so it is impossible to resize.您的 object 是空的,因此无法调整大小。

For more information click here欲了解更多信息,请单击此处

The problem is that in the line that is supposed to grab the data from the file ( all_pixels = np.frombuffer(f.read(), dtype=np.uint8) ), the call to f.read() does not read anything, resulting in an empty array, which you cannot reshape, for obvious reasons.问题是在应该从文件中获取数据的行( all_pixels = np.frombuffer(f.read(), dtype=np.uint8) ),对f.read()的调用没有读取任何内容,导致一个空数组,由于显而易见的原因,您无法对其进行整形。

The underlying reason is that file.read() without any argument will read/consume all the bytes from the open file.根本原因是没有任何参数的file.read()将读取/消耗打开文件中的所有字节。 So by the next file.read() call, you are at the end of the file and nothing is fetched.因此,通过下一个file.read()调用,您位于文件的末尾,并且没有获取任何内容。

Instead, it looks like you would want to read the first 16 bytes as header, and read the rest as data.相反,您似乎希望将前 16 个字节读取为 header,并将 rest 作为数据读取。

To do so, you should replace your first call to .read() with the number of bytes you want to read for the header.为此,您应该将第一次调用.read()替换为您要为 header 读取的字节数。

This will ensure that you get read only the first few bytes, leaving the rest to be read by the subsequent f.read() call:这将确保您只读取前几个字节,而 rest 将由随后的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM