Skip to main content

BestatML

Automated, leakage-safe ensemble machine learning for Python.

BestatML provides sklearn-compatible BestatClassifier and BestatRegressor estimators that combine automatic preprocessing, heterogeneous model selection, cross-validation, out-of-fold prediction generation, tuning, stacking, and blending behind a simple API.

BestatML is designed to provide strong automated baselines and competitive ensembles. It does not claim that an ensemble will outperform every individual model on every dataset.

Contents

Features

  • Simple sklearn-style fit, predict, score, and predict_proba APIs
  • Leakage-safe preprocessing inside CV pipelines
  • Automatic numeric/categorical pandas handling
  • Missing-value imputation and unknown-safe one-hot encoding
  • Selective scaling for linear models
  • Optional low-variance filtering
  • Diverse linear, tree, forest, and gradient-boosting candidates
  • Genuine OOF predictions for comparison and meta-learning
  • Weighted blending, voting, and stacking with safe fallbacks
  • Optional CatBoost, XGBoost, and LightGBM integrations
  • Permutation-based feature importance
  • Structured training summaries and learned-model inspection
  • Reproducible random-state handling
  • Versioned persistence with artifact trust warnings
  • Extensible model registry

Installation

Once published to PyPI:

python -m pip install bestatml

Optional CatBoost/XGBoost/LightGBM integrations:

python -m pip install "bestatml[boosting]"

Development install from GitHub/source:

git clone <your-repository-url>
cd BestatML
python -m pip install -e ".[dev]"

Replace <your-repository-url> with the actual repository URL when the project is hosted publicly.

Quick Start

Classification

from bestatml import BestatClassifier

model = BestatClassifier(random_state=42)
model.fit(X_train, y_train)

predictions = model.predict(X_test)
probabilities = model.predict_proba(X_test)

print(model)

Regression

from bestatml import BestatRegressor

model = BestatRegressor(random_state=42)
model.fit(X_train, y_train)

predictions = model.predict(X_test)

The default workflow evaluates multiple model families with CV, generates OOF predictions, selects a compact model set, constructs an ensemble, and refits the selected pipelines on the complete training data.

Automatic Preprocessing

With a pandas DataFrame, BestatML detects numeric and categorical features and learns transformations inside each CV training fold. Numeric data uses robust median imputation by default; categorical/object/bool data can use most-frequent imputation and one-hot encoding with handle_unknown="ignore".

For linear candidates, scaling can be enabled selectively. Tree-based candidates do not need feature scaling. Optional variance filtering is also fitted inside the pipeline, so it does not inspect validation folds early.

NumPy arrays and supported scipy sparse matrices are also accepted.

Model Inspection

After fitting:

print(model)
print(model.summary())
print(model.model_scores_)
print(model.selected_models_)
print(model.ensemble_method_)

Useful learned attributes include classes_ for classification, n_features_in_, feature_names_in_, best_model_, fitted_models_, oof_predictions_, ensemble_weights_, meta_learner_, and training_summary_.

Feature importance

importance = model.get_feature_importance(
    X_test,
    y_test,
    n_repeats=5,
)
print(importance.head(10))

The result is a pandas table with feature, importance, and std columns. This uses permutation importance because the final ensemble may combine different model families.

Configuration

Important decisions remain configurable while sensible defaults keep the common path simple:

model = BestatClassifier(
    cv=5,
    scoring="roc_auc",
    ensemble_method="stacking",
    tune_hyperparameters=True,
    max_models=6,
    random_state=42,
    search_budget="balanced",
)

Search budgets provide a simple resource trade-off:

  • lightweight — smallest tuning budget
  • balanced — practical default
  • aggressive — broader tuning

Advanced users can also control model_families, disable tuning, choose an explicit ensemble method, supply a custom sklearn preprocessing transformer, and supply a custom sklearn-compatible meta learner.

Optional Boosting Libraries

CatBoost, XGBoost, and LightGBM are optional. The core package does not require them for import or baseline operation.

Install the optional group with:

python -m pip install "bestatml[boosting]"

Automatically discovered optional candidates are skipped when their packages are unavailable. When an optional family is explicitly requested and unavailable, BestatML raises a dedicated dependency error with an installation hint.

Saving and Loading

model.save("model.bestat")
loaded = BestatClassifier.load("model.bestat")

predictions = loaded.predict(X_test)

Artifacts contain fitted model state, ensemble state, learned metadata, configuration, and artifact-format metadata.

Security: persistence uses joblib. Only load artifacts you trust because Python object deserialization can execute code.

Extending the Model Registry

Advanced users can register sklearn-compatible candidates without modifying the trainer:

from bestatml.models import ModelSpec, register_model
from sklearn.linear_model import LogisticRegression

register_model(ModelSpec(
    name="my_classifier",
    family="linear",
    tasks=("classification",),
    probability=True,
    factory=lambda random_state, n_jobs: LogisticRegression(
        max_iter=2000,
        random_state=random_state,
    ),
))

sklearn Compatibility

Both estimators expose explicit constructor parameters and follow sklearn estimator conventions. They can be cloned and inspected through get_params()/set_params(), used in sklearn pipelines, and placed inside outer model-selection workflows.

BestatML's own CV remains part of the estimator training lifecycle so learned preprocessing is fitted only where allowed by the relevant fold.

Error Handling

BestatML validates configuration, training data, target data, and inference schemas before deeper ML operations whenever practical. Important exception types are available from the package root:

from bestatml import BestatMLDataError, BestatMLConfigurationError

Messages identify the problem and provide a concrete correction when possible. Wrapped lower-level failures retain their original cause for debugging.

Development

Create a virtual environment and install the development dependencies:

python -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[dev]"

Run the main checks:

pytest
ruff check .
python -m build
twine check dist/*

The tests cover estimator compliance, preprocessing, OOF behavior, ensemble execution, persistence, schema validation, and adversarial leakage behavior.

Documentation

The README is intentionally a practical introduction rather than the full reference manual. A separate documentation site can provide the exhaustive API reference, configuration reference, architecture notes, and tutorials in the future.

License

BestatML is released under the MIT License. See LICENSE.

Download files

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

Source Distribution

bestatml-0.1.0.tar.gz (34.2 kB view details)

Uploaded Source

Built Distribution

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

bestatml-0.1.0-py3-none-any.whl (37.7 kB view details)

Uploaded Python 3

File details

Details for the file bestatml-0.1.0.tar.gz.

File metadata

  • Download URL: bestatml-0.1.0.tar.gz
  • Upload date:
  • Size: 34.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.0rc1

File hashes

Hashes for bestatml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 358a946b9b5880862005e67d1efbe9cbe50b367a9d7f274e8083c5065689e10f
MD5 ad124fb14ca0d196315cc8cb4c94c92b
BLAKE2b-256 aa0c98886b022bdb13310aca519b99ad7b0225d1fa97b3abf2746421f5b5157b

See more details on using hashes here.

File details

Details for the file bestatml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bestatml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 37.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.0rc1

File hashes

Hashes for bestatml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5af23ff0569d03c95ffba6d440b36321e67f319a675f2d9102055e4ede7c30a
MD5 702b70a88aec2fb00291c0bdb9d62ab8
BLAKE2b-256 9aaa6e006a9a5e5dc9839c57c0cfc9d65c3b680f2754ddad0b3da12690ff92e4

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.1

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page