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

Scikit-Learn · Function

log_loss

Computes classification log loss, also called logistic or cross-entropy loss, from true labels and predicted probabilities. It evaluates the negative log-likelihood of the true labels and returns the mean by default.

How it works

The function compares each true class with the probability assigned to it and averages the resulting negative log-likelihoods by default. It accepts binary or multiclass probabilities, optionally applies sample weights, and can return either the mean or the total loss.

Syntax

sklearn.metrics.log_loss(y_true, y_proba=None, *, normalize=True, sample_weight=None, labels=None, y_pred='deprecated')

Parameters

NameTypeDescription
y_truearray-like or label indicator matrixGround-truth labels or a label-indicator matrix for the samples.
y_probaarray-like of float, shape (n_samples, n_classes) or (n_samples,)Predicted class probabilities.
normalizebool, default=TrueWhether to return the mean instead of the sum of per-sample losses.
sample_weightarray-like of shape (n_samples,), default=NonePer-sample contribution weights.
labelsarray-like, default=NoneClass labels used to interpret the probability columns; inferred from y_true when omitted.
y_predarray-like, default='deprecated'Deprecated alias for y_proba.

Returns

float: log loss, also known as logistic loss or cross-entropy loss.

Key points

  • This metric is also called logistic loss or cross-entropy loss.
  • Use it for binary or multiclass classification, not a one-label target.
  • Predicted probabilities may be a multiclass matrix or a one-dimensional binary array.
  • The default output is the average loss across samples.
  • Set normalize=False to obtain the aggregate loss.

Common mistakes

  • At least two distinct labels are required.
  • Pass probabilities rather than hard class predictions.
  • Supply labels when inference from the true labels is insufficient, especially with omitted classes.

Example: Evaluate probabilistic predictions

from sklearn.metrics import log_loss

 y_true = [0, 0, 1, 1]
y_proba = [[0.9, 0.1], [0.8, 0.2], [0.3, 0.7], [0.01, 0.99]]

score = log_loss(y_true, y_proba)
print(score)

Computes the mean cross-entropy loss for four binary-classification probability predictions.

When to use it

Probability-based model evaluation

Evaluate binary or multiclass classifiers that expose predict_proba.

Assess probabilistic predictions

Compare whether predicted probabilities are well aligned with observed class labels.

Interview questions

QWhat does log_loss measure?

log_loss measures the negative log-likelihood of the true labels under predicted probabilities. Lower values are better because confident probability assigned to the correct class reduces the penalty.

Follow-up: Why can a confidently wrong prediction be worse than an uncertain one? The logarithm makes assigning near-zero probability to the true class produce a large loss.

QWhat is the effect of normalize?

By default, normalize=True returns the mean loss per sample. Setting normalize=False returns the sum of the per-sample losses.

Follow-up: When would the sum be useful? It preserves the total weighted objective, whereas the mean is easier to compare across datasets with different sample counts.

QWhat input format does y_proba use?

y_proba can be a probability matrix with shape (n_samples, n_classes), or a one-dimensional array for binary classification. Its values should represent classifier probabilities, typically obtained from predict_proba.

Follow-up: How can class interpretation be made explicit? Pass labels to define the class labels used for interpreting the probability columns.

  • sklearn.metrics.accuracy_score
  • sklearn.metrics.brier_score_loss
  • sklearn.metrics.roc_auc_score

Source: log_loss — scikit-learn 1.9.1 documentation. Checked 2026-10-15.