Skip to main content

A reproducible stability-selection pipeline for scientific machine learning

Project description

RobustModelMaker

License: MIT Python Version PyPI

A reproducible model-building pipeline for small-to-medium scientific datasets.

RobustModelMaker (ROBUST) combines bootstrap stability selection with leakage-safe nested cross-validation to identify a stable, minimal feature subset and produce honest performance estimates. It is designed for scientific datasets where reproducibility, interpretability, and honest generalisation estimates matter as much as raw predictive performance.


Why RobustModelMaker?

Standard machine learning pipelines applied to scientific data suffer from two problems that ROBUST addresses directly:

Optimistic performance estimates. When feature selection, hyperparameter tuning, and model evaluation share the same data, the reported score reflects the data used for model building, not future data. ROBUST uses strict nested cross-validation in which each of those steps is performed entirely on the training partition of each fold. The test partition is used only to evaluate the final fold model, never to inform any modelling decision.

Unstable feature selection. Single-run feature selection produces a feature set that can change substantially with small changes in the data. ROBUST runs bootstrap stability selection: features are ranked by how consistently they are selected across hundreds of random subsamples of the training data. Only features that exceed a stability threshold (70% of bootstrap runs by default) are retained.

The result is a model built on a smaller, more reproducible feature set whose estimated performance is trustworthy.


Installation

pip install robustmodelmaker

For XGBoost support:

pip install robustmodelmaker[xgb]

Requirements: Python >= 3.9, numpy, pandas, scikit-learn, scipy


Quick start

import pandas as pd
from robustmodelmaker import RobustModelMaker

X = pd.read_csv("features.csv")
y = pd.read_csv("labels.csv").squeeze()

maker = RobustModelMaker(
    alg="eln",           # elastic net: interpretable and fast
    task_type="binary",  # always set explicitly: "binary", "multiclass", or "regression"
    outer_cv=5,
    inner_cv=5,
    n_bootstrap=100,
    stability_threshold=0.7,
    random_state=42,
).fit(X, y)

result = maker.result_
print(f"Selected {len(result.selected_features)} of {len(result.feature_names)} features")
print(f"Nested CV AUC: {result.mean_score:.4f} +/- {result.std_score:.4f}")

# Predict on new data (preprocessing and feature selection applied automatically)
predictions = maker.predict(X_new)
probabilities = maker.predict_proba(X_new)

The functional API is also available:

from robustmodelmaker import run_pipeline

result = run_pipeline(X, y, alg="eln", task_type="binary",
                      outer_cv=5, inner_cv=5, random_state=42)

Algorithms

Code Model Tasks Notes
eln Elastic net all Fastest; coefficient-based importance; auto-scales
rdg Ridge (L2) all Stable; good default for many scientific datasets
las Lasso (L1) all Sparse coefficients; strong feature selector
log L2 logistic regression classification Reliable baseline
svm Linear SVM all Effective in high-dimensional spaces
rf Random forest all Non-linear; no scaling needed; class_weight balanced
xgb XGBoost all Highest raw performance; requires pip install robustmodelmaker[xgb]
mlp Multi-layer perceptron all Neural baseline; slower on small datasets
lin Linear regression (OLS) regression only Interpretable; no regularisation

Key capabilities

Capability Detail
Task types Binary classification, multiclass classification, regression
Feature selection Bootstrap stability selection with configurable threshold and bootstrap count
Performance estimation Nested CV (outer + inner), repeated nested CV, grouped CV
Preprocessing Median imputation + optional standard scaling, fitted inside each fold
Missing data NaN-tolerant by default; optional data-driven missingness filter
Cutoff determination Bootstrap specificity-targeted threshold for binary classification
Probability calibration Platt scaling (sigmoid) or isotonic regression
Post-hoc analysis Permutation importance, SHAP-ready export, feature stability plots
External validation One-call evaluation on a held-out set with full metric suite
Reproducibility Fully deterministic given a fixed random seed, verified by test suite
Save/load JSON metadata, CSV tables, and pickle of the fitted result

Saving results

# Save at fit time
maker = RobustModelMaker(
    alg="eln", task_type="binary",
    save_results=True,
    output_dir="results/",
    output_prefix="my_model",
    random_state=42,
).fit(X, y)

# Or save afterwards
maker.save_results(output_dir="results/", output_prefix="my_model")

Saves: JSON metadata, full pickle, per-fold score CSVs, stability selection table, and a formatted text summary.


External validation

maker = RobustModelMaker(alg="eln", task_type="binary", random_state=42)
maker.fit(X_train, y_train, X_validation=X_val, y_validation=y_val)

val = maker.result_.validation_result
print(val.metrics)   # auc, accuracy, sensitivity, specificity, ...

Permutation importance

pi = maker.permutation_importance(X_val, y_val, n_repeats=20, random_state=42)
print(pi.summary().head(10))

SHAP integration

shap_data = maker.result_.export_shap_ready(X)

import shap
explainer = shap.LinearExplainer(shap_data["model"], shap_data["X"])
shap_values = explainer.shap_values(shap_data["X"])
shap.summary_plot(shap_values, shap_data["X"])

Grouped cross-validation

maker = RobustModelMaker(alg="eln", task_type="regression", random_state=42)
maker.fit(X, y, groups=sample_ids)   # prevents leakage across experimental units

Benchmark results

Three real scientific datasets evaluated against a full-feature nested-CV baseline using the same algorithm and fold structure. All three benchmarks use Random Forest (rf) for both ROBUST and the baseline, isolating the effect of bootstrap stability selection from any algorithm differences:

Dataset Task n x p ROBUST feats Reduction Metric Outcome
SECOM Manufacturing binary 1254 x 590 ~47 ~92% AUC (higher=better) preserved
Urban Land Cover multiclass 540 x 147 ~31 ~79% AUC-OVR (higher=better) preserved
Graphene Oxide Bulk regression 1294 x 412 ~68 ~83% RMSE in eV (lower=better) preserved

preserved is the primary success criterion: the stability-selected feature subset achieves statistically equivalent performance to the full-feature baseline (paired Wilcoxon, p >= 0.05) while using a fraction of the features. The selected features are robust across bootstrap resamples of the training data, not optimal for any single model fit; a small non-significant performance difference from the baseline is the expected and intended outcome.

Note on split methodology: All benchmarks use BenchMake archetypal splits, which are adversarial by design. BenchMake selects maximally representative train/test partitions that keep the two sets apart in feature space, producing more conservative scores than conventional random splits. This is intentional: the benchmark is a worst-case assessment. Scores on your own data with default random splits will typically be higher. The ROBUST vs. full-feature baseline comparison within each benchmark is internally consistent because both models use the same split.


Citing this work

Barnard, A. S. (2026). RobustModelMaker: A reproducible stability-selection pipeline
for scientific machine learning (v0.3). GitHub: https://github.com/amaxiom/RobustModelMaker

Documentation

Full documentation is available in the GitHub repository:

  • User Guide: parameters, methods, prediction, validation, SHAP, saving
  • Implementation Guide: internal design, algorithm details, tuning for speed and rigor
  • Interpretation Guide: reading results correctly, statistical tests, what to report in a paper

Author

Prof Amanda S Barnard GitHub: amaxiom

RobustModelMaker is developed and maintained as a tool for rigorous, reproducible machine learning in scientific research.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

robustmodelmaker-0.3.1.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

robustmodelmaker-0.3.1-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file robustmodelmaker-0.3.1.tar.gz.

File metadata

  • Download URL: robustmodelmaker-0.3.1.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for robustmodelmaker-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5109806440bacb623908df713eab36b03ba90923ccefbad3fabeb4346ad94308
MD5 c24fd1d4c4f904bccb5a177e81ba4fd2
BLAKE2b-256 a692ae856f26421b848d027d88b1b7986ea6b37a4ee3cae67d7c5fbcd61a4b7a

See more details on using hashes here.

File details

Details for the file robustmodelmaker-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for robustmodelmaker-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5246eccfff5266cedf574de3a19df99918d5cf784652a333f39ba985425066c7
MD5 b4b5ae061ca25aaff69f49a1a28f9196
BLAKE2b-256 1925f70ff7a45b55d69cd27e5e6f6e1dcad351d2974ecdf16faea7d877f1b345

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page