.exp()
The np.exp()
function in NumPy computes the exponential of all elements in the input array. The exponential function, np.exp(x)
, returns e^x
, where e
is Euler’s number with an approximate value of 2.71828. As a part of NumPy, a widely used library for numerical computing in Python, this function is particularly useful in scientific computations where exponential functions are common.
Syntax
numpy.exp(arr, out=None, where=True, casting=‘same_kind’, order=‘K’, dtype=None)
Parameters:
arr
: The input array or list for computing the exponential.out
: The location to store the result. It must be broadcastable to the shape ofarr
.where
: The condition to be checked. WhenTrue
, the function is applied and whenFalse
, the original values are retained.casting
: Controls the type of data casting that may occur.order
: Defines the memory layout order of the result:C
for C-order,F
for Fortran-order,A
for automatic, andK
for input layout.dtype
: Overrides the data type of the resultant array.
Return Value:
The np.exp()
function returns a new array containing the exponential of all elements in the input array.
Note: In the
np.exp()
function, the only mandatory parameter isarr
. All other parameters, includingout
,where
,casting
,order
, anddtype
, are optional and have default values.
Example 1: Using .exp()
with an Array
This example uses the np.exp()
function to compute the exponential of all elements in the given array:
import numpy as np# Create an arrayarr = np.array([0, 1, 2, 3])# Compute the exponential of all elements in the arrayresult = np.exp(arr)# Print the resultprint("Exponential of input array:", result)
The output for the example will be:
Exponential of input array: [ 1. 2.71828183 7.3890561 20.08553692]
Example 2: Using .exp()
with a Positive Number
This example uses the np.exp()
function to compute the exponential of a positive number:
import numpy as np# Compute the exponential of 5result = np.exp(5)# Print the resultprint("Exponential of 5:", result)
The output for the example will be:
Exponential of 5: 148.4131591025766
Codebyte Example: Using .exp()
with a Negative Number
This codebyte example uses the np.exp()
function to compute the exponential of a negative number:
Frequently Asked Questions
1. Can I use np.exp()
with a list?
Yes, np.exp()
can be used with a list; it automatically converts the list to a NumPy array and applies the exponential function element-wise.
2. Does NumPy exp work with complex numbers?
Yes, np.exp()
supports complex numbers and returns the exponential of each complex input using Euler’s formula.
3. What is the difference between math exp and NumPy exp?
math.exp()
works only with single scalar values, while np.exp()
can handle arrays, lists, and complex numbers, applying the function element-wise.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours