scikit-learn
machine learning
sample_weight
class_weight
Python

How does sample_weight compare to class_weight in scikit-learn?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

sample_weight and class_weight both change how much influence training data has, but they operate at different levels. class_weight assigns one weight per class label, while sample_weight lets you weight individual rows directly. The right choice depends on whether your problem is “this class is rare” or “these specific observations should count more or less”.

class_weight Is a Class-Level Shortcut

class_weight is used in classification estimators that support it. Instead of weighting each row manually, you tell the estimator how important each class should be.

python
from sklearn.linear_model import LogisticRegression

model = LogisticRegression(class_weight={0: 1.0, 1: 4.0}, max_iter=1000)

In this example, mistakes on class 1 are treated as more expensive than mistakes on class 0. This is a natural fit for imbalanced classification when the minority class should not be overwhelmed by the majority.

Many estimators also support class_weight="balanced", which computes class weights from the observed class frequencies automatically.

sample_weight Works at the Row Level

sample_weight is passed during fit, and it lets you control the importance of each training example individually.

python
1import numpy as np
2from sklearn.tree import DecisionTreeClassifier
3
4X = np.array([[0], [1], [2], [3]])
5y = np.array([0, 0, 1, 1])
6weights = np.array([1.0, 1.0, 5.0, 1.0])
7
8model = DecisionTreeClassifier(random_state=42)
9model.fit(X, y, sample_weight=weights)

Here, the third sample carries extra influence even though it belongs to the same class as the fourth sample. That is something class_weight alone cannot express.

This is useful when some observations are duplicated, noisy, more reliable, or more important for business reasons.

The Conceptual Difference

The clean way to think about them is:

  • 'class_weight answers “how important is each class overall”,'
  • 'sample_weight answers “how important is each row”.'

If every sample in a class should be treated equally, class_weight is the easier tool. If importance varies inside the same class, use sample_weight.

Some estimators effectively turn class_weight into per-sample weights internally based on the sample’s label. That is why the two ideas are related, but they are not identical in API meaning.

They Can Interact

For estimators that support both, the effective contribution of a sample may reflect both the class-level weight and the row-level weight. In practice, that means a minority-class sample can receive extra class weight, and then a specific example from that class can be emphasized even more with sample_weight.

The details depend on the estimator, so you should confirm support in that estimator’s documentation rather than assuming every model treats both parameters the same way.

class_weight Is Mostly a Classification Feature

class_weight is tied to labeled classes, so it is primarily a classification concept. sample_weight, on the other hand, appears in classification, regression, and sometimes scoring utilities because row-level weighting is more general.

That is a useful practical distinction. If you are solving a regression problem, class_weight is usually irrelevant by definition, but sample_weight may still be very valuable.

Pick the Simpler Tool That Matches the Problem

If your dataset is imbalanced and you have no special reason to distinguish one minority-class row from another, class_weight is usually the cleaner choice. If your dataset contains data-quality differences or business-specific row importance, sample_weight is usually the more precise tool.

Overcomplicating weighting is easy. Start with the simplest explanation of what needs more influence and choose the weighting mechanism that expresses exactly that.

Common Pitfalls

  • Using class_weight when the real issue is that only some individual rows are more important.
  • Using sample_weight to manually reproduce class balancing when the estimator already supports class_weight="balanced".
  • Assuming every scikit-learn estimator supports both weighting mechanisms in the same way.
  • Forgetting that class_weight applies only to classification tasks.
  • Treating weighting as a substitute for evaluating the right metrics on imbalanced data.

Summary

  • 'class_weight weights whole classes, while sample_weight weights individual rows.'
  • Use class_weight for class imbalance when all samples in a class should be treated similarly.
  • Use sample_weight when importance differs from one observation to another.
  • Some estimators can combine both effects, but support varies by model.
  • Pick the weighting mechanism that matches the actual source of imbalance or importance in the data.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.