亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

numpy中的log和ln函數(shù)解讀

 更新時(shí)間:2022年11月03日 10:18:57   作者:勤奮的大熊貓  
這篇文章主要介紹了numpy中的log和ln函數(shù)解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

numpy的log和ln函數(shù)

每次當(dāng)我想用python實(shí)現(xiàn)ln函數(shù)時(shí),下意識(shí)的就會(huì)輸入錯(cuò)誤的函數(shù)代碼,這里特來(lái)記錄一下關(guān)于numpy中的ln和log函數(shù)正確的調(diào)用方式。

ln函數(shù)

import numpy as np


class NumpyStudy:
? ? def lnFunction(self):
? ? ? ? const = np.e
? ? ? ? result = np.log(const)
? ? ? ? print("函數(shù)ln(e)的值為:")
? ? ? ? print(result)


if __name__ == "__main__":
? ? main = NumpyStudy()
? ? main.lnFunction()
"""
函數(shù)ln(e)的值為:
1.0
"""

我們可以看到得到的值為1,說(shuō)明在python中,np.log()指代的便是數(shù)學(xué)中使用的ln函數(shù)。

log函數(shù)

import numpy as np


class NumpyStudy:
? ? def logFunction(self):
? ? ? ? const = 100
? ? ? ? result = np.log10(const)
? ? ? ? print("函數(shù)ln(e)的值為:")
? ? ? ? print(result)


if __name__ == "__main__":
? ? main = NumpyStudy()
? ? main.logFunction()
"""
函數(shù)ln(e)的值為:
2.0
"""

我們可以看到得到的值為2,說(shuō)明在python中,np.log10()指代的便是數(shù)學(xué)中使用的lg函數(shù)。

前幾天看到有一個(gè)小伙伴留言說(shuō),既然以10和以自然數(shù)e為底數(shù)的目前都有了,那么以其他數(shù)比如2,3,4等等為底數(shù)的log函數(shù)該怎么辦呢?

這里我們需要用到一下數(shù)學(xué)上的小技巧—換底公式進(jìn)行一下變換。例如:我們想要求出log以2為底16的值。

import numpy as np


class NumpyStudy:
    def lnFunction(self):
        result = np.log(16) / np.log(2)
        result1 = np.log10(16) / np.log10(2)
        print("函數(shù)ln(e)的值為:")
        print(result)
        print(result1)


if __name__ == "__main__":
    main = NumpyStudy()
    main.lnFunction()
"""
函數(shù)ln(e)的值為:
4.0
4.0
"""

可以看到我們最后成功地獲取到了正確的結(jié)果4.0。用這種方法我們可以獲取到以任意數(shù)為底數(shù)的log函數(shù)值。

numpy的部分通用函數(shù)

1.數(shù)組算術(shù)運(yùn)算符

運(yùn)算符對(duì)應(yīng)的通用函數(shù)描述
+np.add加法運(yùn)算(即1+1=2)
-np.substract減法運(yùn)算(即3-2=1)
-np.negative負(fù)數(shù)運(yùn)算(即-2)
*Nnp.multiply乘法運(yùn)算(即2*3=6)
/np.divide除法運(yùn)算(即3/2=1.5)
//np.floor_divide向下整除運(yùn)算(floor division,即3//2=1)
**np.power指數(shù)運(yùn)算(即2 ** 3=8)
%np.mod模/余數(shù)(即9%4=1)

這些都是一元通用函數(shù),寫(xiě)代碼時(shí)可直接用左欄的運(yùn)算符代替

x=np.arrange(4)
#array([0, 1, 2, 3])
x + 2
#array([2, 3, 4, 5])
np.add(x,2)
#array([2, 3, 4, 5])

2.絕對(duì)值通用函數(shù)np.absolute()

也可以通過(guò)np.abs()訪問(wèn)

其對(duì)復(fù)數(shù)的運(yùn)算是求模

x=np.array([-2, -1, 0, 1, 2])
abs(x)
#array([2, 1, 0, 1, 2])
np.absolute(x)
#array([2, 1, 0, 1, 2])

3.三角函數(shù)

  • np.sin()
  • np.cos()
  • np.tan()

反三角同理

4.指數(shù)和對(duì)數(shù)

表達(dá)函數(shù)
e^xnp.exp(x)
2^xnp.exp2(x)
3^xnp.power(3, x)
ln(x)np.log(x)
log2(x)np.log2(x)
log10(x)np.log10(x)
exp(x)-1np.expm1(x)
log(1+x)np.log1p(x)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python讀取幾個(gè)G的csv文件方法

    python讀取幾個(gè)G的csv文件方法

    今天小編就為大家分享一篇python讀取幾個(gè)G的csv文件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 基于Python中求和函數(shù)sum的用法詳解

    基于Python中求和函數(shù)sum的用法詳解

    今天小編就為大家分享一篇基于Python中求和函數(shù)sum的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • python初學(xué)定義函數(shù)

    python初學(xué)定義函數(shù)

    這篇文章主要為大家介紹了python的定義函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助,希望能夠給你帶來(lái)幫助
    2021-11-11
  • 最新評(píng)論