欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

numpy的log和ln函數(shù)

每次當我想用python實現(xiàn)ln函數(shù)時,下意識的就會輸入錯誤的函數(shù)代碼,這里特來記錄一下關(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,說明在python中,np.log()指代的便是數(shù)學中使用的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,說明在python中,np.log10()指代的便是數(shù)學中使用的lg函數(shù)。

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

這里我們需要用到一下數(shù)學上的小技巧—換底公式進行一下變換。例如:我們想要求出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ù)運算符

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

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

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.絕對值通用函數(shù)np.absolute()

也可以通過np.abs()訪問

其對復數(shù)的運算是求模

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ù)和對數(shù)

表達函數(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)

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

相關(guān)文章

  • python讀取幾個G的csv文件方法

    python讀取幾個G的csv文件方法

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

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

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

    python初學定義函數(shù)

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