How to Find Min and Max Values in a 1D NumPy Array

5/5 - (8 votes)

Problem Formulation and Solution Overview

This article will show you how to find the minimum and maximum values in a 1D NumPy array.

To make it more interesting, these examples will generate an array of ten (10) random integers using the random.randint() function from the NumPy library and return the minimum and maximum values of the same.


πŸ’¬ Question: How would we write code to find the minimum and maximum values in a NumPy array?

We can accomplish this task by one of the following options:


Preparation

Before moving forward, please ensure that the NumPy library is installed.

Then, add the following code to the top of each script. This snippet will allow the code in this article to run error-free.

import numpy as np 
from numpy import random

Method 1: Use min() and max()

This example uses NumPy’s min() and max() functions to retrieve an array’s minimum and maximum values from a 1D NumPy Array.

rand_nums = random.randint(100, size=(10))

min_num = np.min(rand_nums)
max_num = np.max(rand_nums)
print(min_num, max_num)

The first line in this code generates ten (10) random integers by calling the random.randint() function and passing this two (2) arguments: the range to generate from (0-100) and the total number to return (size=10).

The results save to rand_nums. If output to the terminal, a random array similar to the one below would display.

[49 10 64 86 91 43 42 88 94 10]

The following two (2) lines extract the minimum and maximum values by calling the min() and max() functions respectively and passing rand_nums as an argument to each. The results are output to the terminal.

10 94
NumPy Tutorial - Everything You Need to Know to Get Started

Method 2: Use amin() and amax()

This example uses NumPy’s amin() and amax() functions to retrieve an array’s minimum and maximum values from a 1D NumPy Array.

rand_nums = random.randint(100, size=(10))

min_num = np.amin(rand_nums)
max_num = np.amax(rand_nums)
print(min_num, max_num)

The first line in this code generates ten (10) random integers by calling the random.randint() function and passing this two (2) arguments: the range to generate from (0-100) and the total number to return (size=10).

The results save to rand_nums. If output to the terminal, a random array similar to the one below would display.

[47 87 8 70 94 13 7 69 92 42]

The following two (2) lines extract the minimum and maximum values by calling the min() and max() functions, respectively and passing rand_nums as an argument to each. The results are output to the terminal.

7 94

Method 3: Use sort()

This example uses NumPy’s sort() function to retrieve an array’s minimum and maximum values from a 1D NumPy Array.

rand_nums = random.randint(100, size=(10))
sorted_nums = np.sort(rand_nums)

min_num = sorted_nums[0]
max_num = sorted_nums[len(sorted_nums)-1]
print(min_num, max_num)

The first line in this code generates ten (10) random integers by calling the random.randint() function and passing these two (2) arguments: the range to generate from (0-100) and the total number to return (size=10).

The results save to rand_nums. If output to the terminal, a random array similar to the one below would display.

[49 61 4 60 44 49 17 0 11 3]

The following line sorts the array from smallest to largest. The results save to sorted_num. If output to the terminal, the sorted array displays.

[ 0 3 4 11 17 44 49 49 60 61]

Then, the minimum and maximum values are extracted using slicing and saved to min_num and max_num, respectively and output to the terminal.

0 61
Python One-Liners | Data Science 6 | NumPy Argsort

Method 4: Use minimum() and maximum()

This example uses NumPy’s minimum() and maximum() functions to retrieve an array’s minimum and maximum values from a 1D NumPy Array.

rand_nums = random.randint(20, size=(5))

min_nums = np.minimum(rand_nums, [5,6,7,8,9])
max_nums = np.maximum(rand_nums, [5,6,7,8,9])

The first line in this code generates twenty (20) random integers by calling the random.randint() function and passing these two (2) arguments: the range to generate from (0-20) and the total number to return (size=5).

The results save to rand_nums. If output to the terminal, a random array similar to the one below would display.

[11 13 2 12 8]

The following line calls the minimum() function and passes it two (2) arguments, rand_num, and a second array ([5,6,7,8,9]). This array is the same length as rand_num and is used to compare each element in both arrays and output the lowest value for each in a array format.

[5 6 2 8 8]

If you compare the first element in each array, 11, and 5, 5 is the lowest (minimum) number and is appended to the result. If you then compare 13 and 6, 6 is appended to the result, and so on.

[11 13 2 12 8]
[5 6 7 8 9]

The maximum() function works exactly the same as above, except it returns the maximum value as shown below.

[11 13 7 12 9]

Bonus: Use min() and max()

This example uses NumPy’s min() and max() functions to read in a CSV file into a Pandas DataFrame and retrieve the minimum and maximum values of the Solved column.

The pandas library must be installed to successfully run this code. In addition, the finxter.csv must reside in the current working directory.

import pandas as pd
import numpy as np

df = pd.read_csv('finxter.csv')

min_num = np.min(df['Solved'])
max_num = np.max(df['Solved'])
print(min_num, max_num)

The output from the above displays below.

15 2807

Summary

This article has provided five (5) ways to find the minimum and maximum values in a NumPy array to select the best fit for your coding requirements.

Good Luck & Happy Coding!


Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd