numpy
max
amax
maximum
Python

numpy max vs amax vs maximum

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

np.max, np.amax, and np.maximum sound similar, but they do different jobs. The short version is that np.max and np.amax reduce values to a single maximum along an axis, while np.maximum compares two arrays element by element.

np.max And np.amax Mean The Same Thing

For NumPy arrays, np.max is effectively an alias for np.amax. Both compute the largest value in the input, optionally along a chosen axis.

python
1import numpy as np
2
3arr = np.array([[1, 5, 2],
4                [7, 3, 4]])
5
6print(np.max(arr))
7print(np.amax(arr))
8print(np.max(arr, axis=0))
9print(np.amax(arr, axis=1))

This prints:

text
17
27
3[7 5 4]
4[5 7]

So if the question is whether there is a semantic difference between np.max and np.amax, the answer is no for normal usage. Pick the one you find clearer and use it consistently.

What Reduction Means Here

A reduction takes many values and collapses them into fewer values. With no axis argument, the whole array is reduced to one scalar maximum.

python
1import numpy as np
2
3arr = np.array([2, 9, 1, 6])
4print(np.max(arr))

With an axis, the reduction happens per row or per column.

python
1import numpy as np
2
3arr = np.array([[1, 5, 2],
4                [7, 3, 4]])
5
6print(np.max(arr, axis=0))
7print(np.max(arr, axis=1))

Understanding that max and amax are reductions helps separate them mentally from maximum, which is not a reduction.

np.maximum Is Element-Wise

np.maximum compares two arrays element by element and returns a new array of the larger values at each position.

python
1import numpy as np
2
3a = np.array([1, 8, 3])
4b = np.array([4, 2, 9])
5
6print(np.maximum(a, b))

Output:

text
[4 8 9]

That result is not the overall maximum of a and b. It is a pairwise comparison.

This makes np.maximum useful for clipping, masks, and element-wise combination logic.

python
1import numpy as np
2
3values = np.array([-3, 0, 7, -1])
4non_negative = np.maximum(values, 0)
5print(non_negative)

Output:

text
[0 0 7 0]

Broadcasting Makes maximum More Flexible

Because np.maximum is a universal function, it supports broadcasting.

python
1import numpy as np
2
3arr = np.array([[1, 2, 3],
4                [4, 5, 6]])
5
6print(np.maximum(arr, 4))

Output:

text
[[4 4 4]
 [4 5 6]]

This compares each array element against the scalar 4. That is a different operation from np.max(arr), which would simply return 6.

Where Python's Built-In max Fits In

Python also has the built-in max. It works on any iterable and can work on NumPy arrays, but it does not understand NumPy axes the same way.

python
1import numpy as np
2
3arr = np.array([[1, 5, 2],
4                [7, 3, 4]])
5
6print(max(arr))

This does not reduce the entire array numerically the way np.max does. Instead, it compares the row arrays according to Python's object comparison rules for the array iteration behavior. That is usually not what you want for numerical work.

For NumPy arrays, prefer NumPy functions.

Choosing The Right Function

Use np.max or np.amax when you want:

  • the largest value in an array
  • the largest value along an axis
  • a true reduction

Use np.maximum when you want:

  • element-wise comparison between two arrays
  • element-wise comparison between an array and a scalar
  • a result array with the same broadcasted shape as the inputs

A quick side-by-side example makes the difference obvious:

python
1import numpy as np
2
3a = np.array([1, 8, 3])
4b = np.array([4, 2, 9])
5
6print(np.max(a))
7print(np.maximum(a, b))

Output:

text
8
[4 8 9]

One returns a single maximum from a. The other returns three pairwise maxima.

Common Pitfalls

  • Treating np.maximum as if it were a reduction like np.max.
  • Using Python's built-in max on a multidimensional NumPy array and expecting axis-aware numeric behavior.
  • Forgetting the axis argument when you need row-wise or column-wise maxima.
  • Thinking np.max and np.amax are meaningfully different in normal NumPy code.
  • Overlooking broadcasting behavior when np.maximum is given arrays of different but compatible shapes.

Summary

  • 'np.max and np.amax are reduction functions and are effectively aliases.'
  • 'np.maximum performs element-wise comparison between two inputs.'
  • Use axis with np.max or np.amax for row-wise or column-wise results.
  • Use np.maximum when you want pairwise maxima, not a single reduced result.
  • For NumPy arrays, prefer NumPy's max functions over Python's built-in max.

Course illustration
Course illustration

All Rights Reserved.