NumPy - Max



What is the Max?

In mathematics, the "max" (maximum) refers to the largest value in a set of numbers. It identifies the greatest element, providing a measure of the highest point in a data set.

For example, in the set {3, 1, 4, 2}, the maximum is 4. The maximum is useful for understanding the upper bound of a data set.

The NumPy max() Function

The max() function in NumPy returns the largest value in an array. It can be applied to the entire array or along a specified axis to find the maximum value in each row or column.

You can also use the amax() function, which is an alias for max() function. Following is the basic syntax of the max() function in NumPy −

numpy.max(a, axis=None, out=None, keepdims=False)

Where,

  • a: The input array or dataset from which the maximum value is to be found.
  • axis: Specifies the axis along which the maximum value is computed. If None (default), the maximum value is computed over the entire array.
  • out: This allows you to specify a location where the result will be stored. If None (default), the result is returned as a new array.
  • keepdims: If True, the reduced dimensions are retained in the result, making it easier for broadcasting. If False (default), the result is squeezed.

Understanding the Max Calculation

The calculation of the maximum value in a dataset is simple and easy. The function scans through all the elements in the array and identifies the largest value. This process can be applied to arrays of any shape or size.

Example

Let us understand this concept with an example −

import numpy as np

data = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])

# Calculating the maximum value
max_value = np.max(data)

print("Maximum value:", max_value)

This will produce the following result −

Maximum value: 9

Computing Max along Different Axes

In NumPy, the axis parameter allows you to compute the maximum value along specific axes of a multi-dimensional array. The axis parameter refers to the direction along which the maximum value should be calculated. For example, in a 2D array −

  • axis=0: Calculate the maximum value along the columns (vertical axis).
  • axis=1: Calculate the maximum value along the rows (horizontal axis).

Example

In the following example, we are computing the maximum value along both axes of the 2D array −

import numpy as np

# Create a 2D array
data_2d = np.array([[1, 3, 5], [2, 4, 6], [7, 8, 9]])

# Calculate the maximum value along axis 0 (columns)
max_axis_0 = np.max(data_2d, axis=0)

# Calculate the maximum value along axis 1 (rows)
max_axis_1 = np.max(data_2d, axis=1)

print("Maximum value along axis 0:", max_axis_0)
print("Maximum value along axis 1:", max_axis_1)

In the output below, the maximum value along axis 0 is computed by finding the largest element in each column. The maximum value along axis 1 is calculated by finding the largest element in each row −

Maximum value along axis 0: [7 8 9]
Maximum value along axis 1: [5 6 9]

Max for Higher-Dimensional Arrays

The numpy.max() function also works for arrays with more than two dimensions. You can specify the axis along which to calculate the maximum value, and the function will return the maximum value for that axis while retaining the other dimensions. If no axis is specified, the maximum value is calculated over the entire array.

Example

Following is an example to compute maximum value of a 3D array −

import numpy as np

# Create a 3D array
data_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Maximum value along axis 0
max_3d_axis_0 = np.max(data_3d, axis=0)

# Maximum value along axis 1
max_3d_axis_1 = np.max(data_3d, axis=1)

# Maximum value along axis 2
max_3d_axis_2 = np.max(data_3d, axis=2)

print("Maximum value along axis 0:", max_3d_axis_0)
print("Maximum value along axis 1:", max_3d_axis_1)
print("Maximum value along axis 2:", max_3d_axis_2)

In this case, the maximum value is calculated along each of the axes (0, 1, and 2) for the 3D array. The function returns the maximum values for each of the specified axes while preserving the other dimensions −

Maximum value along axis 0: [[5 6]
 [7 8]]
Maximum value along axis 1: [[3 4]
 [7 8]]
Maximum value along axis 2: [[2 4]
 [6 8]]

Handling NaN (Not a Number) Values

Sometimes, arrays may contain NaN (Not a Number) values, which can interfere with the calculation of the maximum value. To handle NaN values, NumPy provides an option to ignore them during the max calculation.

You can use the numpy.nanmax() function, which computes the maximum value while ignoring NaN values.

Example

In this example, we are handling NaN values while computing maximum value in NumPy −

import numpy as np

# Create an array with NaN values
data_with_nan = np.array([1, 3, np.nan, 5, 7])

# Calculate the maximum value while ignoring NaN values
max_without_nan = np.nanmax(data_with_nan)

print("Maximum value without NaN:", max_without_nan)

After executing the above code, we get the following output −

Maximum value without NaN: 7.0

Using the Out Parameter

The out parameter in the numpy.max() function allows you to store the result of the maximum value computation in a pre-allocated array.

This can be useful for memory management and efficiency when working with large datasets. The result is stored in the array specified by the out parameter, which must have the same shape as the expected output.

Example

In this example, the maximum value of the array data is calculated and stored in the pre-allocated array out_array, which is then printed to show the result −

import numpy as np

# Create an array
data = np.array([5, 2, 9, 1, 5, 6])

# Create an output array
out_array = np.empty((), dtype=np.int32)

# Calculate the maximum value and store it in out_array
np.max(data, out=out_array)

print("Output array:", out_array)

The result produced is as follows −

Output array: 9
Advertisements