🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.max()

AI & DATA SCIENCE // np-max

np.max() returns the largest value in an array, either over the entire array or along a specified axis.

Syntax

np.max(arr, axis=None)
arr.max()

Deep Dive Course

np.max() mirrors np.min() exactly, but for the largest value — it works over the whole array by default, or along a specified axis for multi-dimensional arrays, collapsing that axis into an array of per-position maximums. The element-wise, two-array counterpart is np.maximum(a, b), which compares two arrays position by position and keeps the larger value at each one, distinct from a single-array reduction.

1Understanding np.max()

np.max() mirrors np.min() exactly, but for the largest value — it works over the whole array by default, or along a specified axis for multi-dimensional arrays, collapsing that axis into an array of per-position maximums. The element-wise, two-array counterpart is np.maximum(a, b), which compares two arrays position by position and keeps the larger value at each one, distinct from a single-array reduction.

💡

Just like with min, use np.maximum(a, b) for an element-wise comparison between two arrays, and reserve np.max() for finding the largest value within, or along an axis of, a single array.

editor.html
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.max(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.max() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 5], [3, 2]])
print(np.max(matrix, axis=1))
localhost:3000

3Best Practices

Follow these guidelines when working with np.max():

1. Use np.max()/arr.max() for the overall or per-axis largest value within one array

2. Use np.maximum(a, b) instead when you need an element-wise comparison between two separate arrays

3. Use np.nanmax() when NaN values in the data should be ignored rather than causing the result to be NaN

⚠️

Tip: Just like with min, use np.maximum(a, b) for an element-wise comparison between two arrays, and reserve np.max() for finding the largest value within, or along an axis of, a single array.

editor.html
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.max(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.max(arr))
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 5], [3, 2]])
print(np.max(matrix, axis=1))

Best Practices

  • Use np.max()/arr.max() for the overall or per-axis largest value within one array
  • Use np.maximum(a, b) instead when you need an element-wise comparison between two separate arrays
  • Use np.nanmax() when NaN values in the data should be ignored rather than causing the result to be NaN

Interview Question

For a 2D array, what does np.max(matrix, axis=1) actually compute?

Hint: Think about which dimension axis=1 refers to, and which one gets collapsed.

axis=1 refers to the column dimension, so specifying it tells NumPy to find the maximum across the columns for each row, collapsing that row's values down to a single maximum. The result is a 1D array with one value per row, the largest value found within that specific row, rather than a single overall maximum for the whole matrix or a per-column result, which is what axis=0 would give instead.

Exercises

MediumPractice using np.max() in a real scenario.
View Solution
import numpy as np

arr = np.array([5, 2, 8, 1, 9])
print(np.max(arr))

Frequently Asked Questions

For a 2D array, what does np.max(matrix, axis=1) actually compute?

axis=1 refers to the column dimension, so specifying it tells NumPy to find the maximum across the columns for each row, collapsing that row's values down to a single maximum. The result is a 1D array with one value per row, the largest value found within that specific row, rather than a single overall maximum for the whole matrix or a per-column result, which is what axis=0 would give instead.

Related Functions

np-minnp-argmaxnp-clip