numpy dtype ValueError:固定类型元组中的无效形状 - 我该如何绕过它?

时间:2015-10-20 14:42:57

标签: python numpy

我使用自定义数据类型,例如Thing[i]使用

从二进制文件中读取数据
datatype = np.dtype('({:n},{:n})f4'.format(10000,100000))

但是,使用np.dtype定义数据类型会给大型数据集带来错误,如上面的示例数据类型所示:

np.fromfile(filename, dtype=datatype)

初始化该大小的数组没有问题:ValueError: invalid shape in fixed-type tuple: dtype size in bytes must fit into a C int 。 所以我的问题是:这种限制来自哪里,我该如何解决它?我当然可以使用一个循环并一次读取块,但也许有更优雅的方式?

1 个答案:

答案 0 :(得分:2)

当您指定dtype '(M, N)f4'时,您实际上是在指定输出数组的最后两个维度,例如

np.zeros(5, np.dtype('(6, 7)f4')).shape
# (5, 6, 7)

只需将数据作为一维数组读入,然后将其重新塑造为所需的形状,即可获得相同的结果:

x = np.fromfile(filename, np.float32).reshape(-1, 10000, 100000)