In NumPy, logarithm functions are included in the category of universal functions (ufuncs) and are used to compute the logarithm of elements in a NumPy array. NumPy provides several logarithm functions to calculate different types of logarithms:
import numpy as np
arr = np.array([1, 2, 3])
# Compute natural logarithm
result = np.log(arr)
print(result) # Output: [0. 0.69314718 1.09861229]
import numpy as np
arr = np.array([1, 10, 100])
# Compute base-10 logarithm
result = np.log10(arr)
print(result) # Output: [0. 1. 2.]
import numpy as np
arr = np.array([1, 100, 1000])
# Compute logarithm with base 10
result_base_10 = np.log(arr) / np.log(10)
# Compute logarithm with base 2
result_base_2 = np.log(arr) / np.log(2)
print("Logarithm with base 10:", result_base_10)
print("Logarithm with base 2:", result_base_2)
These logarithm functions are useful for various mathematical and scientific computations, such as data transformation, signal processing, and statistical analysis. They are efficient and vectorized, making them suitable for working with large arrays of data in NumPy.