I'm very confused as to what np.exp() actually does. In the documentation it says that it: "Calculates the exponential of all elements in the input array." I'm confused as to what exactly this means. Could someone give me more information to what it actually does?
Asked
Active
Viewed 2.6e+01k times
3 Answers
146
The exponential function is e^x
where e
is a mathematical constant called Euler's number, approximately 2.718281
. This value has a close mathematical relationship with pi
and the slope of the curve e^x
is equal to its value at every point. np.exp()
calculates e^x
for each value of x
in your input array.

machine yearning
- 9,889
- 5
- 38
- 51
66
It calculates ex for each x in your list where e is Euler's number (approximately 2.718). In other words, np.exp(range(5))
is similar to [math.e**x for x in range(5)]
.

Tom
- 2,674
- 1
- 25
- 33
-
2For beginner: `math.e` is the Euler's number. Try `import math; print(math.e) # 2.718281828459045`. And `**` is Python code for power. – Muhammad Yasirroni Oct 30 '22 at 05:48
38
exp(x) = e^x where e= 2.718281(approx)
In Python we can use the exp
function from numpy
(docs):
import numpy as np
ar=np.array([1,2,3])
ar=np.exp(ar)
print ar
outputs:
[ 2.71828183 7.3890561 20.08553692]
-
1Your code gives me following error: ufunc 'exp' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind'" – Sep 27 '16 at 16:12
-
1If you change the [1,2,3] to [1.,2.,3.], the error should disappear. I think it happens because of a conversion of the numpy values to object type. So you put them as float, and it works :) or maybe it happens only in Python 2x, I tested in python 3.6 and it worked. – Joel Carneiro Feb 14 '19 at 12:01
-