In Python Numpy.nonzero () function

Numpy.nonzero () returns an array, the position of non-zero elements. If the two-dimensional array is non-zero elements is described in several odd row, three-dimensional array is non-zero elements described in the first few lines of the first group of several columns.

For example as follows:

Two-dimensional array:

a = np.array([[1, 0, 3], [0, 2, 0], [0, 0, 9]])
b = np.nonzero(a)
print(b)

Results: (array ([0, 0, 1, 2], dtype = int64), array ([0, 2, 1, 2], dtype = int64))

The first description line array, the second array that describes the column, in order to transform our understanding of the results:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

We see the first nonzero element 1 in row 0 0, the corresponding figures for the bold:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

3 is a second non-zero elements in row 2 0, corresponding to:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

The third is non-zero elements 2 in a row 1, corresponding to:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

9 is a fourth nonzero elements in two rows and two, corresponding to:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

As another three-dimensional array of Liezi:

a = np.array([[[0,1],[2,0]],[[0,3],[4,0]],[[0,0],[5,0]]])
b = np.nonzero(a)
print(b)

结果为:(array([0, 0, 1, 1, 2], dtype=int64), array([0, 1, 0, 1, 1], dtype=int64), array([1, 0, 1, 0, 0], dtype=int64))

The same deformation:

array [0, 0, 1, 1, 2] first described in several groups

array [0, 1, 0, 1, 1] Description Line

array [1, 0, 1, 0, 0] Description column

1 is the first nonzero element in a row 0 Group 0, corresponding to

array[0, 0, 1, 1, 2]     

array[0, 1, 0, 1, 1]     

array[1, 0, 1, 0, 0] 

2 is a second non-zero elements in Row 1 0 0 group, the corresponding

array[0, 0, 1, 1, 2]     

array[0, 1, 0, 1, 1]     

array[1, 0, 1, 0, 0] 

3 is the third non-zero elements in row 0 1 1 group, corresponding to

array[0, 0, 1, 1, 2]     

array[0, 1, 0, 1, 1]     

array[1, 0, 1, 0, 0] 

4 is a fourth non-zero elements in row 0 1 1 group, corresponding to

array[0, 0, 1, 1, 2]     

array[0, 1, 0, 1, 1]     

array[1, 0, 1, 0, 0] 

5 is the fifth non-zero elements in 2 groups of rows 10 columns corresponding to

array[0, 0, 1, 1, 2]     

array[0, 1, 0, 1, 1]     

array[1, 0, 1, 0, 0] 

Higher dimension calculation similar, readers can derive their own

Guess you like

Origin www.cnblogs.com/liuys635/p/11236953.html
Recommended