Friday, March 1, 2024

NumPy argmax() Operate : Study with Examples


On this tutorial, we are going to see tips on how to use the NumPy argmax() operate in Python together with examples.

 NumPy argmax() Function in Python

The numpy.argmax() operate in Python is used to seek out the indices of the utmost ingredient in an array.

Syntax of NumPy argmax() Operate

Under is the syntax of the NumPy argmax() operate:

import numpy as np
np.argmax(array, axis, out)

  • array : NumPy array.
  • axis : (non-compulsory). Specify the axis to seek out the indices of the utmost worth in two dimensional arrays.
  • out : (non-compulsory). It’s used to retailer the output of the operate.

Instance 1 : argmax() Operate in One-Dimensional Array

The next code makes use of the argmax() operate from the NumPy package deal to return the index of the biggest worth within the one-dimensional array named “my_array”.

Example of argmax() Function

On this array, the utmost worth is 10 which lies on the 2nd index. Understand that indexing begins from 0 in python so the 2nd index refers back to the third ingredient within the array.

import numpy as np
my_array = np.array([5,7,10,3,1])
np.argmax(my_array)

# Output
2

Instance 2 : argmax() Operate in Two-Dimensional Array

On this instance, now we have created a pattern 2D array. On this array, the biggest worth is 15 which lies on the 4nd index as indexing begins from 0 in python as a substitute of 1.

# 2D array
my_array = np.array([[1, 2, 5], [4, 15, 6], [7, 8, 9]])

# Index of the utmost ingredient
np.argmax(my_array)

# Output
4

Instance 3 : Discovering Most Ingredient Indices per Column

After we use axis=0 argument within the argmax() operate, it implies that we wish to discover the indices of the utmost parts alongside every column of the array. In easy phrases, it returns the index of the utmost worth for every column.

Maximum Element Indices per Column

# 2D array
my_array = np.array([[1, 2, 5], [4, 15, 6], [7, 8, 9]])

# Discovering the indices of the utmost parts alongside every column (axis=0)
print(np.argmax(my_array, axis=0))

# Output
[2 1 2]

Instance 4 : Discovering Most Ingredient Indices per Row

After we use axis=1 argument within the argmax() operate, it implies that we wish to discover the indices of the utmost parts alongside every row of the array. In easy phrases, it returns the index of the utmost worth for every row.

Maximum Element Indices per Row

# 2D array
my_array = np.array([[10,5,17],[2,16,3]])

# Discovering the indices of the utmost parts alongside every row (axis=1)
print("Indices of most parts per row:", np.argmax(my_array, axis=1))
print("Indices of most parts per column:", np.argmax(my_array, axis=0))

# Output
Indices of most parts per row: [2 1]
Indices of most parts per column: [0 1 0]

Instance 5 : Retailer Consequence utilizing out Parameter

We will use the out parameter within the argmax() operate to retailer the end result which is the index of the utmost ingredient.

my_array = np.array([5,7,10,3,1])

# Creating an output array to retailer the index of most ingredient
out_array = np.array(0)
np.argmax(my_array,out=out_array)
print("Index of most ingredient:",out_array)

# Output
Index of most ingredient: 2

In two-dimensional array, you will need to create an empty NumPy array with the variety of columns in “my_array” and integer information kind to retailer end result.

my_array = np.array([[7, 4, 6], [6, 5, 7], [7, 8, 9]])

# Creating an output array to retailer the indices of most parts alongside every column
out_array = np.empty(my_array.form[1], dtype=int)

np.argmax(my_array, axis=0, out=out_array)
print("Indices of most parts:",out_array)

# Output
Indices of most parts: [0 2 2]

About Creator:
Deepanshu Bhalla

Deepanshu based ListenData with a easy goal – Make analytics straightforward to know and comply with. He has over 10 years of expertise in information science. Throughout his tenure, he labored with international purchasers in varied domains like Banking, Insurance coverage, Personal Fairness, Telecom and HR.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles