NumPy log2()
The numpy.log2() function computes the base-2 logarithm of each element in an input array.
Syntax
</>
Copy
numpy.log2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x | array_like | Input values for which base-2 logarithm is computed. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the logarithm. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array containing the base-2 logarithm of the input values. If the input is a scalar, a scalar is returned.
Examples
1. Computing Base-2 Logarithm of a Single Value
Here, we compute the base-2 logarithm of a single number.
</>
Copy
import numpy as np
# Define a number
num = 8
# Compute the base-2 logarithm
result = np.log2(num)
# Print the result
print("log2(8):", result)
Output:
log2(8): 3.0
