添加2个Numpy掩盖阵列时的奇怪错误

发布于 2025-01-22 13:56:09 字数 885 浏览 0 评论 0原文

因此,我正在尝试将2个Numpy掩盖阵列添加在一起。困难是必须将它们添加为字符串,因为IM试图在产生的输出数组中获取二进制代码。下面的代码是我要做的事情的简化版本。两个阵列的掩码都是相同的(实际上,这些阵列会更大,但想法是相同的):

a = np.zeros((3,3))
b = np.ones((3,3))
amask = [[False,True,True],[True, True, False],[False, False , True]]
bmask = [[False,True,True],[True, True, False],[False, False , True]]

a = a.astype('str')
b= b.astype('str')

am = ma.masked_array(a,mask = amask)
bm = ma.masked_array(b, mask = bmask)
x = np.add(am,bm)

我希望输出成为类似的东西:

[['01' -- --],[-- -- '01'],['01', '01' --]]

因此,它对于它来说是非常重要的,因此它们可以这样添加。

但是,运行此代码会给我以下错误:

numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> None

我认为这不理解,因为我认为两个数组显然具有相同的数据类型。在没有字符串转换的情况下添加它们效果很好,但不会给我所需的输出。我以前遇到过这个错误,并尝试查找它,但从未真正理解过。谢谢

So i'm trying to add 2 numpy masked arrays together. The difficulty is that they have to be added as strings because im trying to get a binary code in the resulting output array. The code below is a simplified version of what i'm trying to do. The mask for both arrays will be the same (In practice these would be way larger arrays, but the idea is the same):

a = np.zeros((3,3))
b = np.ones((3,3))
amask = [[False,True,True],[True, True, False],[False, False , True]]
bmask = [[False,True,True],[True, True, False],[False, False , True]]

a = a.astype('str')
b= b.astype('str')

am = ma.masked_array(a,mask = amask)
bm = ma.masked_array(b, mask = bmask)
x = np.add(am,bm)

I would like the output to be something like :

[['01' -- --],[-- -- '01'],['01', '01' --]]

So it's very important for it to be strings, so they can be added as such.

Running this code however gives me the following error:

numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> None

Which I don't understand since both arrays clearly have the same datatypes in my opinion. Adding them without the string conversion works just fine but doesn't give me the required output. I have run into this error before and tried to look it up but never really understood it. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2025-01-29 13:56:09

这不是蒙版的阵列问题;这是一个字符串dtype。

In [254]: a = np.arange(4)
In [255]: a
Out[255]: array([0, 1, 2, 3])
In [256]: a+a
Out[256]: array([0, 2, 4, 6])
In [257]: a1 = a.astype(str)
In [258]: a1
Out[258]: array(['0', '1', '2', '3'], dtype='<U21')
In [259]: a1 + a1
Traceback (most recent call last):
  Input In [259] in <cell line: 1>
    a1 + a1
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> None

astype(str)用numpy string dtype制作一个数组;这是用于数组存储的优化,但与Python字符串不同。 np.char具有一些可以将字符串方法应用于 dtypes:

In [260]: np.char.add(a1,a1)
Out[260]: array(['00', '11', '22', '33'], dtype='<U42')

或如注释时,您可以列出字符串对象数组的列表:

In [261]: a2 = a1.astype(object)
In [262]: a2
Out[262]: array(['0', '1', '2', '3'], dtype=object)
In [263]: a2 + a2
Out[263]: array(['00', '11', '22', '33'], dtype=object)

对于对象dtype数组,操作员,像+将操作委派给元素方法。等效:

In [264]: [i+j for i,j in zip(a2,a2)]
Out[264]: ['00', '11', '22', '33']

我希望[264]最快。 numpy不会为字符串处理添加太多。

This isn't a masked array issue; it's a string dtype one.

In [254]: a = np.arange(4)
In [255]: a
Out[255]: array([0, 1, 2, 3])
In [256]: a+a
Out[256]: array([0, 2, 4, 6])
In [257]: a1 = a.astype(str)
In [258]: a1
Out[258]: array(['0', '1', '2', '3'], dtype='<U21')
In [259]: a1 + a1
Traceback (most recent call last):
  Input In [259] in <cell line: 1>
    a1 + a1
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> None

astype(str) makes an array with a numpy string dtype; this is optimized for array storage, but is not the same as Python strings. np.char has some functions that can apply string methods to Un dtypes:

In [260]: np.char.add(a1,a1)
Out[260]: array(['00', '11', '22', '33'], dtype='<U42')

Or as commented, you can make a list like array of string objects:

In [261]: a2 = a1.astype(object)
In [262]: a2
Out[262]: array(['0', '1', '2', '3'], dtype=object)
In [263]: a2 + a2
Out[263]: array(['00', '11', '22', '33'], dtype=object)

For object dtype arrays, operators like + delegate the action to the methods of the elements. Equivalently:

In [264]: [i+j for i,j in zip(a2,a2)]
Out[264]: ['00', '11', '22', '33']

I expect [264] to be fastest. numpy doesn't add much to string processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文