Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn ComputeClassWeight

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Class Imbalance
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for estimating class weights for unbalanced datasets, provided by scikit-learn.

Description

The compute_class_weight function estimates class weights for unbalanced datasets. When set to "balanced", weights are computed as n_samples / (n_classes * np.bincount(y)), inversely proportional to class frequencies. It also supports custom weight dictionaries and optional sample weights. The "balanced" heuristic is inspired by King and Zen (2001).

Usage

Use this function to compute class weights that can be passed to classifiers (via the class_weight parameter) to handle class imbalance in classification tasks.

Code Reference

Source Location

Signature

@validate_params(...)
def compute_class_weight(class_weight, *, classes, y, sample_weight=None):

Import

from sklearn.utils.class_weight import compute_class_weight

I/O Contract

Inputs

Name Type Required Description
class_weight dict, "balanced", or None Yes Weight strategy: dict of class-to-weight, "balanced" for auto-computed, or None for uniform
classes ndarray Yes Array of classes occurring in the data
y array-like of shape (n_samples,) Yes Array of original class labels per sample
sample_weight array-like of shape (n_samples,) or None No Per-sample weights, only used when class_weight='balanced' (default None)

Outputs

Name Type Description
class_weight_vect ndarray of shape (n_classes,) Array of computed class weights

Usage Examples

Basic Usage

import numpy as np
from sklearn.utils.class_weight import compute_class_weight

y = [1, 1, 1, 1, 0, 0]
classes = np.unique(y)
weights = compute_class_weight("balanced", classes=classes, y=y)
print(weights)  # [1.5, 0.75] - higher weight for minority class

Related Pages

Page Connections

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