Probability-based model evaluation
Evaluate binary or multiclass classifiers that expose predict_proba.
Scikit-Learn · Function
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.
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.
sklearn.metrics.log_loss(y_true, y_proba=None, *, normalize=True, sample_weight=None, labels=None, y_pred='deprecated')| Name | Type | Description |
|---|---|---|
y_true | array-like or label indicator matrix | Ground-truth labels or a label-indicator matrix for the samples. |
y_proba | array-like of float, shape (n_samples, n_classes) or (n_samples,) | Predicted class probabilities. |
normalize | bool, default=True | Whether to return the mean instead of the sum of per-sample losses. |
sample_weight | array-like of shape (n_samples,), default=None | Per-sample contribution weights. |
labels | array-like, default=None | Class labels used to interpret the probability columns; inferred from y_true when omitted. |
y_pred | array-like, default='deprecated' | Deprecated alias for y_proba. |
float: log loss, also known as logistic loss or cross-entropy loss.
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.
Evaluate binary or multiclass classifiers that expose predict_proba.
Compare whether predicted probabilities are well aligned with observed class labels.
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.
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.
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_scoresklearn.metrics.brier_score_losssklearn.metrics.roc_auc_score