📜  Numpy MaskedArray.argmin()函数| Python

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

Numpy MaskedArray.argmin()函数| Python

在许多情况下,数据集可能不完整或因存在无效数据而受到污染。例如,传感器可能无法记录数据,或记录无效值。 numpy.ma模块通过引入掩码数组提供了一种解决此问题的便捷方法。掩码数组是可能缺少或无效条目的数组。

numpy.MaskedArray.argmin()函数返回沿给定轴的最小值的索引数组。屏蔽值被视为具有值 fill_value。

代码#1:

# Python program explaining
# numpy.MaskedArray.argmin() method 
  
# importing numpy as geek 
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
  
# creating input array 
in_arr = geek.array([1, 2, 3, -1, 5])
print ("Input array : ", in_arr)
  
# Now we are creating a masked array 
# by making third entry as invalid. 
mask_arr = ma.masked_array(in_arr, mask =[0, 0, 1, 0, 0])
print ("Masked array : ", mask_arr)
  
# applying MaskedArray.argmin methods to mask array
out_arr = mask_arr.argmin()
print ("Index of min element in masked array : ", out_arr)
输出:
Input array :  [ 1  2  3 -1  5]
Masked array :  [1 2 -- -1 5]
Index of min element in masked array :  3

代码#2:

# Python program explaining
# numpy.MaskedArray.argmin() method 
  
# importing numpy as geek 
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
  
# creating input array 
in_arr = geek.array([10, 20, 30, -10, 50])
print ("Input array : ", in_arr)
  
# Now we are creating a masked array 
# by making first third entry as invalid. 
mask_arr = ma.masked_array(in_arr, mask =[1, 0, 1, 0, 0])
print ("Masked array : ", mask_arr)
  
# applying MaskedArray.argminmethods to mask array
# and filling the masked location by -100
out_arr = mask_arr.argmin(fill_value = 100)
print ("Index of min element in masked array : ", out_arr)
输出:
Input array :  [ 10  20  30 -10  50]
Masked array :  [-- 20 -- -10 50]
Index of min element in masked array :  0