跳到内容

argwhere

查找数组中非零元素的索引,并按元素分组。

参数

名称 类型 描述 默认值
a array_like

输入数据。

必需

返回值

名称 类型 描述
index_array ndarray
另请参阅

sparse.where, sparse.COO.nonzero

示例

>>> import sparse
>>> x = sparse.COO(np.arange(6).reshape((2, 3)))
>>> sparse.argwhere(x > 1)
array([[0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])
源代码位于 sparse/numba_backend/_coo/common.py
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
def argwhere(a):
    """
    Find the indices of array elements that are non-zero, grouped by element.

    Parameters
    ----------
    a : array_like
        Input data.

    Returns
    -------
    index_array : numpy.ndarray

    See Also
    --------
    [`sparse.where`][], [`sparse.COO.nonzero`][]

    Examples
    --------
    >>> import sparse
    >>> x = sparse.COO(np.arange(6).reshape((2, 3)))
    >>> sparse.argwhere(x > 1)
    array([[0, 2],
           [1, 0],
           [1, 1],
           [1, 2]])
    """
    return np.transpose(a.nonzero())