Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I can't able to understand the output of argmax() and argmin(), whenever I use with the axis parameter. For instance:

>>> a = np.array([[1,2,4,7], [9,88,6,45], [9,76,3,4]])

>>> a

array([[ 1,  2,  4,  7],

       [ 9, 88,  6, 45],

       [ 9, 76,  3,  4]])

>>> a.shape

(3, 4)

>>> a.size

12

>>> np.argmax(a)

5

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

array([1, 1, 1, 1])

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

array([3, 1, 1])

>>> np.argmin(a)

0

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

array([0, 0, 2, 2])

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

array([0, 2, 2])

As should be obvious, the maximum value is the point (1,1) and the lowest one is the point (0,0). So according to my logic when I run:

  • np.argmin(a,axis=0) I anticipated array([0,0,0,0])
  • np.argmin(a,axis=1) I anticipated array([0,0,0])
  • np.argmax(a,axis=0) I anticipated array([1,1,1,1])
  • np.argmax(a,axis=1) I anticipated array([1,1,1])

I don't know where I went wrong?

1 Answer

0 votes
by (26.4k points)

By including the axis argument, NumPy takes a gander at the rows and columns separately. At the point when it's not given, the array is straightened into a solitary(single) 1D array.

axis=0 implies that the activity is performed down the columns of a 2D array thusly.

For instance, np.argmin(a, axis=0) restores the index of the base value in every one of the four columns. The base value(Minimum value) in every segment is appeared in below:

>>> a

array([[ 1,  2,  4,  7],  # 0

       [ 9, 88,  6, 45],  # 1

       [ 9, 76,  3,  4]]) # 2

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

array([0, 0, 2, 2])

Then again, axis=1 implies that the activity is performed across the rows of a. 

That implies np.argmin(a, axis=1) which returns [0, 2, 2] because that a has three rows. 0 is the index of the minimum value from the first row, the index of the base balue of the second and third lines is 2:

>>> a

#        0   1   2   3

array([[ 1,  2,  4,  7],

       [ 9, 88,  6, 45],

       [ 9, 76,  3,  4]])

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

array([0, 2, 2])

Join the python certification course and get a chance to learn python in detail.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
4 answers
0 votes
2 answers

Browse Categories

...