Python:math.exp 和 numpy.exp 有什么区别,为什么 numpy 创造者选择再次引入 exp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30712402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: What is the difference between math.exp and numpy.exp and why do numpy creators choose to introduce exp again
提问by Ka-Wa Yip
exp
means exponential function
exp
表示指数函数
exp
in math module
: https://docs.python.org/2/library/math.html
exp
在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
exp
在numpy module
:http: //docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html
Why do numpy
creators introduce this function again?
为什么numpy
创作者又要引入这个功能?
采纳答案by ThePredator
The math.exp
works only for scalars as EdChummentions. Whereas numpy.exp
will work for arrays.
在math.exp
作为标量只能EdChum提到。而numpy.exp
将适用于数组。
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
函数的情况也是如此。
>>> 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 ANSWERto check out how numpy
is faster than math
.
另请参阅此答案以查看numpy
比math
.
回答by Chinmay Kanchi
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.
math.exp
适用于单个数字,numpy 版本适用于 numpy 数组,并且由于矢量化的好处而速度更快。该exp
功能不单单是这一点-几个math
函数有numpy的同行,如sin
,pow
等。
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)
numpy 版本的速度提高了大约 9 倍(通过仔细选择优化的数学库,可能还可以使速度更快)
As @camz states below - the math
version will be faster when working on single values (in a quick test, ~7.5x faster).
正如@camz 在下面指出的那样 -math
处理单个值时版本会更快(在快速测试中,大约快 7.5 倍)。
回答by Z Che
If you manually vectorize math.exp using map, it is faster than numpy. As far as I tested..
如果您使用 map 手动矢量化 math.exp,它比 numpy 更快。据我测试..
%timeit np.exp(arr)
%timeit np.exp(arr)
500 μs ± 3.37 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
每个循环 500 μs ± 3.37 μs(平均值 ± 标准偏差,7 次运行,每次 1000 次循环)
%timeit map(math.exp, arr)
%timeit map(math.exp, arr)
148 ns ± 4 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
每个循环 148 ns ± 4 ns(7 次运行的平均值 ± 标准偏差,每次 10000000 次循环)
?
?