Overflow error encountered in Python's numpy.exp() function
NumPy
is a Python package that contains a rich set of utilities for manipulating large multidimensional matrices and arrays and performing complex and straightforward mathematical operations on them.
These utilities are dynamic on input and are highly optimized and fast. NumPy
The package has a function exp()
that computes the exponential of all elements of an input numpy array.
In other words, it computes e x , x
for each number in the input numpy array, e
to be approximately equal 2.71828
to the Euler number .
Because this calculation can produce huge numbers, some data types cannot handle such large values, so this function returns inf
and error instead of a valid floating point value.
For example, for numpy.exp(709)
, this function will return 8.21840746e+307
, but for numpy.exp(710)
, it will return runtimeWarning:exp inf 中遇到溢出
.
In this article, we will learn how to solve this problem.
Fix numpy.exp()
overflow in Python NumPy function
We have to store the value in a data type that can hold such large values to solve this problem.
For example, np.float128
can hold numbers larger than float64
and float32
. All we have to do is typecast each value of the array to a larger data type and store it in a numpy array.
The following Python code describes this.
import numpy as np
a = np.array([1223, 2563, 3266, 709, 710], dtype=np.float128)
print(np.exp(a))
Output:
[1.38723925e+0531 1.24956001e+1113 2.54552810e+1418 8.21840746e+0307
2.23399477e+0308]
Even though the Python code above runs seamlessly without any issues, we are still prone to the same mistakes.
The reason behind this is simple; even np.float128
has a threshold of the number it can hold. Every data type has an upper limit, and if you exceed that upper limit, things start going wrong and your program starts giving overflow errors.
To understand the above mentioned point, refer to the following Python code. Although np.float128
our problem is solved in the last Python code snippet, it does not work for larger values.
import numpy as np
a = np.array([1223324, 25636563, 32342266, 235350239, 27516346320], dtype=np.float128)
print(np.exp(a))
Output:
<string>:4: RuntimeWarning: overflow encountered in exp
[inf inf inf inf inf]
exp()
The function returns an infinity for each value in the numpy array.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Remove Nan values from NumPy array
Publish Date:2025/05/03 Views:118 Category:Python
-
This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn
Normalizing a vector in Python
Publish Date:2025/05/03 Views:51 Category:Python
-
A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,
Calculating Euclidean distance in Python
Publish Date:2025/05/03 Views:128 Category:Python
-
In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points. In Python, the numpy, scipy
Element-wise division in Python NumPy
Publish Date:2025/05/03 Views:199 Category:Python
-
This tutorial shows you how to perform element-wise division on NumPy arrays in Python. NumPy Element-Wise Division using numpy.divide() the function If we have two arrays and want to divide each element of the first array with each element
Convert 3D array to 2D array in Python
Publish Date:2025/05/03 Views:79 Category:Python
-
In this tutorial, we will discuss the methods to convert 3D array to 2D array in Python. numpy.reshape() Convert 3D array to 2D array using function in Python [ numpy.reshape() Function](numpy.reshape - NumPy v1.20 manual)Changes the shape