numpy.amax() function is an alias for numpy.max() function.

NumPy amax()

The numpy.amax() function returns the maximum value of an array or computes the maximum along a specified axis.

Syntax

</>
Copy
numpy.amax(a, axis=None, out=None, keepdims=False, initial=None, where=True)

Parameters

ParameterTypeDescription
aarray_likeInput array for which the maximum value is computed.
axisNone, int, or tuple of ints, optionalAxis or axes along which to compute the maximum. If None, the function operates on the flattened array.
outndarray, optionalAlternative output array to store the result. Must have the same shape as the expected output.
keepdimsbool, optionalIf True, the reduced dimensions are retained with size one, allowing for proper broadcasting.
initialscalar, optionalSpecifies an initial maximum value to consider, useful for handling empty slices.
wherearray_like of bool, optionalSpecifies 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.amax(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.amax(arr, axis=0)

# Compute the maximum value along rows (axis=1)
max_row = np.amax(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.amax(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.amax(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.