np.max function in Python NumPy

In this NumPy tutorial, I will explain the np.max function in Python NumPy, its syntax, and parameters. I will explain different examples of the np.max function in Python. I will also explain what the np.maximum function is.

To find the maximum value in a Python NumPy array, We use the np.max function is a function used . It can also be used along a specified axis in multi-dimensional arrays. whereas the np.maximum is used to compare two arrays and find the maximum value at each position when comparing element by element.

np.max function in Python NumPy

The np.max is a function in the NumPy Python library that returns the maximum value from an array or along a specified axis of a multidimensional array. It is a shorthand for NumPy’s amax function and is widely used in data manipulation and scientific computing tasks.

Syntax of np.max

The syntax of the np.max function in Python NumPy is:

``numpy.max(array, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)``

Parameters required for the numpy.max

Here, is the list of all the parameters required in the Python numpy.max functions

np max function in Python NumPy use cases

There can be many different use cases of the np.max function in Python NumPy. Let’s see some of them in detail:

Case 1: max NumPy function on a 1D array

We can find the maximum between the values stored in a 1D array in Python through the numpy.max() function.

Example: Here’s how to find the maximum value in a one-dimensional array

``````import numpy as np

arr = np.array([1, 3, 2, 5, 4])
max_value = np.max(arr)
print("The maximum value is:", max_value)``````

Output: The implementation of the code is as mentioned below:

``The maximum value is: 5``

This example finds the maximum value using the np.max function in Python NumPy in a 1D array.

Case 2: NumPy max value in matrix 2D

It is the basic usage of np.max function in Python NumPy without any additional parameters. we can easily find the maximum value element in the entire multi-dimensional array.

Example: Let’s take a multi-dimensional array in Python of numbers, and now we have to find the maximum of numbers through the np.max function.

``````import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
max_value = np.max(matrix)
print("The maximum value among the matrix is:", max_value)``````

Output: The output is mentioned below:

``The maximum value among the matrix is: 9``

This way, we can simply find the largest element in the entire multi-dimensional array through the np.max function in Python NumPy.

Case 3: np.max function in Python along an Axis

The axis parameter is used to specify the axis along which to look for the maximum values. For a 2D array in Python, axis=0 refers to columns, and axis=1 refers to rows.

Example: For instance, we have a 2D array and must find the max values within a row and columns through NumPy max.

``````import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
max_in_columns = np.max(matrix, axis=0)
max_in_rows = np.max(matrix, axis=1)

print("Maximum value in each column:", max_in_columns)
print("Maximum value in each row:", max_in_rows)``````

Output: The implementation of the code is mentioned below:

``````Maximum value in each column: [7 8 9]
Maximum value in each row: [3 6 9]``````

We can use the axis parameter in the np.max function Python NumPy.

Case 4: NumPy max function with keepdims

The keepdims parameter lets us retain the dimensions of the original array in Python.

Example: Here, we have an array trying a create a new array with the max value of the actual array through Python.

``````import numpy as np

matrix = np.array([[1, 2], [3, 4]])
max_value = np.max(matrix, keepdims=True)

print("The maximum value with original dimensions kept:")
print(max_value)``````

Output: After implementation, the output we get is mentioned below with a screenshot.

``````The maximum value with original dimensions kept:
[[4]]``````

We can use the np.max function in Python NumPy with the keepdims parameter.

Case 5: NumPy max value with Initial Parameter

The initial parameter is useful for specifying the minimum starting value for comparison, which is especially helpful for empty slices.

Example: We have an array with different values and we don’t know whether our array contains a value greater than a specific number or not. To check this we can use the initial parameter.

``````import numpy as np

arr = np.array([1, 2, 3, -1, -2, -3])
max_value = np.max(arr, initial=1)

print("The maximum value considering the initial value is:", max_value)``````

Output: The implementation of the given Python code is mentioned below:

``The maximum value considering the initial value is: 3``

We can use the np.max function in Python NumPy with an initial parameter.

np.maximum in Python

The np.maximum is a function used to compare two arrays and find the element-wise maximum in Python. It broadcasts the arrays against each other if they do not have the same shape. It returns a NumPy array with the maximum values picked from the input arrays for each position.

Example: Say, we have two different arrays in Python and we have to find the maximum of each column.

``````import numpy as np
a = np.array([1, 2, 3])
b = np.array([3, 1, 2])
result = np.maximum(a, b)
print(result)``````

Output: The implementation of the code is given below:

``[3 2 3]``

This way we can compare the elements of two arrays and find the maximum values of all, using np.maximum in Python.

Conclusion

The np.max function in Python is a versatile tool for finding the maximum values within arrays of any dimension. It becomes even more powerful when we leverage parameters like axis, keepdims, where, and initial to fine-tune the function’s behavior according to specific needs.

Through the examples provided, we can see how np.max() can be applied in different situations, making it an essential function for data manipulation and analysis in Python.

You may also like to read these Python NumPy tutorials: