網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
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ù),寫代碼時(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^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) |
原文鏈接:https://blog.csdn.net/u011699626/article/details/118885071
相關(guān)推薦
- 2022-08-10 python數(shù)組中的?k-diff?數(shù)對(duì)例題解析_python
- 2022-06-26 python數(shù)據(jù)處理之Pandas類型轉(zhuǎn)換的實(shí)現(xiàn)_python
- 2022-03-16 .Net?6簡(jiǎn)介并和之前版本寫法做對(duì)比_基礎(chǔ)應(yīng)用
- 2022-11-20 .Net中Task?Parallel?Library的基本用法_基礎(chǔ)應(yīng)用
- 2022-04-03 Nginx構(gòu)建Tomcat集群的操作方法_nginx
- 2022-11-01 redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí)_React
- 2022-07-24 Harbor高可用配置及倉(cāng)庫(kù)使用介紹_云其它
- 2022-06-14 C語(yǔ)言選擇、循環(huán)、函數(shù)、數(shù)組與操作符_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支