CompML
CompML is a Python library to compare machine learning algorithms through frequentist and bayesian statistics.
It helps you organize evaluation scores, run statistical tests, and create clear result reports.
Table of Contents
Why CompML
When two or more machine learning algorithms have close scores, raw metrics are often not enough. CompML helps you determine whether observed differences are meaningful, account for uncertainty, and make more reliable algorithm selection decisions.
Main Features
- Frequentist tests (for example Friedman, Wilcoxon Signed Rank, Paired t-test).
- Frequentist post-hoc procedures (for example Holm, Nemenyi, Bonferroni).
- Bayesian tests with ROPE-based decisions (for example Bayesian Signed Rank).
- Score containers (
EvaluationScores and CVScores) with save/load support.
- Optional scikit-learn helpers for hold-out and cross-validation workflows.
- PDF report generation for sharing results.
Supported Statistical Procedures
Tests
| Procedure class |
Name |
Category |
Family |
DAgostinoPearsonTest |
D’Agostino-Pearson normality test |
Normality |
Frequentist |
KolmogorovSmirnovTest |
Kolmogorov-Smirnov test |
Normality |
Frequentist |
ShapiroWilkTest |
Shapiro-Wilk test |
Normality |
Frequentist |
BartlettTest |
Bartlett’s test |
Homoscedasticity |
Frequentist |
LeveneTest |
Levene’s test |
Homoscedasticity |
Frequentist |
CorrelatedTTest |
Correlated t-test |
Parametric |
Frequentist |
PairedTTest |
Paired t-test |
Parametric |
Frequentist |
RepeatedMeasuresANOVATest |
Repeated-measures ANOVA |
Parametric |
Frequentist |
MannWhitneyUTest |
Mann-Whitney U test |
Non-parametric |
Frequentist |
SignTest |
Sign test |
Non-parametric |
Frequentist |
SignedRankTest |
Wilcoxon signed-rank test |
Non-parametric |
Frequentist |
FriedmanTest |
Friedman test |
Non-parametric |
Frequentist |
FriedmanAlignedRanksTest |
Friedman aligned ranks test |
Non-parametric |
Frequentist |
KruskalWallisTest |
Kruskal-Wallis test |
Non-parametric |
Frequentist |
QuadeTest |
Quade test |
Non-parametric |
Frequentist |
BayesianCorrelatedTTest |
Bayesian correlated t-test |
ROPE-based |
Bayesian |
BayesianSignTest |
Bayesian sign test |
ROPE-based |
Bayesian |
BayesianSignedRankTest |
Bayesian signed-rank test |
ROPE-based |
Bayesian |
BayesianHierarchicalTest |
Bayesian hierarchical test |
ROPE-based |
Bayesian |
BayesianFriedmanTest |
Bayesian Friedman test |
Rank-based |
Bayesian |
Post-hoc methods
| Procedure class |
Name |
Family |
BonferroniPostHoc |
Bonferroni post-hoc |
Frequentist |
FinnerPostHoc |
Finner post-hoc |
Frequentist |
HochbergPostHoc |
Hochberg post-hoc |
Frequentist |
HollandPostHoc |
Holland post-hoc |
Frequentist |
HolmPostHoc |
Holm post-hoc |
Frequentist |
HommelPostHoc |
Hommel post-hoc |
Frequentist |
LiPostHoc |
Li post-hoc |
Frequentist |
NemenyiPostHoc |
Nemenyi post-hoc |
Frequentist |
RomPostHoc |
Rom post-hoc |
Frequentist |
ShafferPostHoc |
Shaffer post-hoc |
Frequentist |
BayesianPostHoc |
Bayesian post-hoc |
Bayesian |
Installation
Install with uv (recommended)
uv add compml
Optional extra:
uv add "compml[scikit-learn]"
Install with pip
pip install compml
Optional extra:
pip install "compml[scikit-learn]"
Quick Start
This example compares two machine learning algorithms across three datasets. For each dataset,
each algorithm’s scores are stored as NumPy arrays, which are then combined into the scores
input for EvaluationScores. Using that object, we run two statistical tests:
- Frequentist Wilcoxon signed-rank test (
SignedRankTest)
- Bayesian signed-rank test (
BayesianSignedRankTest)
import numpy as np
from compml.evaluation_scores import EvaluationScores
from compml.frequentist.tests import SignedRankTest
from compml.bayesian.tests import BayesianSignedRankTest
# Per-dataset scores for each algorithm
ds1_alg_a = np.array([0.87, 0.85, 0.86, 0.88, 0.84])
ds1_alg_b = np.array([0.84, 0.83, 0.82, 0.85, 0.81])
ds2_alg_a = np.array([0.79, 0.80, 0.78, 0.81, 0.80])
ds2_alg_b = np.array([0.77, 0.76, 0.78, 0.75, 0.77])
ds3_alg_a = np.array([0.91, 0.90, 0.92, 0.89, 0.90])
ds3_alg_b = np.array([0.88, 0.87, 0.89, 0.86, 0.88])
# scores shape: (n_datasets, n_algorithms, n_samples)
scores = np.array(
[
[ds1_alg_a, ds1_alg_b],
[ds2_alg_a, ds2_alg_b],
[ds3_alg_a, ds3_alg_b],
],
dtype=float,
)
evaluation_scores = EvaluationScores(
scores=scores,
metric="accuracy",
dataset_names=("ds_1", "ds_2", "ds_3"),
algorithm_names=("alg_a", "alg_b"),
)
# Frequentist Wilcoxon signed-rank test
signed_rank_result = SignedRankTest(alpha=0.05).run(evaluation_scores)
print(signed_rank_result.summary())
# Bayesian signed-rank test
bayes_signed_rank_result = BayesianSignedRankTest(
rope=0.01,
n_posterior_samples=1000
).run(evaluation_scores)
print(bayes_signed_rank_result.summary())
Output:
SignedRankTest (metric='accuracy')
Datasets: ds_1, ds_2, ds_3
Algorithms: alg_a, alg_b
Alpha: 0.050
Statistic: 0.000
P-value: 0.109
C-value: -0.667
Reject null: False
BayesianSignedRankTest (metric='accuracy')
Datasets: ds_1, ds_2, ds_3
Algorithms: alg_a, alg_b
ROPE: [-0.010, 0.010]
Posterior probabilities: p_left=0.000, p_rope=0.107, p_right=0.893
Decision: alg_a is better
Scikit-Learn Workflow
If you install the scikit-learn extra, you can generate CVScores directly from estimators and datasets.
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from compml.frequentist.tests import CorrelatedTTest
from compml.sklearn_utils import run_cross_val
# X and y come from your own dataset
# datasets is a sequence of (X, y) pairs
datasets = ((X, y),)
cv_scores = run_cross_val(
estimators=(
DummyClassifier(strategy="most_frequent"),
LogisticRegression()
),
algorithm_names=("dummy", "logreg"),
datasets=datasets,
dataset_names=("my_dataset",),
scoring="accuracy",
cv=5,
)
result = CorrelatedTTest(alpha=0.05).run(cv_scores)
print(result.summary())
Create a PDF Report
You can export one or more results to a PDF file.
from compml.report import Report
report = Report(
path="results/comparison_report.pdf",
title="Comparison Report",
author="Your Name",
)
report.add_section_header("Frequentist Results")
report.add_result(friedman_result, include_plots=False)
report.add_section_header("Bayesian Results")
report.add_result(bayes_result, include_plots=True)
output_path = report.build()
print(f"Report saved to: {output_path}")
Python Version
- Python 3.12 or newer is required.
Project Links
License
CompML is distributed under the MIT License. See LICENSE for details.
评论