Find Max and Min Value of Numpy Array
Find Max and Min Value of Numpy Array

Find Max and Min Value of Numpy Array with its index ( 1D & 2D)

Beginners always face difficulty in finding max and min Value of Numpy. Therefore in this entire tutorial, you will know how to find max and min value of Numpy and its index for both the one dimensional and multi dimensional array.

1. Max and Min Value for the One Dimensional (1-D) array

Let’s create you a 1-D NumPy array using the numpy.array() method.

#1-D array
import numpy as np
array = np.array([19,5,10,1,9,17,50,19,25,50])
print(array)
Creation of 1D- Numpy array
Creation of 1D- Numpy array

Finding the Maximum Value

To find the max value you have to use the max() method.

max = np.max(array)
print("The maximum value in the array is :",max)
Max Value in a 1D Numpy Array
Max Value in a 1D Numpy Array

Index for the Maximum Value

To find the index for the maximum value you have to pass the condition as the argument inside the numpy.where() method.

# Index of the Maximum element
conditon = (array == max)
result = np.where(conditon)
print("Arrays for the max element:",result)
print("List for the maximum value indexes:",result[0])
Index for the Maximum Value in 1D Array
Index for the Maximum Value in 1D Array
Please note that. If there are duplicate values in the array then the output will be a list of the same maximum values.

Finding the Minimum Value

You can find the minimum value using the numpy.min() method.
# Minimum Value
min = np.min(array)
print("The minimum value in the array is :",min)
Min Value in a 1D Numpy Array
Min Value in a 1D Numpy Array

Index of the Minimum Value

The concept to find the index of the min value is the same as finding the maximum values. You have to just change the condition inside the numpy.where() method.
# Index of the Minimum element
conditon = (array == min)
result = np.where(conditon)
print("Arrays for the min element:",result)
print("List for the min value indexes:",result[0])
Index for the Minimum Value in 1D Array
Index for the Minimum Value in 1D Array

2. Max and Min Value for the Two Dimensional (2-D) array

There are three ways you can find the max and min value of the NumPy array.

  1. Maximum and Minumum in the entire array
  2. Max and Min value in each column
  3. Maximum and Minimum value in each row.

Let’s create a two dimensional before finding max and min value. I am randomly generation 2-D array of size 4×3.

#2-D Array
array_2d = np.arange(12).reshape(4,3)
print(array_2d)
Creation of 2D- Numpy array
Creation of 2D- Numpy array

Finding the Max Value in the entire array

You can find the maximum value in the entire array using the same numpy.max() method just like you have used in finding the max in 1D.

max_2d = np.max(array_2d)
print("The maximum value for the 2D-array:",max_2d)
Max Value in a 2D Numpy Array
Max Value in a 2D Numpy Array

Maximum Value in Each Column and Row

Max Value in Column

# maximum value in each column 
max_in_column = np.max(array_2d,axis=0)
print(max_in_column)

"<yoastmark

Max Value in Row

# maximum value in each row 
max_in_row = np.max(array_2d,axis=1)
print(max_in_row)

"<yoastmark

Here I am using the same method max() but now I am passing axis =0 to tell the interpreter to traverse along with the columns and axis =1 to traverse along the columns.

Finding the Index for the Max Value in 2D

You can easily find the index of the max value in a 1-dimensional NumPy array. But for the 2D array, you have to use Numpy module unravel_index. It will easily find the Index of the Max and Min value.

from numpy import unravel_index
result = unravel_index(np.max(array_2d),array_2d.shape)
print("Index for the Maximum Value in the 2D Array is:",result)
Index for the Maximum Value in 2D Array
Index for the Maximum Value in 2D Array

Here I am passing the two arguments inside the unravel_index() method one is the maximum value of the array and shape of the array. Here In our case, the shape of the array is 4 rows and 3 columns.

Finding the Min Value in the entire array

To find the minimum value inside the array you have to use the numpy.min() method and pass the array.

#Minimum Element in the 2D- Array
min_2d = np.min(array_2d)
print("The minimum value for the 2D-array:",min_2d)
Min Value in a 2D Numpy Array
Min Value in a 2D Numpy Array

Minimum Value in Each Column and Row

Min Value in Column

# minimum value in each column 
min_in_column = np.min(array_2d,axis=0)
print(min_in_column)

"<yoastmark

Min Value in Row

# minimum value in each row 
min_in_row = np.min(array_2d,axis=1)
print(min_in_row)

"<yoastmark

To find the min value in each column and row you have to just change the value of the axis, axis = 0 for the column, and axis =1 for the row in the min() method.

Index for the Min Value in 2D

Just like you have learned how to find the index of max value in 2D using the unravel_index() method. Here you will also use that. You have to pass the minimum value and the shape of the array as the arguments.

index_of_min = unravel_index(np.min(array_2d),array_2d.shape)
print("Index for the Minimum Value in the 2D Array is:",index_of_min)
Index for the Minimum Value in 2D Array
Index for the Minimum Value in 2D Array

 

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner