NumPy – Using maximum() and fmax() functions (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

Understanding how to perform element-wise operations efficiently on arrays is a fundamental aspect of using NumPy for numerical computations. In this comprehensive guide, we will explore the nuances of two important functions provided by NumPy: maximum() and fmax(). By working through four progressively advanced examples, you’ll gain a deep understanding of how and when to use these functions effectively. Let’s dive into the subtle distinctions and powerful capabilities of both.

The Fundamentals of the maximum() and fmax() Functions

NumPy, or the Numerical Python, is an essential library for scientific computing in Python. It provides robust support for large, multi-dimensional arrays and matrices, along with a broad collection of mathematical functions to operate on these arrays.

The maximum() and fmax() functions are part of this vast library, offering two options for element-wise maximum operations over arrays or between scalars and arrays. The primary difference lies in how they handle NaN (Not a Number) values:

  • maximum() – Returns the maximum of two arrays, element-wise. It will return NaN if either of the elements being compared is NaN.
  • fmax() – Ignores NaN values, considering only numeric values in the comparison, which makes it slightly safer for handling missing data.

Example 1: Basic Usage

Let’s start with the basic usage of these functions to understand their primary behavior. You’ll need NumPy imported to explore these examples, so make sure to install it using pip (pip install numpy) if you haven’t already.

import numpy as np
from numpy import NaN

# define two arrays
array_a = np.array([1, 2, NaN, 4])
array_b = np.array([2, NaN, 3, 1])

# Maximum between two arrays
max_result = np.maximum(array_a, array_b)
print('Result of np.maximum:', max_result)

# Maximum ignoring NaN
fmax_result = np.fmax(array_a, array_b)
print('Result of np.fmax:', fmax_result)

Output:

Result of np.maximum: [ 2. nan nan  4.]
Result of np.fmax: [2. 2. 3. 4.]

This simple comparison reveals how maximum() returns NaN when either of the compared values is NaN, whereas fmax() ignores the NaN and provides a numeric comparison instead.

Example 2: Operating with Scalars

Both maximum() and fmax() can be used to compare arrays with scalars, making them incredibly useful for thresholding operations. For instance:

import numpy as np
from numpy import NaN

array_c = np.array([1, 5, NaN, 7])
scalar = 4

# Compare each element of the array with the scalar
max_with_scalar = np.maximum(array_c, scalar)
print('Comparing array with scalar using np.maximum:', max_with_scalar)

fmax_with_scalar = np.fmax(array_c, scalar)
print('Comparing array with scalar using np.fmax:', fmax_with_scalar)

Output:

Comparing array with scalar using np.maximum: [ 4.  5. nan  7.]
Comparing array with scalar using np.fmax: [4. 5. 4. 7.]

This functionality allows for intuitive and straightforward thresholding operations across your data.

Example 3: Broadcasting

One of NumPy’s strengths is its ability to perform broadcasting, which allows for operations across arrays of different shapes. The maximum() and fmax() functions are no exception here. Consider when comparing a 1D array with a 2D array:

import numpy as np

array_d = np.array([1, 2, 3])
array_e = np.array([[1, 2, 3], [4, 5, 6]])

# Broadcasting operation
max_broadcast = np.maximum(array_d, array_e)
print('Result of broadcasting with np.maximum:', max_broadcast)

fmax_broadcast = np.fmax(array_d, array_e)
print('Result of broadcasting with np.fmax:', fmax_broadcast)

Output:

Result of broadcasting with np.maximum: [[1 2 3]
 [4 5 6]]
Result of broadcasting with np.fmax: [[1 2 3]
 [4 5 6]]

Broadcasting enables the functions to apply across arrays of different sizes effectively, opening up more complex and flexible operations.

Example 4: Advanced Usage with np.nanmax and Handling Arrays with NaNs

While fmax() is beneficial for comparisons involving NaNs, sometimes you might want to first preprocess arrays to replace or ignore NaNs altogether. This is where np.nanmax() comes into play, providing a way to compute the maximum value in an array while ignoring any NaNs:

import numpy as np
from numpy import NaN

array_f = np.array([NaN, 2, NaN, 4])

# Using np.nanmax to ignore NaNs and find max
max_ignore_nan = np.nanmax(array_f)
print('Max ignoring NaNs using np.nanmax:', max_ignore_nan)

Output:

Max ignoring NaNs using np.nanmax: 4.0

This approach is especially useful in data preprocessing stages or when performing statistical analyses.

Conclusion

The maximum() and fmax() functions are versatile tools in NumPy’s arsenal for numerical computing. Through these examples, we’ve seen how they allow for elemental comparisons, broadcasting across different array shapes, and handling of NaNs in a manner suited to your needs. Understanding when to use maximum versus fmax, especially in the presence of NaNs, can significantly enhance your data processing workflows.