exp
表示指数函数
数学模块
中的exp
:https://docs.python.org/2/library/math.html
numpy 模块
中的exp
:http://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html
为什么numpy
创作者又要引入这个功能了?
最佳答案
math.exp
仅适用于标量 EdChum提到。而 numpy.exp
将适用于数组。
例子:
>>> 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 ])
>>>
其他 math
函数也是如此。
>>> 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])
>>>
另请参阅 THIS ANSWER查看 numpy
如何比 math
快。
关于Python:math.exp和numpy.exp有什么区别,为什么numpy创建者选择再次引入exp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712402/