Max & Min of NumPy Array in Python (3 Examples)

 

In this tutorial, I’ll illustrate how to calculate the maximum and minimum of a NumPy array in the Python programming language.

Table of contents:

Let’s dive right into the examples.

 

Example Data & Libraries

If we want to use the functions of the NumPy library, we first need to import NumPy:

import numpy as np                             # Import NumPy library in Python

Next, we’ll also need to construct some example data:

my_array = np.array([[1, 2, 3], [4, 5, 6]])    # Create example array
print(my_array)                                # Print example array
# [[1 2 3]
#  [4 5 6]]

The previous output of the Python console shows the structure of our example data: We have created a NumPy array with six values in two rows and three columns.

 

Example 1: Max & Min of All Values in NumPy Array

In this example, I’ll illustrate how to get the minima and maxima of all values in a NumPy array.

To calculate the maximum value, we can use the np.max function as shown below…

print(np.max(my_array))                        # Get max of all array values
# 6

…and to compute the minimum value, we can apply the min function as illustrated in the following Python code:

print(np.min(my_array))                        # Get min of all array values
# 1

 

Example 2: Max & Min of Columns in NumPy Array

Example 2 shows how to find the max and min values of a NumPy array by columns.

For this task, we have to set the axis argument to be equal to 0:

print(np.max(my_array, axis = 0))              # Get max of array columns
# [4 5 6]
print(np.min(my_array, axis = 0))              # Get min of array columns
# [1 2 3]

As you can see, the previous Python codes have returned the maximum and minimum of our NumPy array by column.

 

Example 3: Max & Min of Rows in NumPy Array

In this example, I’ll show how to compute the row-wise maxima and minima of a NumPy array.

Similar to Example 2, we can use the axis argument for this task. However, this time, we have to set the axis argument to 1:

print(np.max(my_array, axis = 1))              # Get max of array rows
# [3 6]
print(np.min(my_array, axis = 1))              # Get min of array rows
# [1 4]

 

Video & Further Resources

I have recently released a video on the Statistics Globe YouTube channel, which illustrates the Python programming code of this article. Please find the video below:

 

The YouTube video will be added soon.

 

Furthermore, you could read the related tutorials on my homepage. You can find some posts below.

 

To summarize: In this post, I have demonstrated how to find the maximum and minimum of a NumPy array in Python. In case you have additional questions, tell me about it in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top