Understanding argmax

前端 未结 4 691
生来不讨喜
生来不讨喜 2020-12-24 13:49

Let say I have the matrix

import numpy as np    
A = np.matrix([[1,2,3,33],[4,5,6,66],[7,8,9,99]])

I am trying to understand the function a

4条回答
  •  佛祖请我去吃肉
    2020-12-24 14:16

    In my first steps in python i have tested this function. And the result with this example clarified me how works argmax.

    Example:

    # Generating 2D array for input 
    array = np.arange(20).reshape(4, 5) 
    array[1][2] = 25
    
    print("The input array: \n", array) 
    
    # without axis
    print("\nThe max element: ", np.argmax(array))
    # with axis
    print("\nThe indices of max element: ", np.argmax(array, axis=0)) 
    print("\nThe indices of max element: ", np.argmax(array, axis=1)) 
    

    Result Example:

    The input array: 
    [[ 0  1  2  3  4]
    [ 5  6 25  8  9]
    [10 11 12 13 14]
    [15 16 17 18 19]]
    
    The max element:  7
    
    The indices of max element:  [3 3 1 3 3]
    
    The indices of max element:  [4 2 4 4]
    

    In that result we can see 3 results.

    1. The highest element in all array is in position 7.
    2. The highest element in every column is in the last row which index is 3, except on third column where the highest value is in row number two which index is 1.
    3. The highest element in every row is in the last column which index is 4, except on second row where the highest value is in third columen which index is 2.

    Reference: https://www.crazygeeks.org/numpy-argmax-in-python/

    I hope that it helps.

提交回复
热议问题