Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Scikit learn Scikit learn LossFunction

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Loss Functions, Optimization
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete module providing loss function classes for fitting regression and classification models in scikit-learn.

Description

The _loss/loss module provides a common private framework for loss functions used across scikit-learn estimators including LogisticRegression, PoissonRegressor, GammaRegressor, TweedieRegressor, and gradient boosting models. The BaseLoss class uses composition with Cython loss implementations and link functions. Specific loss classes include HalfSquaredError, HalfBinomialLoss, HalfPoissonLoss, HalfGammaLoss, HalfMultinomialLoss, and more.

Usage

These loss classes are used internally by scikit-learn estimators. They provide gradient, hessian, and loss computation with support for sample weights. Use them when implementing custom gradient-based estimators that need standardized loss computation.

Code Reference

Source Location

Signature

class BaseLoss:
    # Conventions:
    #   y_true.shape = sample_weight.shape = (n_samples,)
    #   y_pred.shape = raw_prediction.shape = (n_samples,)
    def __init__(self, closs, link, n_classes=None):
        ...
    def loss(self, y_true, raw_prediction, sample_weight=None, ...):
        ...
    def gradient(self, y_true, raw_prediction, sample_weight=None, ...):
        ...
    def gradient_hessian(self, y_true, raw_prediction, sample_weight=None, ...):
        ...

class HalfSquaredError(BaseLoss):
    ...

class HalfBinomialLoss(BaseLoss):
    ...

class HalfMultinomialLoss(BaseLoss):
    ...

class HalfPoissonLoss(BaseLoss):
    ...

class HalfGammaLoss(BaseLoss):
    ...

Import

from sklearn._loss.loss import BaseLoss, HalfSquaredError, HalfBinomialLoss

I/O Contract

Inputs

Name Type Required Description
y_true ndarray of shape (n_samples,) Yes True target values
raw_prediction ndarray of shape (n_samples,) Yes Raw prediction values (before link function)
sample_weight ndarray of shape (n_samples,) No Per-sample weights

Outputs

Name Type Description
loss float Aggregated loss value
gradient ndarray of shape (n_samples,) Gradient of the loss w.r.t. raw_prediction
hessian ndarray of shape (n_samples,) Hessian (second derivative) of the loss

Usage Examples

Basic Usage

import numpy as np
from sklearn._loss.loss import HalfSquaredError

loss_fn = HalfSquaredError()
y_true = np.array([1.0, 2.0, 3.0])
raw_prediction = np.array([1.1, 1.9, 3.2])

loss_value = loss_fn.loss(y_true, raw_prediction)
gradient = loss_fn.gradient(y_true, raw_prediction)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment