python中math.log()函数和numpy.log()函数区别
python中 math.log 函数和numpy.log 函数区别1.调用math.log 函数进行对数运算2.调用numpy.log函数进行对数运算3.总结区别1.调用math.log 函数进行对数运算因为我需要对一个数组的每个元素都取对数,一开始,我使用的是math.log(),结果程序给我报错:#执行的python程序L_p=math.log10(data/P_ref1)#程序返回的错误:
·
python中 math.log 函数和numpy.log 函数区别
1.调用math.log 函数进行对数运算
因为我需要对一个数组的每个元素都取对数,一开始,我使用的是math.log(),结果程序给我报错:
#执行的python程序
L_p=math.log10(data/P_ref1)
#程序返回的错误:
TypeError: only size-1 arrays can be converted to Python scalars
出错原因很显然,math.log()只能对单个数值 (scalar) 进行运算,而无法对多个数值(scalars)进行计算。
2.调用numpy.log函数进行对数运算
将程序改为numpy.log进行计算:
L_p=numpy.log10(data/P_ref1)
#程序结果输出 L_p:
[-48.20831346 -48.20831346 -48.20831346 ... -65.11027426 -59.08967434
-59.08967434]
此次就没有 再次报错了。
3.总结区别
- numpy.log()和math.log()都可以进行对数运算
- math.log无法对多个数值进行计算,而numpy.log可以
更多推荐
所有评论(0)