将numpy数组保存到csv会产生TypeError Mismatch [英] Saving numpy array to csv produces TypeError Mismatch
问题描述
我有一个数字数组的numpy数组,形式如下:
I have a numpy array with numeric data of the form:
example = numpy.array([[[i for i in range(0, 5)],[0 for j in range(0, 5)]] for k in range(0, 10)])
所以它是10组的数组,其中每个组由2个相等长度的列表组成,并且只包含数字。
运行以下保存代码会给我以下错误:
So it's array of 10 groups, where each group consists of 2 lists of equal length and contains only numbers. Running the following save code gives me the error below:
numpy.savetxt('exampleData.csv', test, delimiter=',')
TypeError: Mismatch between array dtype ('int32') and format specifier ('%.18e %.18e')
我猜这可以用fmt ='xyz'参数修复,但文档并不是特别清楚。
I'm guessing this could be fixed with something in the fmt='xyz' argument, but the documentation isn't particularly clear. Any help would be appreciated.
(在我的实际数据中,i和j列表是长浮点列表,例如'0.0047322940571'等)
(In my actual data, the i and j lists are lists of long floats, e.g.'0.0047322940571' etc.)
推荐答案
您的示例
是一个3d数组
In [82]: example=np.array([[[i for i in range(0, 5)],[0 for j in range(0, 5)]] for k in range(0, 3)]) # chg 10 to 3 for display
In [83]: example.shape
Out[83]: (3L, 2L, 5L)
In [84]: example
Out[84]:
array([[[0, 1, 2, 3, 4],
[0, 0, 0, 0, 0]],
[[0, 1, 2, 3, 4],
[0, 0, 0, 0, 0]],
[[0, 1, 2, 3, 4],
[0, 0, 0, 0, 0]]])
尝试保存整个事件会导致错误(由于不同版本的不同邮件):
trying to save the whole thing results in an error (different message due to different version):
In [87]: np.savetxt('test.csv',example, delimiter=',')
....
TypeError: float argument required, not numpy.ndarray
但是保存一行是确定
In [88]: np.savetxt('test.csv',example[1,...], delimiter=',')
使用整数格式保存更漂亮的输出
Save with integer format makes a prettier output
In [94]: np.savetxt('test.csv',example[1,...], delimiter=',',fmt='%d')
In [95]: with open('test.csv') as f:print f.read()
0,1,2,3,4
0,0,0,0,0
那么你想如何保存3d数组呢?记住你将如何使用它/读它。多个文件?一个文件中有多个块?
So how do you want the 3d array to be saved? Keep in mind how you will use it/read it. Multiple files? Multiple blocks within one file?
http://stackoverflow.com/a / 3685339/901925
是一个6岁的如何保存一个3d数组的答案。简单的答案是打开一个文件,并为数组的多个片段执行多个 savetxt
。这将以块为单位保存数据。但是加载这些块是另一个SO问题(之前已经出现过)。
http://stackoverflow.com/a/3685339/901925
is a 6 yr old SO answer on how to save a 3d array. The simple answer is to open a file, and perform multiple savetxt
for slices of the array. This saves the data in blocks. But loading those blocks is another SO question (which has come up before).
In [100]: with open('test.csv','w') as f:
...: for row in example:
...: np.savetxt(f,row,delimiter=',',fmt='%d',footer='====')
...:
In [101]: with open('test.csv') as f:print f.read()
0,1,2,3,4
0,0,0,0,0
# ====
0,1,2,3,4
0,0,0,0,0
# ====
0,1,2,3,4
0,0,0,0,0
# ====
响应您的评论,此操作
example=np.ones((4,2,100))
np.savetxt('test.csv',example[1,...], delimiter=',',fmt='%.18e')
3d数组是重塑它2d。
Another way to save a 3d array is to reshape it to 2d. You reshape it back to 3d after loading, possibly using information that you stored in a comment line
np.savetxt('test.csv',example.reshape(-1,example.shape[-1]), delimiter=',',fmt='%.18e')
这篇关于将numpy数组保存到csv会产生TypeError Mismatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!