可以numpy。savetxt在N维ndarray中使用N>2?

时间:2022-08-01 21:22:40

I am trying to output a 4D numpy float array to a plaintext file using numpy.savetxt

我尝试使用numpy.savetxt将一个4D numpy浮动数组输出到一个纯文本文件。

However numpy gives an error saying that a float argument is required when I try to pass this array. Nevertheless the numpy doc specifies that the argument to be passed should just be array like... NOT that it should be of max rank 2. The only way I can make it work is by reshaping the data to 2D (and this is actually not always practical for data organisation reasons)

但是,numpy给出了一个错误,说当我试图传递这个数组时,需要一个浮动参数。然而,numpy文档指定要传递的参数应该是像……不是说它应该是2。我能让它工作的唯一方法是将数据重新修改为2D(这实际上并不总是适用于数据组织的原因)

Is there way around this? Or must one necessarily reshape the numpy array to 2D? I was expecting to be able to read the data in fortran like column-by-column style (working up through the dimensions).

有办法解决这个问题吗?或者一定要把numpy数组重塑为2D?我希望能够阅读fortran的数据,如列柱式(在维度中工作)。

Are there other possibilities? Note that I do not want to use the npy format since I seek compatibility with another program which needs plaintext format.

有其他的可能性?注意,我不想使用npy格式,因为我希望与另一个需要明文格式的程序兼容。

2 个解决方案

#1


3  

A different approach is to save the array as a simple list of numbers (the flat version of the array) and save along it the information about its shape.

另一种方法是将数组保存为一个简单的数字列表(数组的平面版本),并将它的形状保存下来。

The problem about multidimensional arrays is that it's not that simple to move them from program to program even in text format.

多维数组的问题在于,即使是文本格式,也不容易将它们从程序转移到程序。

you can do something like this:

你可以这样做:

myarray = rand(5,5,5)
name = 'myarray'+myarray.shape+'.txt'
np.savetxt(name,myarray.flatten())

and use the information on the size inclued in the filename to restore the initial shape

并使用文件名中包含的大小信息来恢复初始形状。

#2


5  

If you look at the source code for numpy.savetxt you'll find

如果您查看numpy的源代码。savetxt你会发现

    for row in X:
        fh.write(asbytes(format % tuple(row) + newline))

so numpy.savetxt will only work for 1- or 2D-arrays.

所以numpy。savetxt只适用于1或2个数组。

For interoperability, you could use JSON if you have enough memory to convert the numpy array to a list:

对于互操作性,如果有足够的内存将numpy数组转换为列表,则可以使用JSON:

import json
import numpy as np
a = np.arange(24).reshape(-1, 2, 3, 4).astype('float')
a[0,0,0,0] = np.nan
with open('/tmp/out', 'w') as f:
    json.dump(a.tolist(), f, allow_nan = True)

yields

收益率

[[[[NaN, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0], [8.0, 9.0, 10.0, 11.0]], [[12.0, 13.0, 14.0, 15.0], [16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0]]]]

#1


3  

A different approach is to save the array as a simple list of numbers (the flat version of the array) and save along it the information about its shape.

另一种方法是将数组保存为一个简单的数字列表(数组的平面版本),并将它的形状保存下来。

The problem about multidimensional arrays is that it's not that simple to move them from program to program even in text format.

多维数组的问题在于,即使是文本格式,也不容易将它们从程序转移到程序。

you can do something like this:

你可以这样做:

myarray = rand(5,5,5)
name = 'myarray'+myarray.shape+'.txt'
np.savetxt(name,myarray.flatten())

and use the information on the size inclued in the filename to restore the initial shape

并使用文件名中包含的大小信息来恢复初始形状。

#2


5  

If you look at the source code for numpy.savetxt you'll find

如果您查看numpy的源代码。savetxt你会发现

    for row in X:
        fh.write(asbytes(format % tuple(row) + newline))

so numpy.savetxt will only work for 1- or 2D-arrays.

所以numpy。savetxt只适用于1或2个数组。

For interoperability, you could use JSON if you have enough memory to convert the numpy array to a list:

对于互操作性,如果有足够的内存将numpy数组转换为列表,则可以使用JSON:

import json
import numpy as np
a = np.arange(24).reshape(-1, 2, 3, 4).astype('float')
a[0,0,0,0] = np.nan
with open('/tmp/out', 'w') as f:
    json.dump(a.tolist(), f, allow_nan = True)

yields

收益率

[[[[NaN, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0], [8.0, 9.0, 10.0, 11.0]], [[12.0, 13.0, 14.0, 15.0], [16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0]]]]