与布尔数组一起使用的 python scipy ndimage.label() 函数中出现“未找到匹配签名”错误

问题描述 投票:0回答:1

我有一个布尔数组来指示干燥的日子:

dry_bool

0        False
1        False
2        False
3        False
4        False
...  
15336    False
15337    False
15338    False
15339    False
15340    False
Name: budget, Length: 15341, dtype: object

False 和 True 值的数量为:

False    14594
True       747
Name: budget, dtype: int64

我想找到连续的干燥天(其中 dry_bool=True)。 使用 scipy 包的 ndimage.label() 函数

import scipy.ndimage as ndimage

events, n_events = ndimage.label(dry_bool)

给我以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[76], line 2
      1 # Find contiguous regions of dry_bool = True
----> 2 events, n_events = ndimage.label(dry_bool)

File ~/opt/anaconda3/lib/python3.9/site-packages/scipy/ndimage/_measurements.py:219, in label(input, structure, output)
    216         return output, maxlabel
    218 try:
--> 219     max_label = _ni_label._label(input, structure, output)
    220 except _ni_label.NeedMoreBits as e:
    221     # Make another attempt with enough bits, then try to cast to the
    222     # new type.
    223     tmp_output = np.empty(input.shape, np.intp if need_64bits else np.int32)

File _ni_label.pyx:202, in _ni_label._label()

File _ni_label.pyx:239, in _ni_label._label()

File _ni_label.pyx:95, in _ni_label.__pyx_fused_cpdef()

TypeError: No matching signature found

我无法理解这一点。知道发生了什么事吗?

肯定有连续的干旱天,正如我在 dry_bool 数组中读取 True 值的索引时所看到的:

dry_idcs = [i for i, x in enumerate(dry_bool) if x]
print(dry_idcs[0:10])

[165, 205, 206, 214, 229, 230, 262, 281, 292, 301]

python arrays scipy boolean ndimage
1个回答
0
投票

在您的第一个代码片段中,它显示

dtype: object
,即使它应该是数据类型布尔值(而不是对象)的数组。鉴于您的错误消息给您一个
TypeError
,也许这就是问题所在?

您可以尝试显式转换数组的数据类型:

dry_bool = dry_bool.astype(bool)

如果您提供更多代码(例如,如何创建/填充数组

dry_bool
),则更容易找出问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.