Skip to main content

Framework Agnostic Cross-validation Trainer

Project description

Factrainer

CI codecov PyPI image License Stars

Factrainer (Framework Agnostic Cross-validation Trainer) is a machine learning tool that provides a flexible cross-validation training framework. It addresses the limitations of existing cross-validation utilities in popular ML libraries by offering a unified, parallelized approach that retains models and yields out-of-fold (OOF) predictions.

Documentation: For detailed documentation, please visit https://ritsuki1227.github.io/factrainer/

Why Use Factrainer?

Various ML frameworks (e.g., Scikit-learn, LightGBM) offer cross-validation functions. However, each has different features and interfaces. The table below highlights some widely used cross-validation APIs and which capabilities they support:

Framework API OOF prediction return trained models parallel training
LightGBM lgb.cv 🚫 ✅️ 🚫
Scikit-learn GridSearchCV 🚫 🚫 ✅️
Scikit-learn cross_val_score 🚫 🚫 ✅️
Scikit-learn cross_val_predict ✅️ 🚫 ✅️
Scikit-learn cross_validate 🚫 ✅️ ✅️

No built-in API combines OOF predictions, trained-model access, and parallelized training—Factrainer does.

Key Features

  • Unified Cross-Validation API – Provides a single, consistent interface to perform K-fold (or any CV) training, acting as a meta-framework that wraps around multiple ML libraries.
  • Parallelized Training – Run cross-validation folds in parallel to fully utilize multi-core CPUs and speed up model training.
  • Mutable Model Container – Access each fold’s trained model as a mutable object. This makes it easy to analyze models or create ensembles from the fold models.
  • Out-of-Fold Predictions – Retrieve out-of-fold predictions for every training instance through a simple API.

Installation

To install with LightGBM and Scikit-learn support:

pip install "factrainer[lightgbm,sklearn]"

To install with all supported backends (LightGBM, Scikit-learn, XGBoost, and CatBoost):

pip install "factrainer[all]"

At present, LightGBM and Scikit-learn are the primary supported backends. Support for additional frameworks will be implemented as the project evolves.

Get started

Code example: California Housing dataset

import lightgbm as lgb
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import r2_score
from sklearn.model_selection import KFold
from factrainer.core import CvModelContainer, EvalMode
from factrainer.lightgbm import LgbDataset, LgbModelConfig, LgbTrainConfig

data = fetch_california_housing()
dataset = LgbDataset(
    dataset=lgb.Dataset(
        data.data, label=data.target
    )
)
config = LgbModelConfig.create(
    train_config=LgbTrainConfig(
        params={"objective": "regression", "verbose": -1},
        callbacks=[lgb.early_stopping(100, verbose=False)],
    ),
)
k_fold = KFold(n_splits=4, shuffle=True, random_state=1)
model = CvModelContainer(config, k_fold)
model.train(dataset, n_jobs=4)

# Get OOF predictions
y_pred = model.predict(dataset, n_jobs=4)

# Evaluate predictions
metric = model.evaluate(data.target, y_pred, r2_score)
print(f"R2 Score: {metric:.4f}")

# Or get per-fold metrics
metrics = model.evaluate(
    data.target, y_pred, r2_score, eval_mode=EvalMode.FOLD_WISE
)
print(f"R2 Scores by fold: {[f'{m:.4f}' for m in metrics]}")

# Access trained models
model.raw_model

Project Status

Factrainer is in active development. The goal is to expand support to more frameworks and make the tool even more robust. Contributions, issues, and feedback are welcome to help shape the future of Factrainer.

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

factrainer-0.1.25.tar.gz (152.7 kB view details)

Uploaded Source

Built Distribution

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

factrainer-0.1.25-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file factrainer-0.1.25.tar.gz.

File metadata

  • Download URL: factrainer-0.1.25.tar.gz
  • Upload date:
  • Size: 152.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for factrainer-0.1.25.tar.gz
Algorithm Hash digest
SHA256 be1a760b84758a33933b7ed6e778becc1efa09a4067f8a6fce9f8243bd87cde8
MD5 4c4b7352db94a0a33dfdf755566b75ce
BLAKE2b-256 f4be0a1ee720940c220b4af3762c37966affdaf88f0c0353dbdffaa9e2f9c8b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for factrainer-0.1.25.tar.gz:

Publisher: cd-release.yaml on ritsuki1227/factrainer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file factrainer-0.1.25-py3-none-any.whl.

File metadata

  • Download URL: factrainer-0.1.25-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for factrainer-0.1.25-py3-none-any.whl
Algorithm Hash digest
SHA256 575ae70486bd026067da3c4b2051c5fdb4617f84e006831945d97b74cdc38927
MD5 7b6612a1f1aecd4cf6f214955b4f622d
BLAKE2b-256 d8e5f0e08bd006c1777984a8ffeebbb492c3f50ad1f86a1e4758275825d65433

See more details on using hashes here.

Provenance

The following attestation bundles were made for factrainer-0.1.25-py3-none-any.whl:

Publisher: cd-release.yaml on ritsuki1227/factrainer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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