exp means exponential function
Why do numpy creators introduce this function again?
解决方案
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 "", line 1, in
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 "", line 1, in
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.