ufunc Logs
A logarithm answers the question "to what power must I raise the base to get this number?". log₁₀(1000) = 3 because 10³ = 1000. Logs turn multiplication into addition and squash huge ranges of values into manageable ones, which is why they appear in the decibel scale, the Richter scale, pH, information theory and data analysis.
NumPy provides logs for the three most common bases:
| ufunc | Base | Typical use |
|---|---|---|
np.log2(x) |
2 | bits, doublings, binary trees |
np.log10(x) |
10 | orders of magnitude, decibels |
np.log(x) |
e ≈ 2.718 (natural log) | growth rates, statistics, calculus |
Log base 2
import numpy as np
sizes = np.array([1, 2, 8, 1024, 1_000_000])
print(np.log2(sizes))
# How many yes/no questions to find one item among n?
print(np.ceil(np.log2([10, 100, 1000])).astype(int))[ 0. 1. 3. 10. 19.93156857]
[ 4 7 10]
The second line explains why binary search is fast: even among 1,000 items you need at most 10 halvings.
Log base 10
import numpy as np
amounts = np.array([7, 70, 700, 7000, 70_000])
print(np.log10(amounts).round(3))
print("number of digits:", np.floor(np.log10(amounts)).astype(int) + 1)
# Sound intensity ratio to decibels
ratio = np.array([1, 10, 100, 1_000_000])
print("dB:", 10 * np.log10(ratio))[0.845 1.845 2.845 3.845 4.845]
number of digits: [1 2 3 4 5]
dB: [ 0. 10. 20. 60.]
Each tenfold step adds exactly 1 to the log₁₀. That is why log scales are handy when data spans many orders of magnitude.
Natural log
The natural logarithm uses the base e. It is the inverse of np.exp:
import numpy as np
x = np.array([1, np.e, np.e**2, 10])
print(np.log(x).round(4))
print(np.exp(np.log(x)).round(4)) # back where we started
# Continuous growth: how long until money doubles at 7 % per year?
print("years to double:", (np.log(2) / np.log(1.07)).round(2))[0. 1. 2. 2.3026]
[ 1. 2.7183 7.3891 10. ]
years to double: 10.24
Log at any base
There is no ufunc for an arbitrary base, but the change-of-base rule log_b(x) = ln(x) / ln(b) does the job. You can also wrap a helper in np.frompyfunc from the standard math.log:
import numpy as np
from math import log
x = np.array([1, 3, 9, 27, 81])
print(np.log(x) / np.log(3)) # vectorised, float result
log_any = np.frompyfunc(log, 2, 1)
print(log_any(100, 15)) # one value, base 15[0. 1. 2. 3. 4.]
1.7005483074552052
Logs of small numbers and zero
np.log(0) is -inf (with a warning), and logs of negative numbers are nan. For values very close to zero, np.log1p(x) computes log(1 + x) more accurately than np.log(1 + x):
import numpy as np
with np.errstate(divide="ignore", invalid="ignore"):
print(np.log(np.array([0.0, -1.0, 1.0])))
tiny = 1e-12
print(np.log(1 + tiny)) # loses precision
print(np.log1p(tiny)) # accurate[-inf nan 0.]
1.000088900581841e-12
9.999999999995e-13
Tip: A common data-science trick is to apply
np.log1pto skewed, non-negative data (prices, counts) before analysing or plotting it.np.expm1undoes it.
Test yourself
What is np.log10(10_000)?
- 2.0
- 4.0
- 10.0
- 1000.0
Which function is the inverse of np.log?
- np.log10
- np.exp
- np.power
- np.sqrt
How do you compute log base 5 of an array x?
- np.log5(x)
- np.log(x, 5)
- np.log(x) / np.log(5)
- np.log10(x) * 5