如何在numpy数组的每一列中找到第一个非零值? [英] How to find first non-zero value in every column of a numpy array?

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

问题描述

假设我有一个形式为 numpy 的数组:

Suppose I have a numpy array of the form:

arr=numpy.array([[1,1,0],[1,1,0],[0,0,1],[0,0,0]])

我想找到值不为零的第一个索引(对于每一列)的索引.

I want to find the indices of the first index (for every column) where the value is non-zero.

因此,在这种情况下,我希望返回以下内容:

So in this instance, I would like the following to be returned:

[0,0,2]

我该怎么做?

推荐答案

首次出现的索引

使用np.argmax 沿着该轴(此处列的第零轴)在非零掩码上获得第一个 matches(真值)的索引 -

(arr!=0).argmax(axis=0)

扩展到涵盖通用轴说明符,并且对于沿该轴找不到元素的非零值的情况,我们将有这样的实现 -

Extending to cover generic axis specifier and for cases where no non-zeros are found along that axis for an element, we would have an implementation like so -

def first_nonzero(arr, axis, invalid_val=-1):
    mask = arr!=0
    return np.where(mask.any(axis=axis), mask.argmax(axis=axis), invalid_val)

注意,由于 argmax() 在所有 False 值上返回 0,所以如果 invalid_val 需要的是0,我们将直接使用 mask.argmax(axis=axis) 获得最终输出.

Note that since argmax() on all False values returns 0, so if the invalid_val needed is 0, we would have the final output directly with mask.argmax(axis=axis).

样品运行 -

In [296]: arr    # Different from given sample for variety
Out[296]: 
array([[1, 0, 0],
       [1, 1, 0],
       [0, 1, 0],
       [0, 0, 0]])

In [297]: first_nonzero(arr, axis=0, invalid_val=-1)
Out[297]: array([ 0,  1, -1])

In [298]: first_nonzero(arr, axis=1, invalid_val=-1)
Out[298]: array([ 0,  0,  1, -1])

扩展以涵盖所有比较操作

要找到第一个 zeros,只需在函数中使用 arr==0 作为 mask 即可.对于第一个等于某个值 val 的,使用 arr == val 等用于 comparisons 可能在这里.

To find the first zeros, simply use arr==0 as mask for use in the function. For first ones equal to a certain value val, use arr == val and so on for all cases of comparisons possible here.

为了找到符合特定比较标准的最后一个,我们需要沿该轴翻转并使用与使用 argmax 相同的想法,然后通过从轴长度偏移来补偿翻转,如如下图-

To find the last ones matching a certain comparison criteria, we need to flip along that axis and use the same idea of using argmax and then compensate for the flipping by offsetting from the axis length, as shown below -

def last_nonzero(arr, axis, invalid_val=-1):
    mask = arr!=0
    val = arr.shape[axis] - np.flip(mask, axis=axis).argmax(axis=axis) - 1
    return np.where(mask.any(axis=axis), val, invalid_val)

样品运行 -

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

In [321]: last_nonzero(arr, axis=0, invalid_val=-1)
Out[321]: array([ 1,  2, -1])

In [322]: last_nonzero(arr, axis=1, invalid_val=-1)
Out[322]: array([ 0,  1,  1, -1])

同样,comparisons 可以通过使用相应的比较器获取 mask 然后在列出的函数中使用来覆盖这里的可能.

Again, all cases of comparisons possible here are covered by using the corresponding comparator to get mask and then using within the listed function.

这篇关于如何在numpy数组的每一列中找到第一个非零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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