📜  numpy count_nonzero 方法 | Python

📅  最后修改于: 2022-05-13 01:55:14.092000             🧑  作者: Mango

numpy count_nonzero 方法 | Python

numpy.count_nonzero()函数计算数组 arr 中非零值的数量。

代码#1:

# Python program explaining
# numpy.count_nonzero() function
  
# importing numpy as geek 
import numpy as geek
  
arr = [[0, 1, 2, 3, 0], [0, 5, 6, 0, 7]]
  
gfg = geek.count_nonzero(arr)
  
print (gfg) 

输出 :

6


代码#2:

# Python program explaining
# numpy.count_nonzero() function
  
# importing numpy as geek 
import numpy as geek
  
arr = [[0, 1, 2, 3, 4], [5, 0, 6, 0, 7]]
  
gfg = geek.count_nonzero(arr, axis = 0)
  
print (gfg) 

输出 :

7