Numpy MaskedArray.argmin()函数| Python
在许多情况下,数据集可能不完整或因存在无效数据而受到污染。例如,传感器可能无法记录数据,或记录无效值。 numpy.ma
模块通过引入掩码数组提供了一种解决此问题的便捷方法。掩码数组是可能缺少或无效条目的数组。
numpy.MaskedArray.argmin()
函数返回沿给定轴的最小值的索引数组。屏蔽值被视为具有值 fill_value。
Syntax : numpy.MaskedArray.argmin(axis=None, fill_value=None, out=None)
Parameters:
axis : [None, integer] If None, the index is into the flattened array, otherwise along the specified axis.
fill_value : [ var, optional] Value used to fill in the masked values.
out : [ndarray, optional] A location into which the result is stored.
-> If provided, it must have a shape that the inputs broadcast to.
-> If not provided or None, a freshly-allocated array is returned.
Return : [index_array ]A new integer_array is returned unless out is specified, in which case a reference to out is returned.
代码#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