Find the maximum and minimum element in a NumPy array


The well-known open-source Python library NumPy is used for numerical computing. Large, multi-dimensional arrays and matrices are supported, along with a sizable number of sophisticated mathematical operations that can be used to work effectively on these arrays.

When working with NumPy arrays, finding the maximum and minimum elements is often necessary.

Method 1:Using max() and min() Functions

The following code demonstrates creating an array of integers. We then use the max() and min() functions to find the highest and lowest value elements in the array, respectively.

Algorithm

  • Import the desired Numpy library.

  • Create a 1D numpy array with integer statistics.

  • discover the most and minimum element through the use of max() and min() features.

  • Print the records.

Example

# Import numpy library
import numpy as np
arry = np.array([69, 96, 99, 14, 3])

# Finding the maximum element
max_elent = np.max(arry)

# Finding the maximum element
min_elent = np.min(arry)

print("max element in the array is:", max_elent)
print("min element in the array is:", min_elent)

Output

Max element in the array is: 99
Min element in the array is: 3

Method 2: Using Argmax() and Argmin() Functions in NumPy

The following code illustrates the implementation of argmax() and argmin() techniques to find the minimal and maximum element within the given array.

Algorithm

  • Import numpy library.

  • Create a numpy array with a listing containing integer statistics.

  • Fetch the index of maximum and minimum factors.

  • Get maximum and minimum elements with their indices.

  • Print the information.

Example

import numpy as npy
ary = npy.array([2023, 56, 400, 2025, 3])

# fetching index of the maximum and minimum element
max_idx = npy.argmax(ary)
min_idx = npy.argmin(ary)

# Get the maximum and minimum element
max_elnt = ary[max_idx]
min_elnt = ary[min_idx]

print("Max_element in the array is:", max_elnt)
print("Min_element in the array is:", min_elnt)

Output

Max_element in the array is: 2025
Min_element in the array is: 3

Method 3: Using the Sort() Method

In this case, we will create a numpy array with a few integers. The array's factors are sorted using the sort() technique from which we can retrieve the minimal and maximum elements from the sorted array’s first and final index.

Algorithm

  • Import the numpy library.

  • developing a numpy array with random factors.

  • sort array using the sort() approach.

  • by gaining access to the initial element we are able to get the array's smallest element.

  • Through accessing the last element you could retrieve the biggest element in the array.

  • Print the information.

Example

import numpy as npy
ary = npy.array([420, 99, 840, 366, 9])
ary.sort()

# Get the minimum and maximum element after sorting
min_elnt = ary[0]
max_elnt = ary[-1]

print("max_ele in the array is:", min_elnt)
print("min_ele in the array is:", max_elnt)

Output

max_ele in the array is: 9
min_ele in the array is: 840

Method 4: Using Amax() and Amin() Functions

In this example, we will construct a two-dimensional NumPy array and make use the amax() and amin() methods to discover the array's maximum and minimal objects.

Algorithm

  • Import the numpy library.

  • Initialise a two-dimensional numpy array.

  • Determine each of the largest and smallest elements in the array using the amax() and amin() methods.

  • Display the details that are required.

import numpy as nmp
ary = nmp.array([[17, 23, 53], [466, 5, 76]])

# Finding max and min element
max_elnt = nmp.amax(ary)
min_elnt = nmp.amin(ary)

print("max element in the array is:", max_elnt)
print("min element in the array is:", min_elnt)

Output

max element in the array is: 466
min element in the array is: 5

Conclusion

The max() and min() methods are efficient and straightforward for managing huge data but they do not provide element indices.

amax() and amin() functions work for both one-dimensional and multi-dimensional arrays. However, it is slower and requires more memory to store the array compared to other methods.

Updated on: 23-Aug-2023

510 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements