Friday, October 3, 2025
HomeLanguagesNumpy MaskedArray.argmin() function | Python

Numpy MaskedArray.argmin() function | Python

In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arrays that may have missing or invalid entries.

numpy.MaskedArray.argmin() function returns array of indices of the minimum values along the given axis. Masked values are treated as if they had the value 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.

Code #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)


Output:

Input array :  [ 1  2  3 -1  5]
Masked array :  [1 2 -- -1 5]
Index of min element in masked array :  3

 

Code #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)


Output:

Input array :  [ 10  20  30 -10  50]
Masked array :  [-- 20 -- -10 50]
Index of min element in masked array :  0
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32332 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS