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^x | np.exp(x) |
2^x | np.exp2(x) |
3^x | np.power(3, x) |
ln(x) | np.log(x) |
log2(x) | np.log2(x) |
log10(x) | np.log10(x) |
exp(x)-1 | np.expm1(x) |
log(1+x) | np.log1p(x) |
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實現(xiàn)上傳樣本到virustotal并查詢掃描信息的方法
這篇文章主要介紹了python實現(xiàn)上傳樣本到virustotal并查詢掃描信息的方法,是比較實用的技巧,需要的朋友可以參考下2014-10-10python3.6中anaconda安裝sklearn踩坑實錄
這篇文章主要介紹了python3.6中anaconda安裝sklearn踩坑實錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07全面掌握Python?JSON庫函數(shù)與方法學會JSON數(shù)據(jù)處理
Python提供了內(nèi)置的JSON庫,允許在Python中解析和序列化JSON數(shù)據(jù),本文將深入研究Python中JSON庫的各種函數(shù)和方法,為你提供豐富的示例代碼來幫助掌握JSON處理的方方面面2024-01-01python使用requests庫爬取拉勾網(wǎng)招聘信息的實現(xiàn)
這篇文章主要介紹了python使用requests庫爬取拉勾網(wǎng)招聘信息的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11Python利用PyExecJS庫執(zhí)行JS函數(shù)的案例分析
這篇文章主要介紹了Python利用PyExecJS庫執(zhí)行JS函數(shù),本文通過案例分析給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12