How to Use the Numpy maximum() Function in Python

09/28/2021
Contents
In this article, you will learn how to use the Numpy maximum() function in Python.
Numpy maximum() Function
The NumPy maximum() function is used to compute the element-wise maximum of two arrays. The function returns a new array that contains the maximum value from each pair of corresponding elements in the input arrays.
Syntax
Here’s the syntax for the NumPy maximum() function:
numpy.maximum(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Parameters
x1
andx2
: The input arrays to be compared.out
: An optional output array in which to place the results. If not specified, a new array is created.where
: An optional Boolean array that specifies which elements to include in the computation.casting
: An optional string that specifies the typecasting rules to use when computing the result.order
: An optional string that specifies the memory layout of the output array.dtype
: An optional data type for the output array.subok
: An optional Boolean flag that specifies whether to return a subclass of the input arrays if possible.
Example
Here’s an example of how to use the NumPy maximum() function to compute the element-wise maximum of two arrays:
import numpy as np
x1 = np.array([1, 2, 3, 4])
x2 = np.array([3, 1, 4, 2])
result = np.maximum(x1, x2)
print(result)
Output:
[3 2 4 4]
In this example, the maximum value from each pair of corresponding elements in the input arrays x1 and x2 is computed and stored in the result array.