python scipy ndimage.label()函数与布尔数组一起使用时出现“No matching signature found”错误

cyvaqqii  于 8个月前  发布在  Python
关注(0)|答案(1)|浏览(83)

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

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值的数量为:

dry_bool.value_counts()

False    14594
True       747
Name: budget, dtype: int64


我想找到连续的干旱期(其中dry_bool=True)。
第一个月
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]

tpxzln5u

tpxzln5u1#

在你的第一个代码片段中,它显示为dtype: object,尽管它应该是一个数据类型为boolean(而不是object)的数组。
你可以尝试显式地转换数组的数据类型:

dry_bool = dry_bool.astype(bool)

字符串
如果你提供了更多的代码(例如,你如何创建/填充数组dry_bool),那么就更容易找出问题到底是什么。

相关问题