• Post author:
• Post category:NumPy / Python

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 return 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 axes using syntax and parameters of `numpy.maximum()` with examples.

## 1. Quick Examples of Calculate Maximum of Array

If you are in a hurry, below are some quick examples of how to calculate maximum of array.

``````
# Quick examples of calculate maximum of array

import numpy as np

# Example 1: Get the maximum value
arr = 15
arr1 = 24
max_value = np.maximum(arr, arr1)

# Example 2: Get the maximum value
# Of 1-D numpy array
arr = np.array([16,10,96,32,50,64,85])
max_value = np.max(arr)

# Example 3: Get the element-wise
# Maximum of the two arrays
arr = [22,34,88,99]
arr1 = [35,43,55,78]
max_value = np.maximum(arr,arr1)

# Example 4: Get the maximum value
# In the entire array
arr = np.array([[15,28,57],
[99,65,34],
[39,68,69],
[37,55,88]])
max_value = np.max(arr)

# Example 5: Get the maximum values
# Along columns (axis=0)
max_values = np.max(arr, axis=0)

# Example 6: Get the maximum values
Along rows (axis=1)
max_values = 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])
max_values = 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), the result for that pair is also `NaN`. Similarly, if both elements being compared are `NaN`, the result is `NaN`.

### 3.1 Get Maximum Value of Two Scalars

To find the maximum value between two scalar variables `arr` and `arr1`. In this case, `arr` has a value of `15`, and `arr1` has a value of `24`.

In this program, `np.maximum()` compares the two scalar values `arr` and `arr1` and returns an array-like object containing the maximum value, which is 24 in this case

``````
# Import numpy
import numpy as np

# Initialize variables
arr = 15
arr1 = 24

# Get maximum value of two scalars
max_value = np.maximum(arr, arr1)
print ("After getting the maximum value is:", max_value)
``````

Yields below output.

### 3.2 Get Max Value of 1-D Array

If you have a 1-dimensional array and you want to find the maximum value in that array, you can use the `numpy` library in Python. For instance, `np.max()` function is used to find the maximum value in the 1-dimensional NumPy array `arr`.

``````
import numpy as np

# Create a 1-D array
arr = np.array([16,10,96,32,50,64,85])

# Get the maximum value of 1-D numpy array
max_value = np.max(arr)
print ("After getting the maximum value in the array:", max_value)
``````

Yields below output.

## 4. Get a Maximum of Two NumPy Arrays

Similarly, you can also find the element-wise maximum of two NumPy arrays, you can use the `np.maximum()` function.

In the below example, `np.maximum(arr, arr1)` compares the elements of `arr` and `arr1` element-wise and returns a new array containing the maximum of each pair of elements.

``````
import numpy as np

# Create two NumPy arrays
arr = [22,34,88,99]
arr1 = [35,43,55,78]

# Get the element-wise maximum of the two arrays
max_value = np.maximum(arr,arr1)
print("Element-wise maximum of the arrays:",max_value)

# Output:
# Element-wise maximum of the arrays: [35 43 88 99]
``````

## 5. Get the Maximum Values of 2-D Array

If you have a 2-dimensional NumPy array and you want to find the maximum values along specific axes (rows or columns), you can use the `np.amax()` function in NumPy.

You can creates a 2-dimensional NumPy array and then uses `np.max()` function to find the maximum value in the entire array. This output indicates that 99 is the maximum value in the provided 2-D NumPy array.

``````
import numpy as np

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

# Get the maximum value in the entire array
max_value = np.max(arr)
print("Maximum value in the entire array:", max_value)

# Output:
# Maximum value in the entire array: 99
``````

To create a 2-dimensional NumPy array and then use `np.max()` function with `axis=0` to find the maximum values along columns. This output indicates the maximum values along the columns are 99, 68, and 88, respectively.

``````
# Get the maximum values along columns (axis=0)
max_values = np.max(arr, axis=0)
print("Maximum values along columns:", max_values)

# Output:
# Maximum values along columns: [99 68 88]
``````

To create a 2-dimensional NumPy array and then use `np.max()` function with `axis=1` to find the maximum values along rows. This output indicates the maximum values along the rows are 57, 99, 69, and 88, respectively.

``````
# Get the maximum values along rows (axis=1)
max_values = np.max(arr, axis=1)
print("Maximum values along rows:",max_values)

# Output:
# Maximum values along rows: [57 99 69 88]
``````

## 6. Use maximum() Function & NaN

To create two NumPy arrays, `arr` and `arr1`, with NaN values and then use `np.maximum()` function to find the element-wise maximum of the arrays, handling NaN values appropriately.

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.

``````
import numpy as np

# Create two NumPy arrays with NaN values
arr = np.array([4,9,14,np.nan,16])
arr1 = np.array([np.nan,9,18,np.nan,25])

# Use numpy.maximum() function & NaN
max_values = np.maximum(arr,arr1)
print("After the Element-wise maximum of the arrays:", max_values)

# Output:
# After the Element-wise maximum of the arrays: [nan  9. 18. nan 25.]
``````

How do I find the maximum value in a NumPy array?

You can find the maximum value in a NumPy array using the `np.max()` function. For instance, `np.max(arr)` returns the maximum value in the array `arr`.

How do I find the maximum value along a specific axis in a multi-dimensional NumPy array?

You can use maximum value along a specific `axis` in a multi-dimensional NumPy array, you can use the axis parameter in NumPy functions such as `np.max()`. The axis parameter allows you to specify along which `axis` the operation should be performed.

How do I handle NaN values while finding the maximum of a NumPy array?

NumPy functions like `np.max()` handle NaN values appropriately. NaN values do not affect the maximum computation unless the whole array consists of NaN values.

How can I find the element-wise maximum of two arrays in NumPy?

To find the element-wise maximum of two arrays in NumPy, you can use the `numpy.maximum()` function. This function compares two arrays element-wise and returns a new array containing the maximum values from the input arrays.

How do I find the index of the maximum value in a NumPy array?

You can use `np.argmax()` to find the index of the maximum value in a NumPy array. For instance, `np.argmax(arr)` returns the index of the maximum value in the array `arr`. Keep in mind that `np.argmax()` will return the index of the first occurrence of the maximum value if there are multiple occurrences of the maximum value in the array.

## 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!!