Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

ufunc Logs

Logs

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.

Log at Base 2

Apply the log2() function to calculate the logarithm with base 2.

Example

Calculate the base 2 logarithm of all elements in the following array:

import numpy as np

arr = np.arange(110)

print(np.log2(arr))