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

ParameterTypeDescription
xarray_likeInput values for which base-2 logarithm is computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the logarithm.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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