Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
numpy
array
communitycreator

What is the nump.argmax() function in NumPy?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The argmax() function in NumPy is simply used to return the indicesthe index positions or index numbers of the maximum values along an axis.

Syntax

numpy.argmax(a, axis=None, out=None, *, keepdims=<no value>)

Required parameter value

The argmax() function takes a mandatory parameter value, a, representing the input array.

Optional parameter value

The argmax() function takes the following optional parameter values:

  • axis: By default, the index is into the flattened array, and is otherwise along the specified axis.
  • keepdims: This takes a Boolean value (True or False). If set to True, the reduced axis is left as dimensions with size one.

Return value

The argmax() function returns an array of indices into the array.

Code example

import numpy as np
# creating a 2D array
myarray = np.arange(6).reshape(2,3) + 1

# implementing the argmax() function
newarray1 = np.argmax(myarray, axis=None)

# at axis 0
newarray2 = np.argmax(myarray, axis=0)

# at axis 1
newarray3 = np.argmax(myarray, axis=1)

print(myarray)
print(newarray1)
print(newarray2)
print(newarray3)
Implementing the argmax() function

Code example

  • Line 1: We import the numpy module.

  • Line 3: We create a 2D array, myarray, with a sequence of elements starting from 1 to 6 using the reshape() and the arange() functions.

  • Line 6: We implement the argmax() function on the array myarray, setting axis value as None. The result is assigned to a variable newarray1.

  • Line 9: We implement the argmax() function on the array myarray, setting the axis value as 0. The result is assigned to a variable newarray2.

  • Line 12: We implement the argmax() function on the array myarray, setting the axis value as 1. The result is assigned to a variable newarray3.

  • Lines 14-17: We print all the arrays we created in the code.

RELATED TAGS

function
numpy
array
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring