Class Imbalance Calculator in Machine Learning

Measure class imbalance, calculate model weights, design sampling targets, test data splits, compare strategies, generate code, and improve evaluation decisions with greater confidence today.

Analysis Results

Results update after calculation and appear above the form.

Waiting for data

1. Dataset and Class Inputs

The calculator detects comma-separated counts or counts raw labels.

2. Imbalance Thresholds and Weighting

3. Sampling Planner

4. Split and Cross-Validation Analysis

5. Confusion Matrix and Cost Analysis

6. Code and Report Options

Class Distribution

Class Weights and Framework Values

Sampling Plan Comparison

Stratified Split Projection

Confusion Matrix Metrics

Recommendations and Leakage Warnings

Visualisations

Generated Machine Learning Code


        

Formula Used

Class Percentageᵢ = (Nᵢ ÷ N) × 100
Imbalance Ratio = Nmax ÷ Nmin
Balanced Weightᵢ = N ÷ (K × Nᵢ)
Samples Addedᵢ = max(0, Targetᵢ − Nᵢ)
Samples Removedᵢ = max(0, Nᵢ − Targetᵢ)
Normalised Entropy = −Σ(pᵢ ln pᵢ) ÷ ln(K)

How to Use

Enter each class name and sample count. You can also paste labels or import a CSV file. Keep class names unique for clear reports.

Choose thresholds, weight rules, and a sampling target. Configure train, validation, and test percentages. Then select your preferred code framework.

Press calculate to review imbalance, weights, sampling plans, split risks, and model guidance. Export results when your analysis is complete.

Example Data

ClassSample CountExpected Percentage
Normal9,50095%
Fraud5005%

This dataset has a 19:1 imbalance ratio. Balanced weights are approximately 0.5263 and 10. XGBoost scale_pos_weight equals 19.

Frequently Asked Questions

What is class imbalance?

Class imbalance occurs when labels contain very different sample counts. It can bias learning toward larger classes. Minority performance may remain hidden.

How is imbalance ratio calculated?

Divide the largest class count by the smallest positive class count. A value near one indicates balance. Larger values indicate stronger imbalance.

Is accuracy reliable for imbalanced data?

Accuracy can be misleading when one class dominates. Review recall, precision, balanced accuracy, MCC, and PR-AUC. Always inspect every important class.

Should oversampling happen before splitting?

No. Split the dataset first. Fit resampling only on training folds to prevent leakage.

When should SMOTE be used?

Use SMOTE when numeric minority examples are sufficient and meaningful interpolation is plausible. Validate carefully. Categorical data may require SMOTE-NC.

Can class weights replace resampling?

Sometimes. Weighted losses often work well with tree, linear, and neural models. Compare weighted and resampled pipelines using stratified validation.

Which metric is best?

There is no universal metric. Choose metrics around business costs and minority detection needs. PR-AUC is helpful for rare positives.

How is multiclass imbalance handled?

Inspect each class proportion, macro metrics, pairwise ratios, and class weights. Sampling targets can differ by class. Preserve test realism.

What is scale_pos_weight?

It is commonly calculated as negative samples divided by positive samples. XGBoost uses it to emphasise the positive class during learning.

Can oversampling cause overfitting?

Yes. Random duplication can amplify noise. Synthetic methods can also create unrealistic examples when classes overlap.

How many minority samples are enough?

The answer depends on feature complexity, noise, and validation stability. Very small classes need cautious claims. More representative data is preferable.

Should the test set remain imbalanced?

Usually yes. The test distribution should represent expected deployment conditions. Report per-class results and uncertainty.

What is threshold moving?

Threshold moving changes the probability cutoff used for class decisions. It trades precision against recall. Tune only with validation data.

When is anomaly detection suitable?

Consider anomaly detection when positives are extremely rare or poorly labelled. It is also useful when normal behaviour is much easier to model.

Why use stratified cross-validation?

Stratification preserves class proportions across folds. It reduces unstable evaluation caused by missing minority samples. Group constraints still need separate handling.

Related Calculators

Train-Test Split CalculatorDataset Sample Size CalculatorFeature Scaling CalculatorMin-Max Normalization CalculatorZ-Score Standardization CalculatorOutlier Detection CalculatorInterquartile Range CalculatorSynthetic Oversampling CalculatorData Augmentation Size CalculatorFeature Variance Calculator

Important Note: All the Calculators listed in this site are for educational purpose only and we do not guarentee the accuracy of results. Please do consult with other sources as well.