的Python:是什么math.exp和numpy.exp之间的区别,为什么numpy的创造者选择再次引进EXP [英] Python: What is the difference between math.exp and numpy.exp and why do numpy creators choose to introduce exp again
本文介绍了的Python:是什么math.exp和numpy.exp之间的区别,为什么numpy的创造者选择再次引进EXP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
EXP
表示指数函数
EXP
在数学模块
:<一href=\"https://docs.python.org/2/library/math.html\">https://docs.python.org/2/library/math.html
EXP
在 numpy的模块
:<一href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html\">http://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html
为什么 numpy的
创再次推出此功能?
Why do numpy
creators introduce this function again?
推荐答案
的 math.exp
仅适用于作为标量的 EdChum 提到。而 numpy.exp
将用于数组。
The math.exp
works only for scalars as EdChum mentions. Whereas numpy.exp
will work for arrays.
例如:
>>> 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.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])
>>>
另请参阅这个答案检查出如何 numpy的
比数学
更快。
这篇关于的Python:是什么math.exp和numpy.exp之间的区别,为什么numpy的创造者选择再次引进EXP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文