如何写一个多维数组到文本文件? [英] How to write a multidimensional array to a text file?

查看:437
本文介绍了如何写一个多维数组到文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个问题是,如果我能提供我遇到的麻烦,其他用户提供了一些帮助。然而,我甚至在一个基本的I / O任务上失败了,比如把一个数组写入一个文件。

任何人都可以解释我需要什么样的循环写一个4x11x14的numpy数组到文件?



这个数组由四个11 x 14数组组成,所以我应该用一个很好的换行符来格式化它,在其他人阅读文件更容易。



编辑:所以我试过了numpy.savetxt函数。奇怪的是,它提供了以下错误:

  TypeError:需要浮点参数,而不是numpy.ndarray 

我认为这是因为该函数不适用于多维数组?任何解决方案,我想他们在一个文件中?

解决方案

如果你想写它到磁盘,这将是容易的以numpy数组的形式读回,请查看 numpy.save 。酸洗它也会工作得很好,但是对于大型数组来说效率不高,所以对于大型数组来说效率会比较低(如果不是这样的话,那么两者都是完全正常的)。

如果你想要请阅读 numpy.savetxt
$ b

编辑:所以,好像 savetxt code>对于具有2维以上的数组来说不是一个很好的选择...但是只是为了得出全部的结论:

我只是意识到在ndarrays上有两个以上的尺寸的 numpy.savetxt 扼流器......这可能是设计的原因,因为在文本文件中没有固定的方式来指示附加的尺寸。

这个(二维数组)工作正常

  import numpy as np 
x = np.arange(20).reshape (4,5))
np.savetxt('test.txt',x)

对于3D数组,同样的事情会失败(带有一个相当不明确的错误: TypeError:float argument required,not numpy.ndarray ):

  import numpy as np 
x = np.arange(200).reshape((4,5,10))
np.savetxt ('test.txt',x)

一个解决方法就是打破3D(或更高)阵列分成二维切片。例如

  x = np.arange(200).reshape((4,5,10))
with file ('test.txt','w')作为outfile:
for slice_2d in x:
np.savetxt(outfile,slice_2d)

但是,我们的目标是清晰的读取人的内容,同时还可以用 numpy.loadtxt 轻松读取。因此,我们可以稍微详细一点,并使用注释掉的行来区分切片。默认情况下, numpy.loadtxt 会忽略任何以开头的行(或者由code>评论
kwarg)。 (这看起来比实际上更加冗长......)

pre $ $ $ $ c $ n $ n

$生成一些测试数据
data = np.arange(200).reshape((4,5,10))

#将数组写入磁盘
with file('test .txt','w')作为outfile:
#我正在为了可读性而写一个头文件
#任何以#开头的行都会被numpy.loadtxt忽略
outfile.write('#Array shape:{0} \\\
'.format(data.shape))

#遍历一个数组产生沿
#轴。在这种情况下,这相当于data [i,:,]:
data_slice在数据中:

#格式化字符串表示我写出
#在左对齐的列中7个字符的宽度
#有2个小数位。
np.savetxt(outfile,data_slice,fmt ='% - 7.2f')

#写出一个中断来表示不同的片断...
outfile.write(' #

这产生了:

 #数组形状:(4,5,10)
0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00
10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00
20.00 21.00 22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00
30.00 31.00 32.00 33.00 34.00 35.00 36.00 37.00 38.00 39.00
40.00 41.00 42.00 43.00 44.00 45.00 46.00 47.00 48.00 49.00
#新片
50.00 51.00 52.00 53.00 54.00 55.00 56.00 57.00 58.00 59.00
60.00 61.00 62.00 63.00 64.00 65.00 66.00 67.00 68.00 69.00
70.00 71.00 72.00 73.00 74.00 75.00 76.00 77.00 78.00 79.00
80.00 8 1.00 82.00 83.00 84.00 85.00 86.00 87.00 88.00 89.00
90.00 91.00 92.00 93.00 94.00 95.00 96.00 97.00 98.00 99.00
#新片
100.00 101.00 102.00 103.00 104.00 105.00 106.00 107.00 108.00 109.00
110.00 111.00 112.00 113.00 114.00 115.00 116.00 117.00 118.00 119.00
120.00 121.00 122.00 123.00 124.00 125.00 126.00 127.00 128.00 129.00
130.00 131.00 132.00 133.00 134.00 135.00 136.00 137.00 138.00 139.00
140.00 141.00 142.00 143.00 144.00 145.00 146.00 147.00 148.00 149.00
#新片
150.00 151.00 152.00 153.00 154.00 155.00 156.00 157.00 158.00 159.00
160.00 161.00 162.00 163.00 164.00 165.00 166.00 167.00 168.00 169.00
170.00 171.00 172.00 173.00 174.00 175.00 176.00 177.00 178.00 179.00
180.00 181.00 182.00 183.00 184.00 185.00 186.00 187.00 188.00 189.00
190.00 191.00 192.00 193.00 194.00 195.00 196.00 197.00 198.00 199.00
#新切片

读回来非常简单只要我们知道原始数组的形状。我们可以做 numpy.loadtxt('test.txt')。reshape((4,5,10))。作为一个例子(你可以在一行中做到这一点,我只是在详细说明事情):

 #阅读数组从磁盘
new_data = np.loadtxt('test.txt')

#请注意,这返回一个二维数组!
print new_data.shape

#但是,如果我们知道数组
#的原始形状,则返回到3D是很容易的。
new_data = new_data.reshape(( 4,5,10))

#只是为了检查它们是否一样...
assert np.all(new_data == data)


In another question, other users offered some help if I could supply the array I was having trouble with. However, I even fail at a basic I/O task, such as writing an array to a file.

Can anyone explain what kind of loop I would need to write a 4x11x14 numpy array to file?

This array consist of four 11 x 14 arrays, so I should format it with a nice newline, to make the reading of the file easier on others.

Edit: So I've tried the numpy.savetxt function. Strangely, it gives the following error:

TypeError: float argument required, not numpy.ndarray

I assume that this is because the function doesn't work with multidimensional arrays? Any solutions as I would like them within one file?

解决方案

If you want to write it to disk so that it will be easy to read back in as a numpy array, look into numpy.save. Pickling it will work fine, as well, but it's less efficient for large arrays (which yours isn't, so either is perfectly fine).

If you want it to be human readable, look into numpy.savetxt.

Edit: So, it seems like savetxt isn't quite as great an option for arrays with >2 dimensions... But just to draw everything out to it's full conclusion:

I just realized that numpy.savetxt chokes on ndarrays with more than 2 dimensions... This is probably by design, as there's no inherently defined way to indicate additional dimensions in a text file.

E.g. This (a 2D array) works fine

import numpy as np
x = np.arange(20).reshape((4,5))
np.savetxt('test.txt', x)

While the same thing would fail (with a rather uninformative error: TypeError: float argument required, not numpy.ndarray) for a 3D array:

import numpy as np
x = np.arange(200).reshape((4,5,10))
np.savetxt('test.txt', x)

One workaround is just to break the 3D (or greater) array into 2D slices. E.g.

x = np.arange(200).reshape((4,5,10))
with file('test.txt', 'w') as outfile:
    for slice_2d in x:
        np.savetxt(outfile, slice_2d)

However, our goal is to be clearly human readable, while still being easily read back in with numpy.loadtxt. Therefore, we can be a bit more verbose, and differentiate the slices using commented out lines. By default, numpy.loadtxt will ignore any lines that start with # (or whichever character is specified by the comments kwarg). (This looks more verbose than it actually is...)

import numpy as np

# Generate some test data
data = np.arange(200).reshape((4,5,10))

# Write the array to disk
with file('test.txt', 'w') as outfile:
    # I'm writing a header here just for the sake of readability
    # Any line starting with "#" will be ignored by numpy.loadtxt
    outfile.write('# Array shape: {0}\n'.format(data.shape))

    # Iterating through a ndimensional array produces slices along
    # the last axis. This is equivalent to data[i,:,:] in this case
    for data_slice in data:

        # The formatting string indicates that I'm writing out
        # the values in left-justified columns 7 characters in width
        # with 2 decimal places.  
        np.savetxt(outfile, data_slice, fmt='%-7.2f')

        # Writing out a break to indicate different slices...
        outfile.write('# New slice\n')

This yields:

# Array shape: (4, 5, 10)
0.00    1.00    2.00    3.00    4.00    5.00    6.00    7.00    8.00    9.00   
10.00   11.00   12.00   13.00   14.00   15.00   16.00   17.00   18.00   19.00  
20.00   21.00   22.00   23.00   24.00   25.00   26.00   27.00   28.00   29.00  
30.00   31.00   32.00   33.00   34.00   35.00   36.00   37.00   38.00   39.00  
40.00   41.00   42.00   43.00   44.00   45.00   46.00   47.00   48.00   49.00  
# New slice
50.00   51.00   52.00   53.00   54.00   55.00   56.00   57.00   58.00   59.00  
60.00   61.00   62.00   63.00   64.00   65.00   66.00   67.00   68.00   69.00  
70.00   71.00   72.00   73.00   74.00   75.00   76.00   77.00   78.00   79.00  
80.00   81.00   82.00   83.00   84.00   85.00   86.00   87.00   88.00   89.00  
90.00   91.00   92.00   93.00   94.00   95.00   96.00   97.00   98.00   99.00  
# New slice
100.00  101.00  102.00  103.00  104.00  105.00  106.00  107.00  108.00  109.00 
110.00  111.00  112.00  113.00  114.00  115.00  116.00  117.00  118.00  119.00 
120.00  121.00  122.00  123.00  124.00  125.00  126.00  127.00  128.00  129.00 
130.00  131.00  132.00  133.00  134.00  135.00  136.00  137.00  138.00  139.00 
140.00  141.00  142.00  143.00  144.00  145.00  146.00  147.00  148.00  149.00 
# New slice
150.00  151.00  152.00  153.00  154.00  155.00  156.00  157.00  158.00  159.00 
160.00  161.00  162.00  163.00  164.00  165.00  166.00  167.00  168.00  169.00 
170.00  171.00  172.00  173.00  174.00  175.00  176.00  177.00  178.00  179.00 
180.00  181.00  182.00  183.00  184.00  185.00  186.00  187.00  188.00  189.00 
190.00  191.00  192.00  193.00  194.00  195.00  196.00  197.00  198.00  199.00 
# New slice

Reading it back in is very easy, as long as we know the shape of the original array. We can just do numpy.loadtxt('test.txt').reshape((4,5,10)). As an example (You can do this in one line, I'm just being verbose to clarify things):

# Read the array from disk
new_data = np.loadtxt('test.txt')

# Note that this returned a 2D array!
print new_data.shape

# However, going back to 3D is easy if we know the 
# original shape of the array
new_data = new_data.reshape((4,5,10))

# Just to check that they're the same...
assert np.all(new_data == data)

这篇关于如何写一个多维数组到文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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