How to Calculate Maximum of Array in NumPy

Python NumPy `maximum()` or `max()` function is used to get the maximum value (greatest value) of a given array, or compare the two arrays element-wise and returns the maximum values. While comparing, one of the elements of two arrays is a NaN, then that element is returned as NaN. If both elements of two arrays are NaNs then the first element is returned. Use the minimum() function to get the minimum values of NumPy array.

In this article, I will explain how to get the maximum values of 1-D arrays in element-wise and multi-D arrays along with specified axis using syntax and parameters of `numpy.maximum()` with examples.

1. Quick Examples of Python NumPy Maximum Function

If you are in a hurry, below are some quick examples of how to calculate the max value of Python NumPy array using maximum() function.

``````
# Below are the quick examples

# Example 1: Find maximum value on 1-dimensional numpy array
arr = np.array([16,10,96,32,50,64,85])
arr2 = np.max(arr)

# Example 2: find maximum value
arr = 15
arr1 = 24
arr2 = np.maximum(arr, arr1)

# Example 3: Find maximum value on 2-dimensional numpy array
arr = [22,34,88,99]
arr1 = [35,43,55,78]
arr2 = np.maximum(arr,arr1)

# Example 4: Get the maximum value from complete 2D NumPy array
arr2 = np.max(arr)

# Example 5: Get the maximum values of each row i.e. along axis 0
arr2 = np.max(arr, axis=0)

# Example 6: Get the maximum values of each row i.e. along axis 1
arr2 = np.max(arr, axis=1)

# Example 7: Use numpy.maximum() Function & NaN
arr = np.array([4,9,14,np.nan,16])
arr1 = np.array([np.nan,9,18,np.nan,25])
arr2 = np.maximum(arr,arr1)
``````

2. Syntax of maximum()

Syntax of NumPy maximum()

``````
# syntax of numpy.maximum()
numpy.maximum(arr, arr1, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) =
``````

2.1 Parameters of the maximum()

Following are the parameters of maximum().

• `arr`, `arr1` – Input arrays. The arrays which contain elements to compare maximum values
• `out` – Represents the location into which the result is stored.
• `where` – This condition is broadcast over the input, at locations where the condition is True.
• `casting` – Controls the type of datacasting that should occur.
• `order` – Controls the memory layout order of the output function.
• `dtype` – Represents the desired data type of the array.
• `subok `– Decides if subclasses should be made or not. If True, subclasses will be passed through.

2.2 Return Value of maximum()

It returns the maximum of arr1 and arr2 arrays, element-wise. This returns a scalar if both arr1 and arr2 are scalars.

3. Usage of Numpy maximum()

NumPy `maximum()` function is used to get a new array that contains element-wise maximum values of two arrays. It compares two arrays and returns a new array containing the maximum values. If one of the elements being compared is `NaN` (Not a Number), `NaN` is returned. If both elements being compared are `NaN` (Not a Number), then `NaN` is returned.

3.1 Get Maximum Value of Two Scalars

If we use `maximum()` function to compare two scalar values, it will return the maximum scalar value of two scalars. For example,

``````
import numpy as np
arr = 15
arr1 = 24

# find maximum value
arr2 = np.maximum(arr, arr1)
print ("maximum of 15 and 24 : ", arr2)

# Output
# maximum of 15 and 24 :  24
``````

3.2 Get Max Value of 1-D Array

The following example demonstrates how to get the maximum value of 1-D NumPy array using `max()`. Let’s create an NumPy array using array() function and pass an array as input to the function. For example, here I am using max() function.

``````
# Create an array
arr = np.array([16,10,96,32,50,64,85])

# Find maximum value of 1-D numpy array
arr1 = np.max(arr)
print('Maximum value in arr: ', arr1)

# Output
# Maximum value in arr:  96
``````

4. Get a Maximum of Two NumPy Arrays

When comparing two 1-D NumPy arrays element-wise it will return the maximum/max values of two arrays element-wise. Let’s take an example,

``````
arr = [22,34,88,99]
arr1 = [35,43,55,78]

# Get maximum values of two 1-D numpy arrays
arr2 = np.maximum(arr,arr1)
print(arr2)

# Output
# [35 43 88 99]
``````

5. Get the Maximum Values of 2-D Array

Even if we don’t specify the axis value of the 2-D array to this function, it returns the maximum element present in the NumPy array. If you want to get a column-wise maximum value, You will pass `axis=0` in max() then it returns an array containing the max value for each `column`. If you pass `axis=1` then it returns an array containing the max value for each` row`.

``````
# Create 2-D NumPy array
arr = np.array([[15,28,57],
[99,65,34],
[39,68,69],
[37,55,88]])

# Get the maximum value of 2D NumPy array
arr1 = np.max(arr)
print(arr1)

# Output
# 99

# Get the maximum values of each column
arr1 = np.max(arr, axis=0)
print(arr1)

# Output
# [99 68 88]

# Get the maximum values of each row
arr1 = np.max(arr, axis=1)
print(arr1)

# Output
# [57 99 69 88]
``````

6. Use maximum() Function & NaN

If there is a NaN in the given NumPy array then it will return NaN as the maximum value. Both elements of two arrays are having NaNs then which return the first element. If you want to ignore the NaNs while finding the max values then use `numpy.nanmax()` instead.

``````
arr = np.array([4,9,14,np.nan,16])
arr1 = np.array([np.nan,9,18,np.nan,25])

# Use numpy.maximum() Function & NaN
arr2 = np.maximum(arr,arr1)
print(arr2)

# Output
# [nan  9. 18. nan 25.]
``````

7. Conclusion

In this article, I have explained how to get the maximum values (greatest values) of Numpy arrays using a `maximum()` and `max()` functions with examples. While comparing, one of the elements of two arrays is a NaN, then that element is returned as NaN. If both elements of two arrays are NaNs then the first element is returned.

Happy Learning!!

17 Shares