NumPy max()

The numpy.max() function returns the maximum value of an array or computes the maximum along a specified axis.

Syntax

</>
Copy
numpy.max(a, axis=None, out=None, keepdims=False, initial=None, where=True)

Parameters

ParameterTypeDescription
aarray_likeInput array for which the maximum value is computed.
axisNone, int, or tuple of ints, optionalAxis or axes along which to compute the maximum. If None, the function operates on the flattened array.
outndarray, optionalAlternative output array to store the result. Must have the same shape as the expected output.
keepdimsbool, optionalIf True, the reduced dimensions are retained with size one, allowing for proper broadcasting.
initialscalar, optionalSpecifies an initial maximum value to consider, useful for handling empty slices.
wherearray_like of bool, optionalSpecifies elements to consider when computing the maximum.

Return Value

Returns the maximum value of the input array as a scalar if axis=None. If an axis is specified, an array with reduced dimensions is returned.


Examples

1. Finding the Maximum Value in an Array

Here, we compute the maximum value from a 1D array.

</>
Copy
import numpy as np

# Define an array
arr = np.array([10, 25, 3, 99, 50])

# Compute the maximum value in the array
max_value = np.max(arr)

# Print the result
print("Maximum value in the array:", max_value)

Output:

Maximum value in the array: 99