A detailed explanation of the np.nonzero of function in numpy

  • 2020-05-26 09:23:02
  • OfStack

The np.nonzero function is used in numpy to get the location (array index) of the non-zero elements in the array array. 1 generally speaking, you can see the parsing and routines of this function through help (np.nonzero). However, because the routine is an English abbreviation, it is still difficult to read, so this article translates the English interpretation into Chinese for easy understanding.

explain

nonzero(a)

Returns an array of index values for the non-zero elements of the array a.

(1) only non-zero elements in a can have index values, and those elements with zero value have no index values;

(2) the returned array of index values is a 2-d tuple array, which contains a 1-d array array. Where, the number of the 1-dimensional array vector is 1 to the dimension of a.

(3) each array of the index value array describes its index value from one dimension. For example, if a is a 2-dimensional array, then the array of index values has two array, and the first array describes the index value from the row dimension. The second array describes the index value from the column dimension.

(4) the np.transpose (np.nonzero (x))

The function can describe the index values of each non-zero element in different dimensions.

(5) get the non-zero value of all a through a[nonzero(a)]

#a is a 1-dimensional array
a = [0,2,3]
b = np.nonzero(a)
print(np.array(b).ndim)
print(b)

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

Note: the value of the element at the location of index 1 and index 2 is non-zero.

#a is a 2-dimensional array
a = np.array([[0,0,3],[0,0,0],[0,0,9]])
b = np.nonzero(a)
print(np.array(b).ndim)
print(b)
print(np.transpose(np.nonzero(a)))

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

Description:

(1) there are two non-zero elements in a, so the length of array in the index value tuple is 2. Because only non-zero elements have an index value.

(2) the array of index values is 2-dimensional. In fact, no matter what the dimension of a is, the index value array 1 must be tuple of 2, but the number of array of 1 in tuple is the same as the dimension 1 of a.

(3) the first array([0, 2]) is a description of 3 and 9 from the row values. The second array([2, 2]) is a description of 3 and 9 from the col values. Thus, non-zero index values are described using an array from each of the two dimensions, row and column.

(4) by calling the np.transpose () function, the index value of 3 is [0 2], that is, the 0th row and the 2nd column.


#a is a 3 dimensional array

a = np.array([[[0,0],[1,0]],[[0,0],[1,0]],[[0,0],[1,0]]])
b = np.nonzero(a)
print(np.array(b).ndim)
print(b)

Results:

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

Note: since a is a 3-dimensional array, there are 3 1-dimensional arrays of indexed values.

Note: array index values start at 0.


Related articles: