ufunc Logs
NumPy provides logarithmic ufuncs to compute natural logs, base-10 logs, and more. These functions are essential for mathematical modeling, data transformations, and machine learning tasks.
Key Topics
Logarithmic Methods
NumPy's logarithmic ufuncs include:
log
: Computes the natural logarithm (base-e).log10
: Computes the base-10 logarithm.log2
: Computes the base-2 logarithm.
Example
# Logarithmic ufuncs
import numpy as np
arr = np.array([1, 10, 100, 1000])
natural_log = np.log(arr)
log_base10 = np.log10(arr)
log_base2 = np.log2(arr)
print("Natural log:", natural_log)
print("Base-10 log:", log_base10)
print("Base-2 log:", log_base2)
Output
Natural log: [0. 2.302 4.605 6.908]
Base-10 log: [0. 1. 2. 3.]
Base-2 log: [0. 3.321 6.644 9.966]
Base-10 log: [0. 1. 2. 3.]
Base-2 log: [0. 3.321 6.644 9.966]
Explanation: Each ufunc computes logarithms for the array elements with the specified base, transforming data into a logarithmic scale.
Key Takeaways
- Natural Log: Use
log
for base-e logarithms. - Base-10 Log: Use
log10
for logarithms in base-10. - Base-2 Log: Use
log2
for binary logarithms. - Applications: Analyze exponential growth, compress data ranges, or solve power-law relationships.