How to Use Numpy Maximum on Arrays?


At the second position, np.maximum returns 9 from the pair 8 and 9. That position-by-position result catches my attention: each comparison stays tied to its matching pair instead of reducing the whole array to one maximum.

For your two arrays, inspect the result from np.maximum. Compare its shape with the inputs as you read the result.

What np.maximum returns

NumPy’s maximum function compares two inputs element by element and returns the larger value at each corresponding position. Call np.max to reduce one array to a single maximum.

PositionLeftRightResult
1424
2899
3333
4515

The result keeps the shape produced by broadcasting the two inputs. NumPy can compare arrays of different shapes when their dimensions are compatible.

With dtype=int, the input arrays convert decimal values to integers before np.maximum compares them.

Element-wise maximum for two arrays with shape one by four
Both inputs have shape (1, 4). Decimal values are converted to integers before comparison.

Prepare arrays with compatible shapes

Pass two array-like inputs, such as NumPy arrays or a scalar paired with an array. Equal shapes line up directly, and different shapes must satisfy NumPy’s broadcasting rules.

  • Compare shapes from their trailing dimensions. Each pair must be equal or one of the dimensions must be 1.
  • The output has the common broadcast shape, which can be larger than either input.
  • Choose the output data type before comparison. Converting decimal inputs to integers truncates them as the arrays are created.

The NumPy broadcasting rules explain which dimensions fit. NumPy raises a ValueError when a pair of shapes is incompatible.

Compare values at matching positions

For two one-dimensional arrays of equal length, call np.maximum with the left and right inputs. Each index in the result contains the larger value from that pair.

import numpy as np

left = np.array([4, 8, 3, 5])
right = np.array([2, 9, 3, 1])
print(np.maximum(left, right))

The result is [4, 9, 3, 5]. Equal values stay unchanged, as the third pair shows.

Compare multidimensional arrays

The same call works across rows when both inputs have matching shapes. NumPy compares each pair at the same row and column.

import numpy as np

left = np.array([[1.2, 3.4, 6.7, 8.9],
                 [9.8, 7.6, 5.4, 3.2]], dtype=float)
right = np.array([[2.1, 4.3, 5.7, 6.9],
                  [2.1, 8.6, 4.5, 1.2]], dtype=float)
print(np.maximum(left, right, dtype=float))

The output has the same two-row, four-column shape. The first row compares 6.7 with 5.7, and the second row selects 8.6 from the pair 7.6 and 8.6.

Element-wise maximum across two arrays with shape two by four
Each result comes from the values at the same row and column in the two arrays.

Use np.maximum when each position needs its own comparison. If you want the largest value from a single array instead, use a reduction such as np.max.

Broadcast compatible shapes

Broadcasting applies a smaller input across compatible positions during the element-wise operation. A three-by-one array and a three-value vector produce a three-by-three result.

import numpy as np

rows = np.array([[4], [8], [3]])
limits = np.array([2, 9, 1])
result = np.maximum(rows, limits)
print(result)
  • rows has shape (3, 1), so its trailing dimension is 1.
  • limits has shape (3,), which has one trailing dimension of size 3.
  • np.maximum produces shape (3, 3), with a comparison for each row and each limit.

NumPy aligns dimensions from the right, so the trailing sizes 1 and 3 can broadcast. The resulting rows compare against all three values in limits.

I tested arrays with trailing sizes 2 and 3, and np.maximum raised a ValueError before returning a result.

Keep masked output values defined

The where argument selects which positions np.maximum computes. When you pass where without out, positions marked False in the new output remain uninitialized, so their values are not safe to read.

python3 -c 'import numpy as np; left=np.array([1,8,3,5]); right=np.array([4,9,7,1]); mask=np.array([True,False,True,False]); out=left.copy(); np.maximum(left,right,where=mask,out=out); print(out)'

I initialize out from the left input because False positions keep their starting values. The result is [4, 8, 7, 5], with 8 and 5 preserved at the False positions.

Output array with masked positions preserved by an initialized out array
The False mask positions keep their values from the preinitialized output array.

Choose comparison or reduction

np.maximum compares two inputs, and np.max reduces one array to its overall maximum or a maximum along an axis. np.maximum propagates a NaN.

np.fmax returns the numeric value when only one input is NaN.

FunctionInputs and resultNaN handling
np.maximumTwo inputs, element-wise resultPropagates NaN
np.fmaxTwo inputs, element-wise resultIgnores one NaN when paired with a number
np.maxOne array, maximum overall or along an axisPropagates NaN
np.nanmaxOne array, maximum overall or along an axisIgnores NaNs. All-NaN slices warn and return NaN
import numpy as np

left = np.array([np.nan, 7.0])
right = np.array([3.0, np.nan])
print(np.maximum(left, right))
print(np.fmax(left, right))
print(np.max(np.array([3, 7, 2])))
print(np.maximum(np.array([3, 7, 2]), np.array([1, 2, 9])))

I compared the same NaN pairs with both functions. np.maximum returned NaN at both positions, and np.fmax returned the numeric partner, which is useful when one missing value should be ignored.

Use the NumPy minimum function when each position needs the smaller value instead.

Apply one threshold to every value

A scalar uses the same broadcasting rule, so it can set a floor for an array. Each negative value is compared with zero independently.

Try the cutoff you need, then check that the output shape matches the array you will use next.

import numpy as np

values = np.array([-2.0, 1.5, -0.4])
print(np.maximum(values, 0))

Frequently asked questions

When a call feeds another operation, confirm the result shape and decide where the computed values should be stored.

Does np.maximum modify either input array?

When you omit out, np.maximum returns a new result array and leaves both inputs unchanged. If you pass an array to out, NumPy stores the result there, so that destination array changes.

What shape does np.maximum return?

The result has the common broadcast shape of its two inputs. With equal-shaped arrays, that shape matches both inputs. With compatible different shapes, the result can be larger.

Arulius Savio
Arulius Savio
Articles: 56