Using numpy.expm1() function (5 examples)

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

Introduction

The NumPy library is a cornerstone for scientific computing in Python, providing support for large, multi-dimensional arrays along with a collection of mathematical functions to operate on these arrays. In this tutorial, we’ll explore the np.expm1() function, providing a comprehensive guide through five graduated examples.

The np.expm1(x) function calculates e^x - 1 for all elements in the input array x, where e is the base of the natural logarithm. This might seem straightforward, but np.expm1() offers higher precision than the straightforward approach of using np.exp(x) - 1, especially for small values of x.

Basic Usage

Let’s start with the basics. To use np.expm1(), you first need to import the NumPy library:

import numpy as np

Then, you can directly apply the np.expm1() function to a number or a NumPy array:

print(np.expm1(1))
print(np.expm1(np.array([0, 1, 2, 3])))

Output:

1.718281828459045
[0.         1.71828183 6.3890561  19.08553692]

Notice how the function accurately calculates e^x - 1 for single values and arrays.

Working with Different Data Types

NumPy’s np.expm1() function is flexible, automatically handling various data types. Let’s see how it performs with integers and floats:

int_array = np.array([1, 2, 3], dtype=int)
float_array = np.array([1., 2., 3.], dtype=float)
print(np.expm1(int_array))
print(np.expm1(float_array))

Output:

[ 1.71828183  6.3890561  19.08553692]
[ 1.71828183  6.3890561  19.08553692]

As you can see, np.expm1() delivers consistent results across data types, showcasing its versatility.

Dealing with Very Small Numbers

One of the strengths of np.expm1() is its ability to maintain precision with very small numbers, where the traditional exp(x) - 1 might lose significance. Here’s an example:

print(np.expm1(1e-10))
print(np.exp(1e-10) - 1)

Output:

1.00000000005e-10
1.000000082740371e-10

The outputs are close, but np.expm1() provides a slightly more accurate result due to reduced numerical error.

Integration with Arrays

Next, let’s explore how np.expm1() seamlessly integrates with multidimensional arrays:

matrix = np.array([[0, 1], [2, 3]])
print(np.expm1(matrix))

Output:

[[ 0.          1.71828183]
 [ 6.3890561  19.08553692]]

This example demonstrates np.expm1()‘s capability to work effectively with complex data structures, maintaining high precision.

Mixed Applications

Finally, let’s see np.expm1() in a mixed-use case involving both data preprocessing and computation for a hypothetical data analysis:

# Generating a small dataset
dataset = np.random.normal(loc=0, scale=1, size=100)

# Applying np.expm1() for transformation
data_transformed = np.expm1(dataset)

print(data_transformed[:5])

This example shows how np.expm1() can be part of a larger preprocessing pipeline, applying nonlinear transformations to data for analysis or machine learning.

Conclusion

The np.expm1() function is a robust and precise tool for computing e^x - 1 across a wide range of applications. Its precision, especially with small numbers, and flexibility with different data structures make it indispensable for scientific computing in Python.