首页 > 解决方案 > numpy.argwhere 文档中的“按元素分组”是什么意思?

问题描述

Numpy argwhere 文档说:

def argwhere(a):
    """
    Find the indices of array elements that are non-zero, grouped by element.
    ...

然而实际的实现只是

return transpose(nonzero(a))

因此不执行重新排序或分组。

我希望

import numpy as np
x = np.full((2,3),6)
x[:,1] = 5
np.argwhere(x)

会回来

array([[0, 1],
       [1, 1],
       [0, 0],
       [0, 2],
       [1, 0],
       [1, 2]])

相反,它返回

array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])

我错过了什么还是文档中的错误?

标签: pythonnumpy

解决方案


[0,1]是数组的一个元素的索引。[1,1]用于下一个非零元素。不要试图在那个措辞中阅读任何深刻的东西。它只是试图将它与返回的数组元组进行对比np.nonzero。在nonzero“分组”中是按维度。


推荐阅读