Compute scikit-learn Log Loss When Only One Class Is Present

Quick Overview

Compute scikit-learn's log loss for a batch whose true labels are all one class, where the default call raises an error. It tests how the metric infers classes, how a 1-D probability column maps to the positive class, and how clipping bounds the loss.

Compute scikit-learn Log Loss When Only One Class Is Present

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `log_loss_single_class(y_true, y_pred)` that returns `sklearn.metrics.log_loss` when `y_true` (shape `(n,)`) is all `1` and `y_pred` holds positive-class probabilities strictly inside `(0, 1)`. A naive call raises `ValueError: y_true contains only one label (1)`. Pass `labels=[0, 1]` explicitly and return the mean loss as a Python `float`. ```text Input: y_true = [1, 1, 1], y_pred = [0.9, 0.8, 0.7] Output: 0.22839300363692283 ``` ```hint What the metric can see Think about how `log_loss` decides which classes exist, and how it knows which class a 1-D column of probabilities refers to. ``` ### Constraints & Assumptions - `y_true` has shape `(n,)` and every entry is 1. - `y_pred` has shape `(n,)` and holds `P(y=1)`, strictly inside `(0, 1)`. - The value must come from `sklearn.metrics.log_loss` with `labels=[0, 1]` passed explicitly. ### What a Strong Answer Covers - Why the default call fails on a single-class batch. - What `labels=[0, 1]` tells the metric, including which class a 1-D `y_pred` refers to. - What each sample contributes when every label is 1, checked against the example. - Returning a Python `float`. - Where single-class batches come up in practice, and how predictions at exactly 0 or 1 are handled. ### Follow-up Questions - Why does `log_loss` need `labels` when only one class is present? - How does clipping affect the result when a prediction is exactly 0 or 1? - What changes if the batch contains only 0 labels?

Overview: Compute scikit-learn's log loss for a batch whose true labels are all one class, where the default call raises an error. It tests how the metric infers classes, how a 1-D probability column maps to the positive class, and how clipping bounds the loss.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement log_loss_single_class(y_true, y_pred) that returns sklearn.metrics.log_loss when y_true (shape (n,)) is all 1 and y_pred holds positive-class probabilities strictly inside (0, 1). A naive call raises ValueError: y_true contains only one label (1). Pass labels=[0, 1] explicitly and return the mean loss as a Python float.

Input: y_true = [1, 1, 1], y_pred = [0.9, 0.8, 0.7]
Output: 0.22839300363692283

Constraints & Assumptions

  • y_true has shape (n,) and every entry is 1.
  • y_pred has shape (n,) and holds P(y=1) , strictly inside (0, 1) .
  • The value must come from sklearn.metrics.log_loss with labels=[0, 1] passed explicitly.

What a Strong Answer Covers Guidance

  • Why the default call fails on a single-class batch.
  • What labels=[0, 1] tells the metric, including which class a 1-D y_pred refers to.
  • What each sample contributes when every label is 1, checked against the example.
  • Returning a Python float .
  • Where single-class batches come up in practice, and how predictions at exactly 0 or 1 are handled.

Follow-up Questions Guidance

  • Why does log_loss need labels when only one class is present?
  • How does clipping affect the result when a prediction is exactly 0 or 1?
  • What changes if the batch contains only 0 labels?