How to Use the Numpy Maximum Function
This article is originally published at https://www.sharpsightlabs.com
In this tutorial, I’ll explain how to use the Numpy maximum function to compute the element-wise maxima of two Numpy arrays.
I’ll explain the syntax of np.maximum, how the function works, and how to use it.
If you need something specific, just click on any of the following links.
Table of Contents:
Let’s start off with a quick introduction to the function.
A Quick Introduction to Numpy Maximum
The Numpy maximum function computes the element-wise maximum of two input arrays or values in Python.
Having said that, it will also operate on a few other input types, like scalar values, and Numpy arrays of different types. I’ll explain more about these operations in the syntax section and I’ll show you examples in the examples section.
Numpy maximum is different from Numpy Max
One very quick note of caution:
The Numpy maximum function is different from the Numpy max function.
The Numpy max function identifies the maximum value in a Numpy array. So np.max typically takes a single Numpy array as an input, and will return the maximum value (although there are ways to use it where it will return maxima of the rows or columns).
In contrast, Numpy maximum (which we’re discussing in this tutorial) computes the element-wise maximum of two arrays. So np.maximum will typically take two Numpy arrays as an input, and will return an array with the element-wise maximum for each pair of values. (Although, there are some additional ways to use it, which I’ll cover in a moment.)
This is an important distinction to make. np.max and np.maximum work differently, so make sure you use the right one.
Ok. Now that we’ve talked about what Numpy maximum does, let’s look at the syntax.
The syntax of np.maximum
The syntax for Numpy maximum is simple:
Remember: this syntax assumes that you’ve imported Numpy with the alias np
.
Format of the input arrays
Here, I want to add a few notes about the format of the two inputs.
In the image above that explains the syntax, I’ve shown two inputs (i.e., arguments). In the syntax explanation, I’ve called these arguments arr1
and arr1
.
I’ve also noted that these are typically Numpy arrays.
Having said that, np.maximum will operate on a variety of inputs.
Specifically, np.maximum will work on:
- two same-sized Numpy arrays
- two numbers
- one number and one Numpy array
- one Numpy array, and another array with smaller dimensions
Additionally, np.maximum will operate on array-like objects, such as lists and tuples.
Output of np.maximum
The output of np.maximum is typically a new Numpy array with the element-wise maxima, although there are some caveats.
If the input is two scalar values, the output will be a scalar (instead of an array).
Additionally, if the inputs are a Numpy array and a lower-dimensional array, then np.maximum will produce a new array, but it will perform “broadcasting.” Broadcasting may be confusing to new Numpy users, so I’ll show an example in the examples section.
Additional parameters
In addition to the two input arrays, the Numpy maximum function has a two optional parameters:
- out
- where
These are somewhat rarely used, but so I won’t explain them in this tutorial.
Examples: how to use Numpy maximum on numbers and Numpy arrays
Now that we’ve looked at the syntax, let’s work through some examples of how to use the Numpy maximum function.
Examples:
- Use Numpy maximum on two scalars
- Use Numpy maximum with one array and one scalar
- Use Numpy maximum on two same-sized Numpy arrays
- Use Numpy maximum on differently sized Numpy arrays via broadcasting
Preliminary code: Import Numpy and Create Arrays
Before you run the examples, you’ll need to do a few things to get set up.
You’ll need to import Numpy.
You’ll also need to create some Numpy arrays that we can use in the examples.
Import Numpy
First, we need to import Numpy.
You can import Numpy with this following code:
import numpy as np
Create Arrays
Next, we need to create a few Numpy arrays that we can use in our examples.
Specifically, we will create three arrays:
- a 1-dimensional ‘vector’ of numbers
- a 2-dimensional ‘matrix’ of the numbers from 1 to 9 (ordered)
- a 2-dimensional ‘matrix’ of the numbers from 1 to 9 (randomized)
To make these, we’re going to use a few different Numpy functions.
We’ll use np.array to manually define the 1-dimensional vector of values (1, 2, and 3).
We’ll use Numpy arange to make an array with a sequence of numbers. This array will be called numbers_negative4_to_4
. (We won’t use this directly in our examples, but we will use it to create the next couple of arrays.)
We’ll create an an ordered 2D array of numbers called matrix_2d_ordered
. To create this, we’ll use Numpy reshape on numbers_negative4_to_4
.
And finally, we’ll make a randomized 2-dimensional array, called matrix_2d_random
. To create this array, we’ll use Numpy random choice on numbers_negative4_to_4
.
(If any of this confuses you, I recommend that you click on the links in the above paragraphs … they will explain how these functions work.)
vector_1d = np.array([1,2,3]) # CREATE 2D MATRIX OF NUMBERS, 1 TO 9 numbers_negative4_to_4 = np.arange(start = -4, stop = 5) matrix_2d_ordered = numbers_negative4_to_4.reshape((3,3)) # CREATE 2D MATRIX OF NUMBERS, 1 TO 9, RANDOMIZED np.random.seed(22) matrix_2d_random = np.random.choice(size = (3,3), a = numbers_negative4_to_4, replace = False)
Once you run all of this code, you’ll be ready to run the examples.
EXAMPLE 1: Use Numpy maximum on two scalars
Let’s start with a simple example.
Here, we’ll use Numpy maximum on two scalar values.
np.maximum(0,4)
OUT:
4
Explanation
This is very simple.
Here, we’re computing the maximum of 0 and 4, which is 4.
EXAMPLE 2: Use np.maximum on an array and a scalar
Next, we’ll use np.maximum on an array and a scalar.
np.maximum(matrix_2d_ordered, 0)
OUT:
array([[0, 0, 0], [0, 0, 1], [2, 3, 4]])
Explanation
The Numpy array matrix_2d_ordered
contains the numbers from -4 to 4, arranged in a 3×3 array.
Here, we’ve used np.maximum to compute the maximum of each element of matrix_2d_ordered
, and 0.
Some of the values of matrix_2d_ordered
are negative. For those values, np.maximum return 0. For the remaining values, it returns the original value of matrix_2d_ordered
.
(Note, this operation is essentially the relu function.
EXAMPLE 3: Use Numpy Maximum on two same-sized arrays
Next, let’ use np.maximum on two same-sized arrays.
Specifically, we’ll use np.maximum on matrix_2d_random
and matrix_2d_ordered
.
np.maximum(matrix_2d_ordered, matrix_2d_random)
OUT:
array([[ 4, -3, -1], [-1, 2, 1], [ 3, 3, 4]])
Explanation
Here, np.maximum is computing the element-wise maximum of matrix_2d_random
and matrix_2d_ordered
.
Additionally, notice the shape of the output. The output Numpy array is the same size and shape of the input arrays. This is how np.maximum behaves when we give it two same-sized arrays.
EXAMPLE 4: Use Numpy maximum on a 2D array and a 1D array (i.e., broadcasting)
Finally, let’s use np.maximum on a 1-dimensional Numpy array (i.e., a vector) and a 2-dimensional Numpy array (i.e., a matrix).
np.maximum(matrix_2d_ordered, vector_1d)
OUT:
array([[1, 2, 3], [1, 2, 3], [2, 3, 4]])
Explanation
In this example, we’ve “broadcasted” vector_1d
over the rows of matrix_2d_ordered
.
WTF is broadcasting?
Notice that both arrays have the same number of columns, but they have a different number of rows. This allows Numpy to use the values of the 1D array to operate on every row of the 2D array.
This is called “broadcasting.” Here, np.maximum is “broadcasting” the 1D array across the rows of the 2D array.
So np.maximum computes the element-wise maxima of the values of vector_1d
and row 1 of matrix_2d_ordered
. Then it computes the element-wise maxima of the values of vector_1d
and row 2 of matrix_2d_ordered
. And so on.
Remember that this is only possible because the number of elements in vector_1d
is the same as the number of columns in matrix_2d_ordered
. Broadcasting only operates properly when the the number of elements in both inputs are appropriately shaped.
Leave your other questions in the comments below
Do you have other questions about how to use np.maximum to compute the maximum values of Numpy arrays?
If so, leave your questions in the comments section below.
Join our course to learn more about Numpy
In this tutorial, I’ve explained how to compute element-wise maxima with np.maximum.
This should help you on some Numpy problems, but if you really want to learn Numpy, there’s a lot more to learn.
If you’re serious about mastering Numpy, and serious about data science in Python, you should consider joining our premium course called Numpy Mastery.
Numpy Mastery will teach you everything you need to know about Numpy, including:
- How to create Numpy arrays
- How to reshape, split, and combine your Numpy arrays
- What the “Numpy random seed” function does
- How to use the Numpy random functions
- How to perform mathematical operations on Numpy arrays
- and more …
Moreover, this course will show you a practice system that will help you master the syntax within a few weeks. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for.
Find out more here:
Learn More About Numpy Mastery
Thanks for visiting r-craft.org
This article is originally published at https://www.sharpsightlabs.com
Please visit source website for post related comments.