[Reprint] Use of numpy.argmin

Reference link: numpy.argmin in Python

https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.argmin.html 

   

 numpy.argmin(a, axis=None, out=None)[source] 

 The following table gives the minimum value in the axis direction 

 Parameters: a: Input array. axis: The input array is flattened by default. Otherwise, follow the axis direction out: optional Returns: index_array: array of subscripts. The shape is the same as the dimension of the input array a minus the axis. 

   

 For example: 

 1. Flatten, axis=0, axis=1 

  

  >>> a = np.arange(6).reshape(2,3)

>>> a

array([[0, 1, 2],

       [3, 4, 5]])

>>> np.argmin(a)

0

>>> np.argmin(a, axis=0)

array([0, 0, 0])

>>> np.argmin(a, axis=1)

array([0, 0])

 

  

    

 2. Multiple minimum values, only the first one is displayed 

  

  >>> b = np.arange(6)

>>> b[4] = 0

>>> b

array([0, 1, 2, 3, 0, 5])

>>> np.argmin(b) # Only the first occurrence is returned.

0

 

  

   ============================= 

 If looking for the maximum (small) value of a list and its corresponding index: 

  

  list = [9, 12, 88, 14, 25]

max_index = max(list) # Maximum index

max_value = list.index(max(list)) # Return the maximum value

# If the smallest, then max is replaced by min

 

  

    

 If it is the type of arrary in numpy: 

  

  a= np.array([9, 12, 88, 14, 25])

list_a = a.tolist()

 

list_a_max_list = max(list_a) #return maximum

max_index = list_a.index(max(list_a)) # Return the index of the maximum value 

 

  

    

  min/max is a built-in function of python np.argmin/np.argmax is a member function in the numpy library 

 (It is suitable for processing numpy.ndarray objects, the optional parameter is axis=0 or 1) 

 # Find the index of the minimum value by each column axis=0 

 # Find the index of the minimum value for each row axis=1 

  

  import numpy as np

 

a = np.array([1, 2, 3, 4])

b = np.array((5, 6, 7, 8))

c = np.array([[11, 2, 8, 4], [4, 52, 6, 17], [2, 8, 9, 100]])

 

print(a)

print(b)

print(c)

 

print(np.argmin(c))

print(np.argmin(c, axis=0)) # Find the index of the minimum value according to each column

print(np.argmin(c, axis=1)) # Find the index of the minimum value per line

# If the smallest, then min is replaced by max

Guess you like

Origin blog.csdn.net/u013946150/article/details/113078589