Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.log2

Computes the base-2 logarithm of each input value. It is a NumPy ufunc that supports scalar and array inputs, with standard controls for output storage, masking, casting, dtype, and subclasses.

Since
1.3.0

How it works

numpy.log2 applies the base-2 logarithm element by element to its input. It accepts scalars and array-like values, supports optional output storage and masked evaluation, and returns a scalar for scalar input or an array for array input.

Syntax

numpy.log2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature])

Parameters

NameTypeDescription
xarray_likeInput value or array.
outndarray, None, or tuple of ndarray and NoneOptional output destination.
wherearray_like of boolBoolean mask selecting where the result is computed.
casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optionalCasting rule for operands.
order{‘K’, ‘A’, ‘C’, ‘F’}, optionalMemory layout order for the result.
dtypedtype, optionalOutput data type.
subokbool, optionalWhether to return subclasses of input arrays.
signatureNone or str, optionalOptional generalized ufunc signature.

Returns

ndarray: Base-2 logarithm of x; this is a scalar if x is a scalar.

Key points

  • It calculates logarithm base 2 for each input value.
  • The documented call accepts the input plus standard ufunc controls such as out, where, casting, order, dtype, and subok.
  • Use out to write computed values into an existing destination.
  • Real-valued inputs yield real-valued results.
  • For complex results, NumPy uses the principal-value convention with imaginary parts in the interval [-π, π].

Common mistakes

  • Real inputs produce real output; use complex-valued input when complex logarithms are required.
  • Do not assume the result is always an ndarray: scalar input produces a scalar.

Example: Compute powers of two

import numpy as np

x = np.array([1, 2, 4, 16])
y = np.log2(x)
print(y)  # [0. 1. 2. 4.]

Computes the base-2 logarithm element by element.

When to use it

Binary-scale calculations

Convert powers-of-two quantities into exponents, such as determining bit widths or binary scales.

Logarithmic data transformation

Transform positive numeric arrays when a base-2 logarithmic scale is needed.

Complex-valued analysis

Compute complex logarithms using NumPy's documented principal-value convention.

Interview questions

QWhat does numpy.log2 compute, and what kind of operation is it?

np.log2(x) computes the base-2 logarithm of x element by element. It is a NumPy ufunc, so it works with scalars and arrays.

Follow-up: What happens for a scalar input? The result is a scalar rather than an ndarray.

QWhat values does np.log2 return for powers of two and zero?

For an array such as [0, 1, 2, 16], the results are [-inf, 0., 1., 4.]. Zero therefore produces negative infinity in the documented example.

Follow-up: How can computation be restricted to selected elements? Supply a Boolean where mask and optionally an out array.

QWhich important ufunc options does numpy.log2 expose?

The function accepts out for placing results into an existing destination and where for selecting where the ufunc is evaluated. Other ufunc controls include casting, order, dtype, and subok.

Follow-up: Why might out be useful? It can avoid allocating a separate result array when writing into a prepared destination is appropriate.

  • numpy.log
  • numpy.log10
  • numpy.log1p
  • numpy.emath.log2

Source: numpy.log2. Checked 2026-10-15.