NumPy Logs
Logs
NumPy provides functions to perform log at the base 2, e and 10.
We will also explore how we can take log for any base by creating a custom ufunc.
All of the log functions will place -inf or inf in the elements if the log can not be computed.
Log at Base 2
Use the log2()
function to perform log at the base 2.
实例
Find log at base 2 of all elements of following array:
import numpy as np
arr = np.arange(1, 10)
print(np.log2(arr))
亲自试一试 »
注释: arange(1, 10)
函数返回一个从 1(包括)到 10(不包括)的整数数组。
Log at Base 10
Use the log10()
function to perform log at the base 10.
实例
Find log at base 10 of all elements of following array:
import numpy as np
arr = np.arange(1, 10)
print(np.log10(arr))
亲自试一试 »
Natural Log, or Log at Base e
Use the log()
function to perform log at the base e.
实例
Find log at base e of all elements of following array:
import numpy as np
arr = np.arange(1, 10)
print(np.log(arr))
亲自试一试 »
Log at Any Base
NumPy does not provide any function to take log at any base,
so we can use the frompyfunc()
function along with inbuilt function math.log()
with two input parameters and one output parameter:
实例
from math import log
import numpy as np
nplog = np.frompyfunc(log,
2, 1)
print(nplog(100, 15))
亲自试一试 »