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
| Parameter | Type | Description |
|---|---|---|
a | array_like | Input array for which the maximum value is computed. |
axis | None, int, or tuple of ints, optional | Axis or axes along which to compute the maximum. If None, the function operates on the flattened array. |
out | ndarray, optional | Alternative output array to store the result. Must have the same shape as the expected output. |
keepdims | bool, optional | If True, the reduced dimensions are retained with size one, allowing for proper broadcasting. |
initial | scalar, optional | Specifies an initial maximum value to consider, useful for handling empty slices. |
where | array_like of bool, optional | Specifies 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
