Last chance! 50% off DataCamp Premium
Sale ends in
NumPy's `max()` function is a powerful tool for array computation and analysis, used to find the maximum value within an array or along a specified axis. It is essential for data analysis tasks where determining the largest value in a dataset is necessary. Notably, `np.max()` is an alias for the `amax()` function, which is the true underlying implementation.
The `max()` function is used to compute the maximum value of elements in a NumPy array either globally or along a specific axis. This function is useful in scenarios where identifying peak values in datasets can aid in understanding data distribution and making informed decisions.
numpy.max(array, axis=None, out=None, keepdims=False)
In this syntax, `array` is the input array, `axis` specifies the axis along which to find the maximum, and `out` is an optional parameter to store the result. The `keepdims` parameter, when set to `True`, retains reduced dimensions as dimensions with size one. By default, `axis=None`, `out=None`, and `keepdims=False`.
import numpy as np
array = np.array([1, 3, 2, 7, 4])
max_value = np.max(array)
print(max_value)
In this example, `np.max(array)` returns `7`, the maximum value in the entire one-dimensional array since no axis is specified.
import numpy as np
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
max_in_columns = np.max(matrix, axis=0)
print(max_in_columns)
Here, `np.max(matrix, axis=0)` computes the maximum values along each column, resulting in `[7, 8, 9]`.
max_in_rows = np.max(matrix, axis=1)
print(max_in_rows)
In this additional example, `np.max(matrix, axis=1)` computes the maximum values along each row, resulting in `[3, 6, 9]`.
import numpy as np
array = np.array([1, 10, 5, 8])
result = np.empty(1)
np.max(array, out=result)
print(result)
This example demonstrates using the `out` parameter to store the result, which returns the maximum value `10` in a pre-allocated array `result`. Note that `result` must have the same shape as the expected output.