python中exp,的Python:是什么math.exp和numpy.exp之间的区别,为什么numpy的创造者选择再次引进EXP...

exp means exponential function

Why do numpy creators introduce this function again?

解决方案

The math.exp works only for scalars as EdChum mentions. Whereas numpy.exp will work for arrays.

Example:

>>> import math

>>> import numpy as np

>>> x = [1.,2.,3.,4.,5.]

>>> math.exp(x)

Traceback (most recent call last):

File "", line 1, in

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.sin(x)

Traceback (most recent call last):

File "", line 1, in

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 ANSWER to check out how numpy is faster than math.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值