liveBook · Manning

Class imbalance

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

Imbalanced data has substantially unequal representation of classes or outcome groups.

Class imbalance occurs when outcome classes are represented unequally. The practical risk is that a classifier can favor the majority class, making accuracy less informative than metrics that assess ranking and minority detection. section 4.2.1

Choose evaluation measures according to the target distribution:

  • Use accuracy for a balanced binary target, where positive and negative cases occur in nearly equal numbers. section 4.2.1
  • Prefer ROC-AUC or Average Precision for an imbalanced target, such as one with substantially more cases below than above the mean. section 4.2.1
  • Consider extremely randomized trees when random feature selection may prevent minority-class signals from being consistently displaced by majority-class signals. section 5.1.3

A skewed price target

Converting Airbnb listing prices into a binary target above or below the mean produces 34,016 negative cases and 14,879 positive cases. By contrast, splitting prices at the median produces 24,472 and 24,423 cases, making accuracy a safer measure for that target. section 4.2.1

Listing 5.5 ERTs classifier
from sklearn.ensemble import ExtraTreesClassifier
 
accuracy = make_scorer(accuracy_score)
cv = KFold(5, shuffle=True, random_state=0)
model = ExtraTreesClassifier(n_estimators=300, 
                             min_samples_leaf=3,
                             random_state=0)                  #1
 
column_transform = ColumnTransformer(
    [('categories', categorical_onehot_encoding, low_card_categorical),
     ('numeric', numeric_passthrough, continuous)],
    remainder='drop',
    verbose_feature_names_out=False,
    sparse_threshold=0.0)                                     #2
 
model_pipeline = Pipeline(
    [('processing', column_transform),
     ('modeling', model)])                                    #3
 
cv_scores = cross_validate(estimator=model_pipeline, 
                           X=data, 
                           y=target_median,
                           scoring=accuracy,
                           cv=cv, 
                           return_train_score=True,
                           return_estimator=True)             #4
 
mean_cv = np.mean(cv_scores['test_score'])
std_cv = np.std(cv_scores['test_score'])
fit_time = np.mean(cv_scores['fit_time'])
score_time = np.mean(cv_scores['score_time'])
print(f"{mean_cv:0.3f} ({std_cv:0.3f})", 
      f"fit: {fit_time:0.2f}",
      f"secs pred: {score_time:0.2f} secs")                   #5

Run an ExtraTreesClassifier pipeline on the balanced median-price target, then adapt the evaluation choice when the target distribution is skewed. section 5.1.3

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Compares balanced and imbalanced target counts, recommends evaluation metrics, and briefly explains ExtraTrees handling without detailing resampling.
Examples
Uses binary price targets split at the mean and median, with explicit class counts and an ExtraTrees classifier pipeline.
What you can do after
Evaluate classifiers with metrics suited to class proportions and test ExtraTrees when minority signals may be overlooked.
How it connects
Connects class imbalance with classification, accuracy, ROC-AUC, Average Precision, random forests, and extremely randomized trees.

Also explained in

Compare how 11 books explain class imbalance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.