NumPy maximum()
The numpy.maximum()
function compares two input arrays element-wise and returns a new array containing the maximum values at each index.
If an element in either array is NaN
, it is propagated in the output.
Syntax
</>
Copy
numpy.maximum(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1, x2 | array_like | Input arrays to be compared. If their shapes differ, they must be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying where the computation should take place. Elements where where=False retain their original values. |
casting | str, optional | Specifies the casting rule when performing the operation. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Specifies the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the element-wise maximum values of x1
and x2
. If both inputs are scalars, a scalar is returned.
Examples
1. Element-wise Maximum of Two Arrays
Comparing two NumPy arrays element-wise to return the maximum values.
</>
Copy
import numpy as np
# Define two input arrays
x1 = np.array([1, 4, 3, 7, 5])
x2 = np.array([2, 3, 5, 6, 4])
# Compute the element-wise maximum
result = np.maximum(x1, x2)
# Print the result
print("Element-wise maximum:", result)
Output:
Element-wise maximum: [2 4 5 7 5]

2. Handling NaN Values
When NaN
values are present, they are propagated in the result.
</>
Copy
import numpy as np
# Define two arrays with NaN values
x1 = np.array([1.0, np.nan, 3.0, np.nan])
x2 = np.array([2.0, 2.0, np.nan, 4.0])
# Compute the element-wise maximum
result = np.maximum(x1, x2)
# Print the result
print("Element-wise maximum with NaNs:", result)
Output:
Element-wise maximum with NaNs: [ 2. nan 3. nan]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define two input arrays
x1 = np.array([10, 20, 30])
x2 = np.array([15, 10, 35])
# Create an output array with the same shape
output_array = np.empty_like(x1)
# Compute element-wise maximum and store in output_array
np.maximum(x1, x2, out=output_array)
# Print the results
print("Stored in output array:", output_array)
Output:
Stored in output array: [15 20 35]

4. Using the where
Parameter
Applying a condition to selectively compute the maximum values.
</>
Copy
import numpy as np
# Define two input arrays
x1 = np.array([10, 5, 20, 15])
x2 = np.array([8, 25, 18, 12])
# Define a condition mask (True means comparison will be applied)
mask = np.array([True, False, True, False])
# Compute element-wise maximum where mask is True
result = np.maximum(x1, x2, where=mask)
# Print the results
print("Computed maximum with mask:", result)
Output:
Computed maximum with mask: [10 0 20 0]

Here, the maximum values are computed only for elements where mask=True
.