Python: Get the Maximum value in a NumPy Array

To get the maximum value of all the elements in a NumPy array, use the numpy.max() function. Pass the array to evaluate as the first argument of the function and store the result in a variable.

 

Syntax

Here is the syntax of the .max() function from the NumPy Python package:

 

max_val = numpy.max(arr)

 

NumPy Array Max Value Example

To demonstrate how this works, let's create a NumPy array with random number data, find the maximum value then print the result.

 

import numpy as np

arr = np.random.randint(9, size=(3,4))

max_val = np.max(arr)

print('Max value: ', max_val)

print('Array: \n', arr)
Max value:  8
Array:
[[5 7 6 2]
[5 8 2 2]
[6 7 0 1]]

 

Note – the above method will work for both integers and floating-point numbers.

numpy