What exactly does numpy.exp() do?
Asked Answered
M

3

136

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?

Metternich answered 11/8, 2015 at 21:8 Comment(1)
Well, have you tried it? print np.arange(10), np.exp(np.arange(10)) (for python2).Diseur
P
148

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.

Pout answered 11/8, 2015 at 21:16 Comment(0)
M
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)].

Matthaus answered 11/8, 2015 at 21:16 Comment(1)
For beginner: math.e is the Euler's number. Try import math; print(math.e) # 2.718281828459045. And ** is Python code for power.Debris
H
39

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]
Hypogynous answered 11/8, 2015 at 21:17 Comment(3)
Your 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'"Sinclare
If 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.Scopoline
assert np.exp(np.log(2.5)) == 2.5Underestimate

© 2022 - 2024 — McMap. All rights reserved.