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.
Table of Contents
- 1. Max and Min Value for the One Dimensional (1-D) array
- 2. Max and Min Value for the Two Dimensional (2-D) array
1. Max and Min Value for the One Dimensional (1-D) array
Let’s create a 1-D NumPy array using the numpy.array() method. The array() method accepts the list of all values you want to create NumPy array.
import numpy as np array = np.array([19,5,10,1,9,17,50,19,25,50]) print(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)
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)
Finding the Minimum Value
# Minimum Value min = np.min(array) print("The minimum value in the array is :",min)
Index of the Minimum Value
# 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)
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.
- Maximum and Minumum in the entire array
- Max and Min value in each column
- 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)
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)
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)
Max Value in Row
# maximum value in each row max_in_row = np.max(array_2d,axis=1) print(max_in_row)
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)
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)
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)
Min Value in Row
# minimum value in each row min_in_row = np.min(array_2d,axis=1) print(min_in_row)
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)
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.