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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:51:34  来源:igfitidea点击:

Python: What is the difference between math.exp and numpy.exp and why do numpy creators choose to introduce exp again

pythonarraysnumpy

提问by Ka-Wa Yip

expmeans exponential function

exp表示指数函数

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

expmath modulehttps: //docs.python.org/2/library/math.html

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

expnumpy modulehttp: //docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html

Why do numpycreators introduce this function again?

为什么numpy创作者又要引入这个功能?

采纳答案by ThePredator

The math.expworks only for scalars as EdChummentions. Whereas numpy.expwill 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 mathfunctions.

其他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 numpyis faster than math.

另请参阅此答案以查看numpymath.

回答by Chinmay Kanchi

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

math.exp适用于单个数字,numpy 版本适用于 numpy 数组,并且由于矢量化的好处而速度更快。该exp功能不单单是这一点-几个math函数有numpy的同行,如sinpow等。

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 mathversion 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 次循环)

?

?