NumPy ndarray.max()
The numpy.ndarray.max()
method returns the maximum value in a NumPy array.
It can operate across the entire array or along a specified axis.
Syntax
</>
Copy
ndarray.max(axis=None, out=None, keepdims=False, initial=<no value>, where=True)
Parameters
Parameter | Type | Description |
---|---|---|
axis | None, int, or tuple of ints, optional | Axis or axes along which the maximum is computed. If None , it computes the maximum of the entire array. |
out | ndarray, optional | Alternative output array for storing the result. Must have the same shape as expected output. |
keepdims | bool, optional | If True , the reduced dimensions are kept as size one, allowing proper broadcasting. |
initial | scalar, optional | Specifies the minimum value to start the comparison. |
where | array_like of bool, optional | Specifies elements to consider when computing the maximum. |
Return Value
Returns the maximum value in the array if axis=None
, or an array of maximum values if an axis is specified.
Examples
1. Finding the Maximum Value in an ndarray
In this example, we create a 2D array and find the maximum value.
</>
Copy
import numpy as np
# Creating a 2D array
arr = np.array([[3, 5, 7],
[2, 8, 1]])
# Finding the maximum value in the entire array
max_value = arr.max()
print("Maximum value in the array:", max_value)
Output:
Maximum value in the array: 8
2. Using the axis
Parameter in ndarray.max()
Here, we compute the maximum values along different axes.
</>
Copy
import numpy as np
# Creating a 2D array
arr = np.array([[3, 5, 7],
[2, 8, 1]])
# Finding the maximum value along axis 0 (columns)
max_axis0 = arr.max(axis=0)
print("Maximum values along columns:", max_axis0)
# Finding the maximum value along axis 1 (rows)
max_axis1 = arr.max(axis=1)
print("Maximum values along rows:", max_axis1)
Output:
Maximum values along columns: [3 8 7]
Maximum values along rows: [7 8]
3. Keeping Dimensions with keepdims=True
in ndarray.max()
In this example, we use keepdims=True
to retain the reduced axis as a dimension of size one.
</>
Copy
import numpy as np
# Creating a 2D array
arr = np.array([[3, 5, 7],
[2, 8, 1]])
# Finding the maximum value along axis 1 and keeping dimensions
max_keepdims = arr.max(axis=1, keepdims=True)
print("Maximum values along rows with keepdims=True:\n", max_keepdims)
Output:
Maximum values along rows with keepdims=True:
[[7]
[8]]
4. Using the initial
Parameter in ndarray.max()
The initial
parameter sets a baseline value for comparison.
</>
Copy
import numpy as np
# Creating an array
arr = np.array([3, 5, 7, 2, 8, 1])
# Finding the maximum value with an initial baseline
max_with_initial = arr.max(initial=10)
print("Maximum value with initial=10:", max_with_initial)
Output:
Maximum value with initial=10: 10
5. Using the where
Parameter in ndarray.max()
The where
parameter allows computing the maximum only over specified elements.
</>
Copy
import numpy as np
# Creating an array
arr = np.array([3, 5, 7, 2, 8, 1])
# Creating a mask to specify selected elements
mask = np.array([True, False, True, False, True, False])
# Finding the maximum value considering only selected elements
max_where = arr.max(initial=0, where=mask)
print("Maximum value where mask is True:", max_where)
Output:
Maximum value where mask is True: 8