32

exp means exponential function

exp in math module: https://docs.python.org/2/library/math.html

exp in numpy module: http://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html

Why do numpy creators introduce this function again?

Ka-Wa Yip
  • 2,251
  • 3
  • 19
  • 30
  • 7
    The numpy one accepts an array, the math version will work on a scalar object type only. The numpy one will perform `exp` on the entire array, it is a vectorised method of performing the function on the entire array this is what it's designed for – EdChum Jun 08 '15 at 14:53
  • 2
    `numpy.exp()` may be called on array and there is a good chance computation will be paralleled (like a lot of vector / matrix operations in numpy). This gain is a main reason to this kind of libraries in first place. – Łukasz Rogalski Jun 08 '15 at 14:59
  • @kwy: if you think my answer solved your question, pls accept it – Srivatsan Jun 10 '15 at 20:32

3 Answers3

40

The math.exp works only for scalars as EdChum mentions. Whereas numpy.exp will work for arrays.

Example:

>>> import math
>>> import numpy as np
>>> x = [1.,2.,3.,4.,5.]
>>> math.exp(x)

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    math.exp(x)
TypeError: a float is required
>>> np.exp(x)
array([   2.71828183,    7.3890561 ,   20.08553692,   54.59815003,
        148.4131591 ])
>>> 

It is the same case for other math functions.

>>> math.sin(x)

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    math.sin(x)
TypeError: a float is required
>>> np.sin(x)
array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 , -0.95892427])
>>> 

Also refer to THIS ANSWER to check out how numpy is faster than math.

Community
  • 1
  • 1
Srivatsan
  • 8,657
  • 10
  • 51
  • 82
8

math.exp works on a single number, the numpy version works on numpy arrays and is tremendously faster due to the benefits of vectorization. The exp function isn't alone in this - several math functions have numpy counterparts, such as sin, pow, etc.

Consider the following:

In [10]: import math

In [11]: import numpy

In [13]: arr = numpy.random.random_integers(0, 500, 100000)

In [14]: %timeit numpy.exp(arr)
100 loops, best of 3: 1.89 ms per loop

In [15]: %timeit [math.exp(i) for i in arr]
100 loops, best of 3: 17.9 ms per loop

The numpy version is ~9x faster (and probably can be made faster still by a careful choice of optimized math libraries)

As @camz states below - the math version will be faster when working on single values (in a quick test, ~7.5x faster).

Chinmay Kanchi
  • 58,811
  • 22
  • 84
  • 113
  • 4
    Might be worth noting that the math version will be faster than the numpy one when only used on a single value and not a whole array. – camz Jun 08 '15 at 15:11
0

If you manually vectorize math.exp using map, it is faster than numpy. As far as I tested..

%timeit np.exp(arr)

500 µs ± 3.37 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit map(math.exp, arr)

148 ns ± 4 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Z Che
  • 71
  • 3
  • 1
    Just for anyone finding this later, I'm pretty sure the only reason this is so is because `map` doesn't actually evaluate anything. It returns an iterator. Try `%timeit list(map(math.exp, arr))` to force the map to evaluate, and you'll get `104 µs ± 9.17 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)` – Ethan Brouwer Dec 15 '20 at 19:07