Numpy 查找具有相同值的组的索引

2024-03-28

我有一个由 0 和 1 组成的 numpy 数组:

y=[1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1]

我想计算 1(或 0)组的索引。因此,对于上面的示例,一组的结果应该类似于:

result=[(0,2), (8,9), (16,19)]

(如何)我可以用 numpy 做到这一点吗?我没有发现像分组功能这样的东西。

我尝试过np.ediff1d https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.ediff1d.html,但一直没有找到好的解决办法。并不是说数组可能或可能不以一组开始/结束:

import numpy as np

y = [1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1]
mask = np.ediff1d(y)
starts = np.where(mask > 0)
ends = np.where(mask < 0)

我还在这里找到了部分解决方案:查找元素改变值的索引 numpy https://stackoverflow.com/questions/19125661/find-index-where-elements-change-value-numpy

但那个只给我值变化的索引。


我们可以做这样的事情,适用于任何通用数组 -

def islandinfo(y, trigger_val, stopind_inclusive=True):
    # Setup "sentients" on either sides to make sure we have setup
    # "ramps" to catch the start and stop for the edge islands
    # (left-most and right-most islands) respectively
    y_ext = np.r_[False,y==trigger_val, False]

    # Get indices of shifts, which represent the start and stop indices
    idx = np.flatnonzero(y_ext[:-1] != y_ext[1:])

    # Lengths of islands if needed
    lens = idx[1::2] - idx[:-1:2]

    # Using a stepsize of 2 would get us start and stop indices for each island
    return list(zip(idx[:-1:2], idx[1::2]-int(stopind_inclusive))), lens

样本运行 -

In [320]: y
Out[320]: array([1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1])

In [321]: islandinfo(y, trigger_val=1)[0]
Out[321]: [(0, 2), (8, 9), (16, 19)]

In [322]: islandinfo(y, trigger_val=0)[0]
Out[322]: [(3, 7), (10, 15)]

或者,我们可以使用diff获得切片比较,然后简单地重塑2列来代替步长切片,给我们自己一个单行 -

In [300]: np.flatnonzero(np.diff(np.r_[0,y,0])!=0).reshape(-1,2) - [0,1]
Out[300]: 
array([[ 0,  2],
       [ 8,  9],
       [16, 19]])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Numpy 查找具有相同值的组的索引 的相关文章

随机推荐