在numpy数组中查找连续的零 [英] Finding the consecutive zeros in a numpy array

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

问题描述

我有以下数组

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

我想找到值连续为零的数组的开始和结束索引.对于上面的数组,输出如下

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 would be as follows

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

我想尽可能高效地实现这一目标.

I want to achieve this as efficiently as possible.

推荐答案

这是一个相当紧凑的矢量化实现.我稍微改变了要求,所以返回值更像numpythonic":它创建一个形状为 (m, 2) 的数组,其中 m 是运行"零的次数.第一列是每次运行中第一个 0 的索引,第二列是运行后第一个非零元素的索引.(例如,此索引模式匹配切片的工作方式以及 range 函数的工作方式.)

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天全站免登陆