Find the maximum and minimum element in a NumPy array
Last Updated :
13 Aug, 2021
An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type must be the same.
To overcome these problems we use a third-party module called NumPy. Using NumPy we can create multidimensional arrays, and we also can use different data types.
Note: NumPy doesn't come with python by default. So, we have to install it using pip. To install the module run the given command in the terminal.
pip install numpy
Now let's create an array using NumPy. For doing this we need to import the module. Here we're importing the module.
import numpy
Using the above command you can import the module.
Example 1: Now try to create a single-dimensional array.
arr = numpy.array([1, 2, 3, 4, 5])
Here, we create a single-dimensional NumPy array of integers. Now try to find the maximum element. To do this we have to use numpy.max("array name") function.
Syntax:
numpy.max(arr)
For finding the minimum element use numpy.min("array name") function.
Syntax:
numpy.min(arr)
Code:
Python3
# import numpy library
import numpy
# creating a numpy array of integers
arr = numpy.array([1, 5, 4, 8, 3, 7])
# finding the maximum and
# minimum element in the array
max_element = numpy.max(arr)
min_element = numpy.min(arr)
# printing the result
print('maximum element in the array is: ',
max_element)
print('minimum element in the array is: ',
min_element)
Output:
maximum element in the array is: 8
minimum element in the array is: 1
Note: You must use numeric numbers(int or float), you can't use string.
Example 2: Now, let's create a two-dimensional NumPy array.
arr = numpy.array([11, 5, 7],
[4, 5, 16],
[7, 81, 16]]
Now using the numpy.max() and numpy.min() functions we can find the maximum and minimum element.
Here, we get the maximum and minimum value from the whole array.
Code:
Python3
# import numpy library
import numpy
# creating a two dimensional
# numpy array of integers
arr = numpy.array([[11, 2, 3],
[4, 5, 16],
[7, 81, 22]])
# finding the maximum and
# minimum element in the array
max_element = numpy.max(arr)
min_element = numpy.min(arr)
# printing the result
print('maximum element in the array is:',
max_element)
print('minimum element in the array is:',
min_element)
Output:
maximum element in the array is: 81
minimum element in the array is: 2
Example 3: Now, if we want to find the maximum or minimum from the rows or the columns then we have to add 0 or 1. See how it works:
maximum_element = numpy.max(arr, 0)
maximum_element = numpy.max(arr, 1)
If we use 0 it will give us a list containing the maximum or minimum values from each column. Here we will get a list like [11 81 22] which have all the maximum numbers each column.
If we use 1 instead of 0, will get a list like [11 16 81], which contain the maximum number from each row.
Code:
Python3
# import numpy library
import numpy
# creating a two dimensional
# numpy array of integers
arr = numpy.array([[11, 2, 3],
[4, 5, 16],
[7, 81, 22]])
# finding the maximum and
# minimum element in the array
max_element_column = numpy.max(arr, 0)
max_element_row = numpy.max(arr, 1)
min_element_column = numpy.amin(arr, 0)
min_element_row = numpy.amin(arr, 1)
# printing the result
print('maximum elements in the columns of the array is:',
max_element_column)
print('maximum elements in the rows of the array is:',
max_element_row)
print('minimum elements in the columns of the array is:',
min_element_column)
print('minimum elements in the rows of the array is:',
min_element_row)
Output:
maximum elements in the columns of the array is: [11 81 22]
maximum elements in the rows of the array is: [11 16 81]
minimum elements in the columns of the array is: [4 2 3]
minimum elements in the rows of the array is: [2 4 7]
Example 4: If we have two same shaped NumPy arrays, we can find the maximum or minimum elements. For this step, we have to numpy.maximum(array1, array2) function. It will return a list containing maximum values from each column.
Code:
Python3
# importing numpy library
import numpy
# creating two single dimensional array
a = numpy.array([1, 4, 6, 8, 9])
b = numpy.array([5, 7, 3, 9, 22])
# print maximum value
print(numpy.maximum(a, b))
Output:
[ 5 7 6 9 22]
Similar Reads
Maximum and Minimum element in a Set in Python
We are given a set and our task is to find the maximum and minimum elements from the given set. For example, if we have a = {3, 1, 7, 5, 9}, the maximum element should be 9, and the minimum element should be 1.Letâs explore different methods to do it:1. Using min() & max() functionThe built-in m
3 min read
NumPy ndarray.__abs__() | Find Absolute Value of Elements in NumPy Array
The ndarray.__abs__() method returns the absolute value of every element in the NumPy array. It is automatically invoked when we use Python's built-in method abs() on a NumPy array. Example Python3 import numpy as np gfg = np.array([1.45, 2.32, 3.98, 4.41, 5.55, 6.12]) print(gfg.__abs__()) Output[ 1
1 min read
Find indices of elements equal to zero in a NumPy array
Sometimes we need to find out the indices of all null elements in the array. Numpy provides many functions to compute indices of all null elements. Method 1: Finding indices of null elements using numpy.where() This function returns the indices of elements in an input array where the given conditio
3 min read
Return the maximum of an array or maximum ignoring any NaNs in Python
In this article, we will cover how to return the maximum of an array or maximum by ignoring any NaNs in Python using NumPy. ExampleInput: [ -1. -2. nan 1000.] Output: 1000.0 Explanation: maximum value of the array ignoring nans. One approach to use the built-in Python function max(), along with the
3 min read
Python Program to Find Largest Element in an Array
To find the largest element in an array, iterate over each element and compare it with the current largest element. If an element is greater, update the largest element. At the end of the iteration, the largest element will be found. Given an array, find the largest element in it. Input : arr[] = {1
4 min read
Min, Max and Mean of Off-Diagonal Elements in a Matrix in R
A matrix is a combination of elements stacked together in either row or column format. A table-like structure formed of similar data type elements is known as a matrix. A matrix has two diagonals, one of which is known as the main diagonal. The main diagonal elements are characterized by the proper
3 min read
Calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis
Let's see how to calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis. Here, the Second axis means row-wise. So firstly for finding the row-wise maximum and minimum elements in a NumPy array we are using numpy.amax() and numpy.amin() functi
2 min read
Python heapq to find K'th smallest element in a 2D array
Given an n x n matrix and integer k. Find the k'th smallest element in the given 2D array. Examples: Input : mat = [[10, 25, 20, 40], [15, 45, 35, 30], [24, 29, 37, 48], [32, 33, 39, 50]] k = 7 Output : 7th smallest element is 30 We will use similar approach like Kâth Smallest/Largest Element in Uns
3 min read
Find the nearest value and the index of NumPy Array
In this article, let's discuss finding the nearest value and the index in an array with Numpy. We will make use of two of the functions provided by the NumPy library to calculate the nearest value and the index in the array. Those two functions are numpy.abs() and numpy.argmin(). Example Input Arra
3 min read
Select an element or sub array by index from a Numpy Array
The elements of a NumPy array are indexed just like normal arrays. The index of the first element will be 0 and the last element will be indexed n-1, where n is the total number of elements. Selecting a single element from a NumPy array Each element of these ndarrays can be accessed using its index
2 min read