如何在numpy数组中获得非零值的位置?

时间:2021-12-06 12:17:05

I'm using np.nonzero() and i dont understand the return

我使用的是np.nonzero(),我不理解返回

I try

我试着

for groupPosition in np.nonzero(groupMatrix):
    print groupPosition

and return [0 0 1 2 3 3 3]

返回[0 0 1 2 3 3]

for groupPosition in zip(np.nonzero(groupMatrix)):
    print groupPosition

and return (array([0, 1, 0, 3, 0, 1, 3]),)

返回(数组([0,1,0,3,0,1,3])

groupMatrix:

groupMatrix:

[[ 1.  1.  0.  0.]
[ 1.  0.  0.  0.]
[ 0.  0.  0.  2.]
[ 3.  3.  0.  2.]]

But don't return the position like a (0, 0)

但是不要返回像(0,0)这样的位置

2 个解决方案

#1


3  

>>> import numpy as np
>>>
>>> groupMatrix = np.array([
...     [1, 1, 0, 0],
...     [1, 0, 0, 0],
...     [0, 0, 0, 2],
...     [3, 3, 0, 2]
... ])
>>> np.nonzero(groupMatrix)
(array([0, 0, 1, 2, 3, 3, 3], dtype=int64), array([0, 1, 0, 3, 0, 1, 3], dtype=int64))
>>> zip(np.nonzero(groupMatrix))
[(array([0, 0, 1, 2, 3, 3, 3], dtype=int64),), (array([0, 1, 0, 3, 0, 1, 3], dtype=int64),)]

Use zip(*...):

使用zip(*):

>>> zip(*np.nonzero(groupMatrix))
[(0, 0), (0, 1), (1, 0), (2, 3), (3, 0), (3, 1), (3, 3)]

zip(*a) is like zip(a[0], a[1], ...)

zip(*a)就像zip([0],[1],…)

>>> a = [(0, 1, 2), (3, 4, 5)]
>>> zip(a)
[((0, 1, 2),), ((3, 4, 5),)]
>>> zip(a[0], a[1])
[(0, 3), (1, 4), (2, 5)]
>>> zip(*a)
[(0, 3), (1, 4), (2, 5)]

See Unpacking Argument Lists.

看到拆包参数列表。

#2


1  

Try the following:

试试以下:

import numpy as np

var = [
    [1.0, 1.0, 0.0, 0.0],
    [1.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 2.0],
    [3.0, 3.0, 0.0, 2.0]
]

rows, cols = np.nonzero(var)

for r, c in zip(rows, cols):
    print var[r][c]

Returns:

返回:

1.0
1.0
1.0
2.0
3.0
3.0
2.0

You are getting the results you are getting because np.nonzero returns a tuple, since your array has 2 dimensions, it has two arrays. Now, each of these arrays need to be used together, so in my example, the function returns the row number, and then the column number. Lets have a look see:

你得到的结果是你得到的,因为np。非零返回一个元组,因为您的数组有两个维度,它有两个数组。现在,每个数组都需要一起使用,所以在我的示例中,函数返回行号,然后是列号。让我们看一看:

>>> import numpy as np
>>> var = [
    [1.0, 1.0, 0.0, 0.0],
    [1.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 2.0],
    [3.0, 3.0, 0.0, 2.0]
]
>>>                 
>>> non_zeroes = np.nonzero(var)
>>> non_zeroes
(array([0, 0, 1, 2, 3, 3, 3]), array([0, 1, 0, 3, 0, 1, 3]))

If we take a close look, we can see that var[0][0] is indeed non zero. so is var[3][3]. However, you will not see a 2 in the first tuple and another 2 at the corresponding index.

如果我们仔细观察,我们可以看到var[0][0]确实是非零的。var[3][3]。但是,在第一个元组中不会看到2,在相应的索引中也不会看到2。

#1


3  

>>> import numpy as np
>>>
>>> groupMatrix = np.array([
...     [1, 1, 0, 0],
...     [1, 0, 0, 0],
...     [0, 0, 0, 2],
...     [3, 3, 0, 2]
... ])
>>> np.nonzero(groupMatrix)
(array([0, 0, 1, 2, 3, 3, 3], dtype=int64), array([0, 1, 0, 3, 0, 1, 3], dtype=int64))
>>> zip(np.nonzero(groupMatrix))
[(array([0, 0, 1, 2, 3, 3, 3], dtype=int64),), (array([0, 1, 0, 3, 0, 1, 3], dtype=int64),)]

Use zip(*...):

使用zip(*):

>>> zip(*np.nonzero(groupMatrix))
[(0, 0), (0, 1), (1, 0), (2, 3), (3, 0), (3, 1), (3, 3)]

zip(*a) is like zip(a[0], a[1], ...)

zip(*a)就像zip([0],[1],…)

>>> a = [(0, 1, 2), (3, 4, 5)]
>>> zip(a)
[((0, 1, 2),), ((3, 4, 5),)]
>>> zip(a[0], a[1])
[(0, 3), (1, 4), (2, 5)]
>>> zip(*a)
[(0, 3), (1, 4), (2, 5)]

See Unpacking Argument Lists.

看到拆包参数列表。

#2


1  

Try the following:

试试以下:

import numpy as np

var = [
    [1.0, 1.0, 0.0, 0.0],
    [1.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 2.0],
    [3.0, 3.0, 0.0, 2.0]
]

rows, cols = np.nonzero(var)

for r, c in zip(rows, cols):
    print var[r][c]

Returns:

返回:

1.0
1.0
1.0
2.0
3.0
3.0
2.0

You are getting the results you are getting because np.nonzero returns a tuple, since your array has 2 dimensions, it has two arrays. Now, each of these arrays need to be used together, so in my example, the function returns the row number, and then the column number. Lets have a look see:

你得到的结果是你得到的,因为np。非零返回一个元组,因为您的数组有两个维度,它有两个数组。现在,每个数组都需要一起使用,所以在我的示例中,函数返回行号,然后是列号。让我们看一看:

>>> import numpy as np
>>> var = [
    [1.0, 1.0, 0.0, 0.0],
    [1.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 2.0],
    [3.0, 3.0, 0.0, 2.0]
]
>>>                 
>>> non_zeroes = np.nonzero(var)
>>> non_zeroes
(array([0, 0, 1, 2, 3, 3, 3]), array([0, 1, 0, 3, 0, 1, 3]))

If we take a close look, we can see that var[0][0] is indeed non zero. so is var[3][3]. However, you will not see a 2 in the first tuple and another 2 at the corresponding index.

如果我们仔细观察,我们可以看到var[0][0]确实是非零的。var[3][3]。但是,在第一个元组中不会看到2,在相应的索引中也不会看到2。