python nonzero function

First build a simple matrix:

  1. from numpy import *

  2. a = mat([[1,1,0],[1,1,0],[1,0,3]])

  3. print(a)

The output result is as follows:

print(a.nonzero())

 

The first array represents the row where the non-zero element is located, and the second array represents the column where the non-zero element is located. The values ​​of the corresponding positions are taken to form the coordinates of the non-zero elements.

  1. print(len(a.nonzero()[0])) # 非零元素的个数

  2. print(a[a.nonzero()]) # 非零元素的值

 

Explain the nonzero function in detail from one-dimensional, two-dimensional, and three-dimensional perspectives

The np.nonzero function is a function in numpy to get the position (array index) of the non-zero element in the array. Generally speaking, through help(np.nonzero)being able to view the analysis and routines of the function. However, because the routines are English abbreviations, it is still very difficult to read. Therefore, this article translates its English explanations into Chinese for easy understanding.

Explanation

nonzero(a) 

Returns an array of index values ​​of non-zero elements in array a.

(1) Only non-zero elements in a have index values, and those zero-valued elements have no index values; 
(2) The returned index value array is a 2-dimensional tuple array, which contains a one-dimensional array array. Among them, the number of one-dimensional array vectors is consistent with 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 two-dimensional array, the index value array has two arrays, the first array describes the index value from the row dimension; the second array describes the index value from the column dimension. 
(4) This np.transpose(np.nonzero(x)) 
function can describe the index value of each non-zero element in different dimensions. 
(5) By a[nonzero(a)]getting all the non-zero values ​​in a

 

#a是1维数组
a = [0,2,3]
b = np.nonzero(a)
print(np.array(b).ndim)
print(b)

结果:
2
(array([1, 2], dtype=int64),)
说明:索引1和索引2的位置上元素的值非零。

#a是2维数组
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)))
结果:
2
(array([0, 2], dtype=int64), array([2, 2], dtype=int64))
[[0 2]
 [2 2]]
说明:
(1)a中有2个非零元素,因此,索引值tuple中array的长度为2。因为,只有非零元素才有索引值。
(2)索引值数组是2 维的。实际上,无论a的维度是多少,索引值数组一定是2维的tuple,但是tuple中的一维array个数和a的维数一致。
(3)第1个array([0, 2])是从row值上对3和9进行的描述
。第2个array([2, 2])是从col值上对3和9的描述。这样,从行和列上两个维度上各用一个数组来描述非零索引值。
(4)通过调用np.transpose()函数,得出3的索引值是[0 2]
,即第0行,第2列。


#a是3维数组
a = np.array([[[0,1],[1,0]],[[0,1],[1,0]],[[0,0],[1,0]]])
b = np.nonzero(a)
print(np.array(b).ndim)
print(b)
结果:
2
(array([0, 0, 1, 1, 2], dtype=int64), array([0, 1, 0, 1, 1], dtype=int64), array([1, 0, 1, 0, 0], dtype=int64))
说明:由于a是3维数组,因此,索引值数组有3个一维数组。
print(a)
[[[0 1]
  [1 0]]

 [[0 1]
  [1 0]]

 [[0 0]
  [1 0]]]
  a的数组结构如上所示,请将a想像为数量为3的一组小图片,每张图片的大小为2*2,下文中以num * row * col来分别表示其维度。
  b包含3个长度为5的array,这意味着a有3维,且a共有5个非0值。
  先说b中的第1个向量是[0, 0, 1, 1, 2],这实际是a在num维度上描述的非零值。第0张图上有2个非零值,第1张图上有2个非零值,第2张图上有1个非零值。因此在num维度上的非零值数组为[0, 0, 1, 1, 2]。
  b中的第2个向量是[0, 1, 0, 1, 1],这实际是a在row维度上描述的非零值。由于row上的值只有0和1(只2行),所以只由0和1组成。
  b中的第3个向量,聪明的读者可能已经明白,不再赘述。

 

Reprinted from: https://blog.csdn.net/u013698770/article/details/54632047

https://blog.csdn.net/qq_32005671/article/details/74011045

Guess you like

Origin blog.csdn.net/xiezhen_zheng/article/details/81326106