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

NumPy · Function

np.log

Computes the natural logarithm, base e, element-wise. It accepts scalars and array-like inputs and exposes standard NumPy ufunc options for masking, output storage, casting, and dtype control.

How it works

Call np.log with a scalar or array-like input to compute the natural logarithm of each value. Optional ufunc controls such as out, where, dtype, and casting govern destination storage, selected elements, output type, and conversion rules.

Syntax

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

Parameters

NameTypeDescription
xarray_likeInput values.
outndarray, None, or tuple of ndarray and NoneOptional output array or tuple of arrays and None.
wherearray_like of bool, optionalCondition broadcast over the input; only true locations are evaluated.
casting{'no','equiv','safe','same_kind','unsafe'}, optionalControls casting used for the computation.
order{'K','A','C','F'}, optionalMemory layout order for the result.
dtypedtype, optionalOutput data type.
subokbool, optionalWhether subclasses are passed through.
signaturestring or None, optionalOptional generalized-ufunc signature.

Returns

An ndarray containing the natural logarithm of x element-wise, or a scalar when x is scalar.

Key points

  • np.log applies the base-e logarithm independently to every input element.
  • Its mathematical inverse relationship is log(exp(x)) = x.
  • Use np.log for natural logarithms rather than base-10 or base-2 logarithms.
  • Scalar input produces a scalar result; array input produces an array-shaped result.
  • The returned values preserve the element-wise correspondence with the input.

Example: Element-wise natural logarithms

import numpy as np

x = np.array([1.0, np.e, np.e**2, 0.0])
y = np.log(x)
print(y)  # [  0.   1.   2. -inf]

Computes natural logarithms element by element; zero produces negative infinity.

When to use it

Log transformation

Transforming positive measurements or model features whose multiplicative relationships are easier to analyze additively.

Scientific and statistical formulas

Calculating natural-log terms in probability, optimization, and scientific formulas.

Vectorized array computation

Applying one mathematical operation efficiently across an ndarray without an explicit Python loop.

Interview questions

QWhat does np.log calculate?

np.log computes the natural logarithm, base e, element-wise. It is the inverse of the exponential function, so log(exp(x)) equals x mathematically.

Follow-up: How would you compute another base? Divide np.log(x) by np.log(base), or use np.log10 or np.log2 when those bases are appropriate.

QWhat happens for scalar, array, zero, and negative inputs?

For a scalar input, the result is a scalar; for array input, NumPy returns the element-wise logarithms in an ndarray. Zero maps to negative infinity.

Follow-up: What should be done for negative real data? Use np.emath.log if complex-valued logarithms are intended.

QWhich advanced controls matter when using np.log?

The ufunc accepts an out destination and a where mask, along with dtype, casting, order, and subok controls. where selects which elements are evaluated or written.

Follow-up: Why can masked output contain unexpected values? If out is newly allocated, locations skipped by where remain uninitialized unless initialized first.

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

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