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?

Community
  • 1
  • 1
bugsyb
  • 5,662
  • 7
  • 31
  • 47

3 Answers3

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
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]
s2t2
  • 2,462
  • 5
  • 37
  • 47
Arijit
  • 1,506
  • 14
  • 23
  • 1
    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'" –  Sep 27 '16 at 16:12
  • 1
    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. – Joel Carneiro Feb 14 '19 at 12:01
  • `assert np.exp(np.log(2.5)) == 2.5` – s2t2 Nov 13 '22 at 18:30