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

## 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.

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.

## 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.

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.

## 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.

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.

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

## 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.

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.

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.

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

## 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

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

The output from the above displays below.

## 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!