寻找连续零的numpy的阵列 [英] Finding the consecutive zeros in a numpy array

查看:508
本文介绍了寻找连续零的numpy的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下阵列

  a = [1, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 0, 0, 0, 9, 8, 7,0,10,11]

我想找到的开始和数组,其中的值是零连续,对于上面的输出数组如下所示的结束索引

What I would like to find the start and the end index of the array where the values are zeros consecutively, for the array above the output looks as follows

  [3,8],[12,15],[19]

我想这样做,尽可能高效。

I want to do it as efficient as possible.

感谢

推荐答案

下面是一个相当紧凑的矢量化实现。我已经改变了要求一点,这样返回值是多一点numpythonic:它创建具有形状(M 2),其中m是零的运行数量的数组。第一列是第一个0在每次运行的指数,以及第二个是在运行后的第一个非零元素的索引。 (此索引模式相匹配,例如,如何切片工作,以及如何范围函数的工作。)

Here's a fairly compact vectorized implementation. I've changed the requirements a bit, so the return value is a bit more "numpythonic": it creates an array with shape (m, 2), where m is the number of "runs" of zeros. The first column is the index of the first 0 in each run, and the second is the index of the first nonzero element after the run. (This indexing pattern matches, for example, how slicing works and how the range function works.)

import numpy as np

def zero_runs(a):
    # Create an array that is 1 where a is 0, and pad each end with an extra 0.
    iszero = np.concatenate(([0], np.equal(a, 0).view(np.int8), [0]))
    absdiff = np.abs(np.diff(iszero))
    # Runs start and end where absdiff is 1.
    ranges = np.where(absdiff == 1)[0].reshape(-1, 2)
    return ranges

例如:

In [236]: a = [1, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 0, 0, 0, 9, 8, 7, 0, 10, 11]

In [237]: runs = zero_runs(a)

In [238]: runs
Out[238]: 
array([[ 3,  9],
       [12, 16],
       [19, 20]])

通过这种格式,它是简单的获取每个运行零的个数:

With this format, it is simple to get the number of zeros in each run:

In [239]: runs[:,1] - runs[:,0]
Out[239]: array([6, 4, 1])

它总是一个好主意,检查边界情况:

It's always a good idea to check the edge cases:

In [240]: zero_runs([0,1,2])
Out[240]: array([[0, 1]])

In [241]: zero_runs([1,2,0])
Out[241]: array([[2, 3]])

In [242]: zero_runs([1,2,3])
Out[242]: array([], shape=(0, 2), dtype=int64)

In [243]: zero_runs([0,0,0])
Out[243]: array([[0, 3]])

这篇关于寻找连续零的numpy的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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