Python 类型错误:数组 dtype('object')和格式说明符('%.18e')不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48230230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:36:33  来源:igfitidea点击:

TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

pythonarraysnumpytextsave

提问by Simplicity

I have the following array:

我有以下数组:

X = np.array([image_array_to_vector1,image_array_to_vector2,image_array_to_vector3,image_array_to_vector4])

A print out of Xlooks as follows:

打印出来的X外观如下:

[array([167, 167, 169, ...,   1,   1,   1], dtype=uint8)
 array([42, 43, 43, ..., 41, 36, 34], dtype=uint8)
 array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)
 array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)]

When I try to save the data as txt:

当我尝试将数据保存为 txt 时:

X_to_text_file = np.savetxt('x.txt',X)

I get the following:

我得到以下信息:

File "/Library/Python/2.7/site-packages/numpy/lib/npyio.py", line 1258, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

Why is that? How can I solve the issue?

这是为什么?我该如何解决这个问题?

Thanks.

谢谢。

回答by Grr

It's a little difficult to duplicate this without some example data, but here is what I came up with.

在没有一些示例数据的情况下复制这个有点困难,但这是我想出的。

arr = np.array([np.array([1,2,3]), np.array([1,2,3,4])])
arr
array([array([1, 2, 3]), array([1, 2, 3, 4])], dtype=object)
np.savetxt('x.txt', arr)
TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

As pointed out by @Artier there is a little snippet at the end of the accepted answer in Write object array to .txt file that points out you can just save the array as a string with fmt='%s'. Using this format seems to solve the problem for me (again I can't recreate your issue exactly without a sample of data).

正如@Artier 所指出的,在Write object array to .txt 文件 中接受的答案末尾有一个小片段,指出您可以将数组保存为带有fmt='%s'. 使用这种格式似乎解决了我的问题(同样,如果没有数据样本,我无法完全重现您的问题)。

np.savetxt('x.txt', arr, fmt='%s')

I would point out that if you are looking to save disparate arrays and want a single location to keep them savezis very useful.

我要指出的是,如果您希望保存不同的数组并希望在一个位置保存它们,这savez将非常有用。

回答by hpaulj

In essence savetxtis doing:

本质上savetxt是在做:

for row in your_array:
    print(fmt % tuple(row))

where fmtis constructed your fmtparameter (or the default one) and the number of columns, and the delimiter.

wherefmt构造您的fmt参数(或默认参数)和列数,以及分隔符。

You have a 1d array of objects (arrays). So the write/print will be

您有一个一维对象数组(数组)。所以写/打印将是

 print(fmt % tuple(element))

%sis the only format that can handle an array (or other general object).

%s是唯一可以处理数组(或其他通用对象)的格式。

savetxtis meant to be used with 2d numeric arrays, the kind of thing that will produce need csvcolumns. Trying to use it on other things like this object array is going to give you headaches.

savetxt旨在与 2d 数值数组一起使用,这种东西会产生需要的csv列。尝试将它用在像这个对象数组这样的其他东西上会让你头疼。

In [2]: A = np.empty((3,),dtype=object)
In [3]: A[:] = [np.arange(3),np.arange(1,4), np.arange(-3,0)]
In [4]: A
Out[4]: array([array([0, 1, 2]), array([1, 2, 3]), array([-3, -2, -1])], dtype=object)

In [6]: np.savetxt('test',A,fmt='%s')
In [7]: cat test
[0 1 2]
[1 2 3]
[-3 -2 -1]

Iterating on a 1d array it must be skipping the tuple. In any case the best you can do is a %sformat. Otherwise write your own custom file writer. savetxtisn't anything special or powerful.

迭代一维数组,它必须跳过tuple. 在任何情况下,你能做的最好的事情就是一种%s格式。否则编写您自己的自定义文件编写器。 savetxt没有什么特别的或强大的。

In [9]: for row in A:
   ...:     print('%s'%row)  
[0 1 2]
[1 2 3]
[-3 -2 -1]

回答by Olga

I had the same error message, and it appeared that the problem was with arrays having different length. So you need to be sure to give the np.savetxt arrays of equal length.

我有同样的错误信息,看来问题出在不同长度的数组上。所以你需要确保给出等长的 np.savetxt 数组。

回答by R.H

For me error occurred when using an older conda environment which had numpy=1.13.1.

对我来说,在使用 numpy=1.13.1 的旧 conda 环境时发生错误。

Updating to numpy=1.16.2 gave no such errors.

更新到 numpy=1.16.2 没有出现这样的错误。