NumPy - log2() function
The NumPy log2() function is used to calculate the base-2 logarithm of a given value.
Syntax
numpy.log2(x, out=None)
Parameters
x |
Required. Specify array (array_like) containing elements, of which the base-2 logarithm is calculated. |
out |
Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. |
Return Value
Returns the base-2 logarithm of each element of x.
Example:
In the example below, numpy log2() function is used to calculate the base-2 logarithm of each element present in array Arr.
import numpy as np Arr = np.array([1, 2, 10, 16]) print("The base-2 logarithm of values:") print(np.log2(Arr))
The output of the above code will be:
The base-2 logarithm of values: [ 0. 1. 3.32192809 4. ]
❮ NumPy - Functions