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

SciPy · Function

special.softmax

Computes softmax by exponentiating input values and dividing by the sum of exponentials along a selected axis. The output has the same shape as the input and sums to 1 along that axis.

How it works

softmax maps values to an array of the same shape by exponentiating elements and dividing by the sum of exponentials along the chosen axis. With the default axis, the calculation covers the entire input array; specifying an axis applies it along that dimension.

Syntax

scipy.special.softmax(x, axis=None)

Parameters

NameTypeDescription
xarray_likeInput array.
axisint or tuple of ints, optionalAxis or axes along which to compute softmax. Defaults to None, meaning the entire array.

Returns

ndarray: An array with the same shape as x; values sum to 1 along the specified axis.

Key points

  • scipy.special.softmax converts a collection of values into normalized exponential weights.
  • axis can specify the dimension used for normalization, or a tuple of dimensions.
  • The output preserves the input shape.
  • Values normalize to a sum of one along the selected axis.
  • SciPy documents softmax as the gradient of logsumexp.

Common mistakes

  • Leaving axis unset normalizes the whole array rather than each row or column.

Example: Normalize scores by row

import numpy as np
from scipy.special import softmax

scores = np.array([[1.0, 2.0, 3.0], [3.0, 2.0, 1.0]])
probabilities = softmax(scores, axis=1)

print(probabilities)
print(probabilities.sum(axis=1))

Computes a probability distribution independently for each row and verifies that each row sums to one.

When to use it

Classification probabilities

Convert model scores or logits into normalized class probabilities.

Axis-wise normalization

Normalize values independently across rows or another selected dimension.

Exponential weighting

Construct differentiable normalized weights from arbitrary scores.

Interview questions

QHow does scipy.special.softmax compute its output?

softmax exponentiates each input element and divides it by the sum of all exponentials in the selected computation scope. For a vector, the result is e^{x_j}/\sum_k e^{x_k}.

Follow-up: What does axis control? It selects the dimension or dimensions along which the normalization is performed; omitting it uses the entire array.

QWhat shape and normalization property does softmax return?

The result has the same shape as the input array. Along the specified axis, its values sum to 1.

Follow-up: How would you normalize every row of a matrix? Pass axis=1 so each row is treated as a separate collection.

QWhat are softmax's parameters and its key related function?

The function accepts an array-like x and an optional integer or tuple of integers axis. axis defaults to None, which means computation over the entire array.

Follow-up: What related SciPy function is mathematically connected to softmax? softmax is the gradient of logsumexp.

  • scipy.special.logsumexp

Source: scipy.special.softmax. Checked 2026-10-15.