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

SciPy · Function

special.logsumexp

Computes the logarithm of a sum of exponentials in a numerically stable way. It supports axis reduction, broadcastable weights, singleton-dimension preservation, and optional sign output.

How it works

logsumexp evaluates the logarithm of a sum of exponentials while avoiding the direct overflow risks of forming those exponentials first. Reduce selected axes, optionally apply broadcastable weights—including negative weights—and request a separate sign when the weighted result may be nonpositive.

Syntax

scipy.special.logsumexp(a, axis=None, b=None, keepdims=False, return_sign=False)

Parameters

NameTypeDescription
aarray_likeInput array.
axisNone or int or tuple of ints, optionalAxis or axes over which the sum is taken. None, an integer, or a tuple of integers.
barray-like, optionalScaling factors for exp(a), with the same shape as a or a broadcast-compatible shape. Negative values are allowed.
keepdimsbool, optionalIf True, retain reduced axes as dimensions of size one.
return_signbool, optionalIf True, return sign information along with the logarithmic magnitude.

Returns

If return_sign is False, the logarithmic result is returned. If return_sign is True, a pair containing the logarithmic absolute value and sign information is returned.

Key points

  • The function returns the logarithm of the sum of exponentials for the supplied input.
  • axis controls which dimension or dimensions are reduced.
  • b provides elementwise scaling factors and must have a compatible shape with a.
  • keepdims=True preserves reduced axes as singleton dimensions.
  • When return_sign=True, the returned logarithmic value is for the absolute magnitude, accompanied by sign information.

Common mistakes

  • Negative b values enable subtraction, but a nonpositive weighted sum needs return_sign=True to preserve its sign rather than producing NaN.
  • With return_sign=False, a negative result is represented as NaN.

Example: Stable weighted log-sum-exp

import numpy as np
from scipy.special import logsumexp

a = np.array([1000.0, 1001.0])
print(logsumexp(a))

weights = np.array([1.0, -0.5])
log_abs, sign = logsumexp(a, b=weights, return_sign=True)
print(log_abs, sign)

Computes a stable log-sum-exp and separately returns the sign for a weighted sum that can be negative.

When to use it

Avoid overflow when combining exponentials

Large-magnitude log-domain values

Compute weighted log-sums, including subtraction

Weighted likelihoods or signed terms

Reduce selected axes while preserving broadcastable dimensions

Tensor reductions

Interview questions

QWhat does scipy.special.logsumexp compute?

It computes log(sum(exp(a))), with optional weights through b. The routine is useful when exponentials could overflow, because it evaluates the expression in a numerically stable form.

Follow-up: How would you represent a negative weighted sum? Use return_sign=True; the result contains the log of the absolute value and a separate sign value.

QHow do axis and keepdims affect the output?

axis selects the dimensions reduced by the sum. It accepts None, one integer, or a tuple of integers; None reduces all elements.

Follow-up: How can the result retain reduced dimensions for broadcasting? Set keepdims=True, which leaves those axes with size one.

QHow are weighted sums handled?

b supplies scaling factors for exp(a), and it must match a's shape or be broadcastable to it. Negative factors are allowed, so b can express subtraction as well as weighted addition.

Follow-up: What happens if the weighted sum is negative and return_sign is false? The returned result is NaN; request the sign explicitly to avoid losing that information.

  • scipy.special.softmax
  • scipy.special.log_softmax
  • numpy.logaddexp

Source: logsumexp. Checked 2026-10-15.