📜  Python中的 numpy.flatnonzero()

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

Python中的 numpy.flatnonzero()

numpy.flatnonzero()函数用于计算 arr 扁平版本中非零的索引。

代码#1:工作

# Python program explaining
# flatnonzero() function
  
import numpy as geek
arr = geek.arange(-3, 4)
  
print ("Input  array : ", arr)
    
out_arr = geek.flatnonzero(arr)
print ("Indices of non zero elements : ", out_arr) 

输出 :

Input  array :  [-3 -2 -1  0  1  2  3]
Indices of non zero elements :  [0 1 2 4 5 6]


代码#2:使用非零元素的索引作为索引数组。

# Python program using the indices of the non-zero 
# elements as an index array to extract these elements
  
out_arr = arr.ravel()[geek.flatnonzero(arr)]
  
print ("Output array of non-zero number: ", out_arr) 

输出 :

Output array of non-zero number:  [-3 -2 -1  1  2  3]