Skip to main content

Bootstrap-based model stability and supervised binning toolkit

Project description

Bootstrap ML diagnostics + statistical inference + Spark

A lightweight toolkit for statistically robust model diagnostics using bootstrap resampling, with both in-memory and distributed (PySpark) support.

The library provides utilities for:

  • supervised tree binning
  • bootstrap-based feature selection
  • model stability analysis
  • hyperparameter sensitivity analysis
  • statistical feature diagnostics (e.g., logistic relevance, missing analysis)
  • scalable data diagnostics with PySpark

The toolkit focuses on reducing overfitting and improving model interpretability by leveraging bootstrap distributions and statistical inference rather than single-point estimates.


Installation

Option Command
Core (minimal dependencies) pip install maxwailab
Survival Module Only pip install maxwailab[survival]
PySpark Module Only pip install maxwailab[pyspark]
Everything (core + all optional) pip install maxwailab[all]
Core from GitHub pip install git+https://github.com/MaxWienandts/maxwailab.git
GitHub with survival extras pip install "git+https://github.com/MaxWienandts/maxwailab.git#egg=maxwailab[survival]"
GitHub with PySpark extras pip install "git+https://github.com/MaxWienandts/maxwailab.git#egg=maxwailab[pyspark]"

Core Philosophy

Most ML workflows rely on single train/validation splits.

This library instead uses bootstrap resampling to estimate:

  • performance distributions
  • feature selection stability
  • hyperparameter robustness

Benefits:

  • reduces variance from a single split
  • identifies unstable variables
  • provides confidence intervals for model performance

Workflow Overview

Typical modeling workflow using this library:

1️⃣ Supervised binning (optional)

tree_supervised_binning
bootstrap_tree_binning_auc_analysis
plot_target_mean_by_binned_variable


2️⃣ Feature selection

bootstrap_lightgbm_forward_selection
bootstrap_model_variable_comparison_paired

3️⃣ Diagnostics

performance_forward_selection_boxplot
variable_frequency_forward_selection


4️⃣ Extract best variables

top_k_forward_selection_variables_by_frequency_usage
top_k_variables_by_forward_selection_boxplot


5️⃣ Hyperparameter analysis

lightgbm_hyperparameter_auc_curve_bootstrap

6️⃣ Survival Analysis (Optional)
bootstrap_survival_forward_selection
bootstrap_model_variable_comparison_paired
survival_bootstrap_model_comparison

7️⃣ PySpark Data Diagnostics
pyspark_missing_values_table
pyspark_minmax_value
pyspark_compare_columns
pyspark_value_counts_spark
pyspark_missing_by_group
pyspark_logistic_feature_significance

LightGBM Classification Example

import maxwailab

# Forward selection with bootstrap
result_bootstrap = maxwailab.bootstrap_lightgbm_forward_selection(
    df=data,
    target="target",
    n_bootstrap=30,
    n_max_variables=15,
    metric_to_optimize="auc_roc",
    hyperparameters=lgb_params
)

# Analyze performance stability
maxwailab.performance_forward_selection_boxplot(result_bootstrap["auc_roc"], "AUC")

# Variable selection stability
maxwailab.variable_frequency_forward_selection(result_bootstrap["variables"], n_bootstraps=30)

# Extract best variables
top_vars = maxwailab.top_k_forward_selection_variables_by_frequency_usage(result_bootstrap["variables"], n_bootstraps=30, k=10)
# Or
top_vars = maxwailab.top_k_variables_by_forward_selection_boxplot(result_bootstrap["variables"], n_bootstraps=30, k=10)

Paired Bootstrap Comparison (LightGBM)

Compare two models: baseline vs modified (adding/removing variables):

comparison = maxwailab.bootstrap_model_variable_comparison_paired_lgbm(
    df_train=df_train,
    base_variables=["var1", "var2"],
    variables_to_add=["var3"],
    variables_to_remove=["var2"],
    target_col="target",
    n_bootstrap=100,
    metric="auc",
    hyperparameters=lgb_params
)

Generates:

  • Validation performance distributions
  • Paired bootstrap difference distribution
  • Statistical summary (mean, 95% CI, probability of improvement)

Tree-based Supervised Binning

from maxwailab import tree_supervised_binning

tree_supervised_binning(df=data, feature="age", target="target", max_leaf_nodes=5)

# Bootstrap binning stability
bootstrap_tree_binning_auc_analysis(df_train, df_val, feature="age", target="target")
Hyperparameter Sensitivity Analysis
lightgbm_hyperparameter_auc_curve_bootstrap(
    X_train, y_train, X_val, y_val,
    hyperparameters=lgb_params,
    hyperparameter_name="num_leaves",
    hyperparameter_values=[5,10,20,40],
    n_bootstrap=50
)

Analyze target behavior across variable ranges

# Define bins (no need for -inf / +inf)
bins = [0, 18, 30, 50, 80]

summary = plot_target_mean_by_binned_variable(
    df=data,
    target="target",
    variable="age",
    bins=bins
)
  • Visualizes target mean per bin
  • Displays observation count and percentage
  • Useful for feature understanding and pre-binning analysis

Survival Analysis Workflows

Bootstrap Forward Selection for Survival Models

result_survival = maxwailab.bootstrap_survival_forward_selection(
    df_train=df_train,
    duration_col="duration",
    event_col="event",
    start_month_col="start_month",
    model_type="cox_breslow",
    n_bootstrap=50,
    n_max_variables=10,
    metric_to_optimize="c_index",
    hyperparameters=cox_params
)

# Analyze performance stability
maxwailab.performance_forward_selection_boxplot(result_survival["auc_roc"], "AUC")

# Variable selection stability
maxwailab.variable_frequency_forward_selection(result_survival["variables"], n_bootstraps=30)

# Extract best variables
top_vars = maxwailab.top_k_forward_selection_variables_by_frequency_usage(result_survival["variables"], n_bootstraps=30, k=10)
# Or
top_vars = maxwailab.top_k_variables_by_forward_selection_boxplot(result_survival["variables"], n_bootstraps=30, k=10)

Paired Bootstrap Comparison for Survival Models

comparison_surv = maxwailab.bootstrap_model_variable_comparison_paired(
    df_train=df_train,
    model_type="cox_breslow",
    base_variables=["var1", "var2"],
    variables_to_add=["var3"],
    variables_to_remove=["var2"],
    n_bootstrap=50,
    metric="c_index"
)

Generates:

  • Baseline vs Modified model performance distribution
  • Paired difference plot
  • Statistical inference summary

Compare Multiple Survival Models

models_dict = {
    "Cox": CoxModel(),
    "AFT": AFTModel()
}

comparison_multi = maxwailab.survival_bootstrap_model_comparison(
    df_train=df_train,
    models_dict=models_dict,
    feature_cols=["var1", "var2", "var3"],
    n_bootstrap=50
)

Outputs:

  • Bootstrap distributions per model
  • Ranking summary

🔍 Additional Utilities

The examples above cover the core functionality of the library.
maxwailab also includes several additional utilities for:

  • PySpark-based data diagnostics
  • statistical feature analysis
  • extended bootstrap evaluations
  • survival modeling workflows

For a complete list of available functions and usage examples, refer to the notebooks/ directory in the repository, which contains practical, end-to-end implementations.


Module Structure

maxwailab │ ├── binning │ ├── tree_supervised_binning │ └── bootstrap_tree_binning_auc_analysis │ └── plot_target_mean_by_binned_variable │ ├── feature_selection │ ├── bootstrap_lightgbm_forward_selection │ ├── performance_forward_selection_boxplot │ ├── variable_frequency_forward_selection │ ├── top_k_forward_selection_variables_by_frequency_usage │ └── top_k_variables_by_forward_selection_boxplot │ └── bootstrap_model_variable_comparison_paired_lgbm │ └── hyperparameter_analysis | └── lightgbm_hyperparameter_auc_curve_bootstrap │ └── survival_feature_selection | └── bootstrap_survival_forward_selection | └── bootstrap_model_variable_comparison_paired | └── survival_bootstrap_model_comparison │ └── pyspark_basic_functions | └── pyspark_missing_values_table | └── pyspark_minmax_value | └── pyspark_compare_columns | └── pyspark_value_counts_spark | └── pyspark_missing_by_group | └── pyspark_logistic_feature_significance

        
---

# When to Use This Library

This library is particularly useful for:

* **credit risk models**
* **tabular ML problems**
* **high-stakes predictive modeling**
* **interpretable ML workflows**
* **Using large-scale datasets with PySpark
---

# License

MIT License

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

maxwailab-1.2.1.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

maxwailab-1.2.1-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file maxwailab-1.2.1.tar.gz.

File metadata

  • Download URL: maxwailab-1.2.1.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for maxwailab-1.2.1.tar.gz
Algorithm Hash digest
SHA256 accc280fa511acd75385695ac5e2dfcd2bb9354ab8e4405f217ecbcd02a20016
MD5 e01b1dcfc8589fcf65c5e23c6a52b0eb
BLAKE2b-256 38f7f078379c7d6b663e381fe2132666eba77fc07d9439a44e1af47215e62d47

See more details on using hashes here.

File details

Details for the file maxwailab-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: maxwailab-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for maxwailab-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 033717215fa471ddca6c45156e6c4014c1e6db6df318bb57b7fd40d3664d386c
MD5 926eb4a89414f849994168ba40b62d89
BLAKE2b-256 34c991aa5cbccb9fda2d2239a4d301d037249817d069c2f437730cea1355e369

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