NumPy fmax()

The numpy.fmax() function computes the element-wise maximum of two input arrays. Unlike numpy.maximum(), fmax() ignores NaN values whenever possible, returning the non-NaN value if available.

Syntax

</>
Copy
numpy.fmax(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1, x2array_likeArrays containing elements to compare. If their shapes differ, they must be broadcastable.
outndarray, None, or tuple of ndarray and None, optionalOptional output array for storing results. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying where to compute the maximum. Unselected elements retain original values.
castingstr, optionalDefines the casting behavior of the function.
orderstr, optionalMemory layout of the output array.
dtypedata-type, optionalSpecifies the desired data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array containing the element-wise maximum of x1 and x2. If both inputs are scalars, a scalar is returned.


Examples

1. Element-wise Maximum of Two Arrays

Finds the maximum value for each element between two arrays.

</>
Copy
import numpy as np

# Define two input arrays
x1 = np.array([3, 5, 7, 2])
x2 = np.array([4, 2, 8, 10])

# Compute the element-wise maximum
result = np.fmax(x1, x2)

# Print the results
print("Element-wise maximum:", result)

Output:

Element-wise maximum: [ 4  5  8 10]

2. Handling NaN Values in fmax()

Unlike numpy.maximum(), fmax() ignores NaN values whenever possible.

</>
Copy
import numpy as np

# Define two arrays with NaN values
x1 = np.array([np.nan, 3, np.nan, 7])
x2 = np.array([1, np.nan, 5, np.nan])

# Compute the element-wise maximum ignoring NaN
result = np.fmax(x1, x2)

# Print the results
print("Element-wise maximum (ignoring NaN):", result)

Output:

Element-wise maximum (ignoring NaN): [ 1.  3.  5.  7.]

3. Using the out Parameter

Storing the result in a pre-allocated output array.

</>
Copy
import numpy as np

# Define two input arrays
x1 = np.array([1, 4, 6, 9])
x2 = np.array([3, 2, 8, 5])

# Pre-allocate an output array
output_array = np.empty_like(x1)

# Compute element-wise maximum and store in output_array
np.fmax(x1, x2, out=output_array)

# Print the results
print("Element-wise maximum with output array:", output_array)

Output:

Element-wise maximum with output array: [3 4 8 9]

4. Using the where Parameter

Computing the element-wise maximum only for selected elements using a condition.

</>
Copy
import numpy as np

# Define two input arrays
x1 = np.array([2, 8, 1, 6])
x2 = np.array([5, 3, 7, 4])

# Define a mask (condition)
mask = np.array([True, False, True, False])

# Compute element-wise maximum only where mask is True
result = np.fmax(x1, x2, where=mask)

# Print the results
print("Element-wise maximum with condition:", result)

Output:

Element-wise maximum with condition: [5 0 7 0]

Only elements where mask=True are affected, while the others retain their original values.