logo_learn_stats

Learn how to Get the Index of Max Price in NumPy Array

Posted on
banner 336x280

You’ll be able to usefulness please see forms to get the index of the max price in a NumPy array:

Form 1: Get Index of Max Price in One-Dimensional Array

banner 468x60
x.argmax()

Form 2: Get Index of Max Price in Every Row of Multi-Dimensional Array

x.argmax(axis=1)

Form 3: Get Index of Max Price in Every Column of Multi-Dimensional Array

x.argmax(axis=0)

Refer to examples display learn how to usefulness each and every mode in apply.

Instance 1: Get Index of Max Price in One-Dimensional Array

Refer to code presentations learn how to get the index of the max price in a one-dimensional NumPy array:

import numpy as np

#manufacture NumPy array of values
x = np.array([2, 7, 9, 4, 4, 6, 3])

#to find index that incorporates max price
x.argmax()

2

The argmax() serve as returns a worth of 2.

This tells us that the price in index place 2 of the array incorporates the utmost price.

If we take a look at the unedited array, we will see that the price in index place 2 is 9, which is certainly the utmost price within the array.

Instance 2: Get Index of Max Price in Every Row of Multi-Dimensional Array

Refer to code presentations learn how to get the index of the max price in each and every row of a multi-dimensional NumPy array:

import numpy as np

#manufacture multi-dimentsional NumPy array
x = np.array([[4, 2, 1, 5], [7, 9, 2, 0]])

#view NumPy array
print(x)

[[4 2 1 5]
 [7 9 2 0]]

#to find index that incorporates max price in each and every row
x.argmax(axis=1)

array([3, 1], dtype=int32)

From the effects we will see:

  • The max price within the first row is situated in index place 3.
  • The max price in the second one row is situated in index place 1.

Instance 3: Get Index of Max Price in Every Column of Multi-Dimensional Array

Refer to code presentations learn how to get the index of the max price in each and every column of a multi-dimensional NumPy array:

import numpy as np

#manufacture multi-dimentsional NumPy array
x = np.array([[4, 2, 1, 5], [7, 9, 2, 0]])

#view NumPy array
print(x)

[[4 2 1 5]
 [7 9 2 0]]

#to find index that incorporates max price in each and every column
x.argmax(axis=0)

array([1, 1, 1, 0], dtype=int32)

From the effects we will see:

  • The max price within the first column is situated in index place 1.
  • The max price in the second one column is situated in index place 1.
  • The max price within the 3rd column is situated in index place 1.
  • The max price within the fourth column is situated in index place 0.

Matching: A Easy Rationalization of NumPy Axes

Backup Assets

Refer to tutorials provide an explanation for learn how to carry out alternative familiar operations in Python:

Learn how to Fill NumPy Array with Values
Learn how to Substitute Parts in NumPy Array
Learn how to Get Particular Row from NumPy Array

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *