Python:math.exp和numpy.exp有什么区别,为什么numpy创建者选择再次引入exp

标签 python arrays numpy

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/

相关文章:

javascript - 当选择其他选择选项时,用变量值更新选择列表?

python - Python如何查找数组中元素的位置?

python - 如何在 matplotlib 中根据 x、y、z 坐标绘制等高线图? (plt.contourf 或 plt.contour)

一定范围内整数的 Python numpy 乘积

python - 计算字符串出现次数的最快方法

c - 此 C 代码如何为多维数组的第一个索引生成正确的大小

javascript - jQuery 返回不带逗号的字符串的前 5 个单词

python - 给定原始和复杂的一维数据的核估计

python - 是否有用于生成类似于 R 的 seq 函数的序列的 numpy 函数?

python - 一行 Python 代码能知道它的缩进嵌套级别吗?