NumPy offers functions to calculate logarithms with bases 2, e, and 10. We’ll also explore how to create a custom ufunc for logarithms with any base. If a log cannot be computed, the functions will assign -inf or inf to the elements.
Apply the log2() function to calculate the logarithm with base 2.
Calculate the base 2 logarithm of all elements in the following array:
import numpy as np arr = np.arange(1, 10) print(np.log2(arr)) |