Can numpy.savetxt可用于N维的N> N> 2吗? [英] Can numpy.savetxt be used on N-dimensional ndarrays with N>2?

查看:219
本文介绍了Can numpy.savetxt可用于N维的N> N> 2吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用numpy.savetxt输出一个4D numpy浮点数组到一个纯文本文件然而,numpy给出了一个错误,说当我需要float参数的时候尝试通过这个数组。尽管如此,numpy doc指定要传递的参数应该只是数组类似于...不是它应该是最高等级2.唯一可以使其工作的方法是将数据重新整形为2D(而实际上不是总是实际的数据组织的原因)



有没有办法解决这个问题?或者必须一个人必须重新形成numpy数组为2D?
我希望能够像逐列式样(通过维度)来读取fortran中的数据。

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

的方法是将数组保存为一个简单的数字列表(数组的平面版本),并沿着它保存有关其形状的信息。

关于多维数组的问题是不是很简单,即使是文本格式,他们从程序到程序。



你可以这样做:

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

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

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

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)

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).

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.

解决方案

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

这篇关于Can numpy.savetxt可用于N维的N> N> 2吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆