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

2. Computing Maximum Along an Axis
We compute the maximum values along rows and columns of a 2D array.
</>
Copy
import numpy as np
# Define a 2D array
arr = np.array([[3, 7, 2],
[8, 1, 9],
[4, 6, 5]])
# Compute the maximum value along columns (axis=0)
max_col = np.max(arr, axis=0)
# Compute the maximum value along rows (axis=1)
max_row = np.max(arr, axis=1)
# Print results
print("Maximum values along columns:", max_col)
print("Maximum values along rows:", max_row)
Output:
Maximum values along columns: [8 7 9]
Maximum values along rows: [7 9 6]

3. Using the out
Parameter
Using an output array to store the maximum values instead of creating a new one.
</>
Copy
import numpy as np
# Define a 1D array
arr = np.array([15, 22, 8, 37, 50])
# Create an output array
output_array = np.empty_like(arr[0])
# Compute the maximum and store it in output_array
np.max(arr, out=output_array)
# Print the results
print("Maximum value using output array:", output_array)
Output:
Maximum value using output array: 50

4. Using the where
Parameter
Computing the maximum value only for specific elements using a condition.
</>
Copy
import numpy as np
# Define an array
arr = np.array([5, 15, 25, 35, 10])
# Define a condition (only consider values greater than 10)
condition = arr > 10
# Compute the maximum only for elements satisfying the condition
max_value = np.max(arr, where=condition, initial=0)
# Print the results
print("Maximum value considering condition:", max_value)
Output:
Maximum value considering condition: 35

The maximum value is computed only for elements where condition=True
. The initial value ensures a valid comparison when some elements are ignored.