Random Forest
class_weight
sample_weight
machine learning
sklearn

Random forest class_weight and sample_weight parameters

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Imbalanced classification data can make a random forest look accurate while still failing on the minority class. In scikit-learn, class_weight and sample_weight are two different levers for this problem. They sound similar, but they operate at different levels and should be chosen based on whether imbalance is class-wide or record-specific. Understanding that difference early prevents weeks of misleading evaluation and unnecessary model complexity.

What class_weight Changes

class_weight applies a multiplier per class label. During split evaluation, mistakes on higher-weight classes cost more, so trees are encouraged to create boundaries that better protect those classes.

The most common option is class_weight="balanced", which scales each class inversely to its frequency in the training data. This is a fast baseline when imbalance is global and you do not have extra confidence signals per row.

python
1from sklearn.datasets import make_classification
2from sklearn.ensemble import RandomForestClassifier
3from sklearn.model_selection import train_test_split
4from sklearn.metrics import classification_report
5
6X, y = make_classification(
7    n_samples=5000,
8    n_features=20,
9    n_informative=6,
10    n_redundant=2,
11    weights=[0.95, 0.05],
12    random_state=42
13)
14
15X_train, X_test, y_train, y_test = train_test_split(
16    X, y, test_size=0.25, stratify=y, random_state=42
17)
18
19clf = RandomForestClassifier(
20    n_estimators=300,
21    class_weight="balanced",
22    random_state=42,
23    n_jobs=-1
24)
25
26clf.fit(X_train, y_train)
27pred = clf.predict(X_test)
28print(classification_report(y_test, pred, digits=3))

This often improves minority recall, though precision may drop. Evaluate with class-specific metrics, not only overall accuracy.

What sample_weight Changes

sample_weight is passed to fit and weights individual training rows. Use it when some records are more trustworthy, more expensive to misclassify, or need temporal emphasis.

For example, you might upweight recent fraud records or downweight noisy labels.

python
1import numpy as np
2from sklearn.ensemble import RandomForestClassifier
3
4clf = RandomForestClassifier(
5    n_estimators=300,
6    random_state=42,
7    n_jobs=-1
8)
9
10# baseline all ones
11weights = np.ones_like(y_train, dtype=float)
12
13# emphasize minority class records
14weights[y_train == 1] = 8.0
15
16clf.fit(X_train, y_train, sample_weight=weights)

With sample_weight, you can go beyond class imbalance. You can express business value per row, confidence by source system, or freshness effects in evolving datasets.

Using Both Together

You can combine class_weight and sample_weight. Internally, they interact multiplicatively during training. That can be useful, but it can also over-amplify minority or high-cost regions if you are not careful.

A practical approach:

  1. start with class_weight="balanced" alone.
  2. tune core tree settings such as depth and minimum leaf size.
  3. add sample_weight only when you have a clear row-level rationale.
  4. re-evaluate calibration and threshold performance.

Do not assume higher weighting is always better. Very aggressive weighting can increase variance and false positives.

Evaluation Strategy That Matches Imbalance

Use metrics that reflect the target decision quality:

  • recall and precision for minority class.
  • PR-AUC for rare positive cases.
  • confusion matrix at your operating threshold.
  • cost-based score when false positives and false negatives have different impact.

For thresholded decisions, tune threshold after model training, not during random forest fitting. Weighting influences learned probabilities and ranking, while threshold controls business trade-off at inference time.

Common Pitfalls

  • Judging success by overall accuracy on highly imbalanced data.
  • Using both class_weight and heavy sample_weight without checking overcorrection.
  • Forgetting stratified splitting and creating unstable evaluation sets.
  • Assuming weighting replaces feature engineering and data-quality cleanup.
  • Not recalibrating probability thresholds after changing class or sample weights.

Summary

  • class_weight handles class-level imbalance and is a strong first baseline.
  • sample_weight handles row-level importance and business-specific costs.
  • Both can be combined, but over-weighting can hurt precision and stability.
  • Evaluate with minority-focused metrics, not only aggregate accuracy.
  • Tune model settings and decision threshold together for production performance.

Course illustration
Course illustration

All Rights Reserved.