Numpy查找具有相同值的组的索引 [英] Numpy find indices of groups with same value

查看:1339
本文介绍了Numpy查找具有相同值的组的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个零和一的numpy数组:

I have a numpy array of zeros and ones:

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

我想计算一个(或零)组的索引.因此,对于上面的示例,一组一组的结果应类似于以下内容:

I want to calculate the indices of groups of ones (or zeros). So for the above example the result for groups of ones should be something similar to:

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

(如何)可以用numpy做到这一点?我找不到像分组功能一样的东西.

(How) Can I do that with numpy? I found nothing like a group-by function.

我尝试了 np.ediff1d ,但找不到一个好的解决方案.并不是说该数组可能会或可能不会以一组数组开始/结束:

I experimented around with np.ediff1d, but couldn't figure out a good solution. Not that the array may or may not begin/end with a group of ones:

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的索引

I also found a partial solution here: Find index where elements change value numpy

但是那只给了我改变值的索引.

But that one only gives me the indices where the values change.

推荐答案

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

We can do something like this that works for any generic array -

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列简单地重塑形状以替换步长大小的切片,从而给自己一个单线-

Alternatively, we can use diff to get the sliced comparisons and then simply reshape with 2 columns to replace the step-sized slicing to give ourselves a one-liner -

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]])

这篇关于Numpy查找具有相同值的组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆