1. 程式人生 > 實用技巧 >np.log 和math.log的底數是什麼

np.log 和math.log的底數是什麼

一直分不清楚log到底是以什麼為底,就寫下這個作為備忘

看到沒,是以e為底的,如果是其他的就logn

import numpy as np
print( 'np.e:',np.e)
print( 'np.log([100,10000,10000]:',np.log([100,10000,10000]))   #輸出結果是不是和你想象的不一樣,請往下看   
print( 'np.log10([100,10000,10000]:',np.log10([100,10000,10000])) #以10為底的對數
print( 'np.log([1,np.e,np.e**2]):',np.log([1,np.e,np.e**2]))   #np.log(x) 預設底數為自然數e
print
( 'np.log2([2,2**2,2**3]):',np.log2([2,2**2,2**3])) #以2為底的對數
np.e: 2.718281828459045
np.log([100,10000,10000]: [4.60517019 9.21034037 9.21034037]
np.log10([100,10000,10000]: [2. 4. 4.]
np.log([1,np.e,np.e**2]): [0. 1. 2.]
np.log2([2,2**2,2**3]): [1. 2. 3.]

但是,math的log又是怎麼使用的,預設還是以e為底

import math 
#一般用法是math.log(a,底數),其中只輸入一個值,則是以e為底
math.log(10) #2.302585092994046 math.log(math.e) #1.0 math.log(100,10) #2.0 math.log(10,100) #0.5