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 predictions.

Why Use factrainer?

Modern ML frameworks have useful cross-validation functions, but they come with notable limitations:

  • scikit-learn:
    • cross_val_score: cannot provide out-of-fold (OOF) predictions for each sample.
    • cross_val_predict: cannot retain the trained model from each fold (only returns predictions).
  • LightGBM:
    • lgb.cv: does not support parallelized training of cv.

These gaps make it cumbersome to get both OOF predictions and reusable trained models in a single workflow.

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 support:

pip install "factrainer[lightgbm]"

At present, LightGBM is the primary supported backend. Support for additional frameworks will be added as the project evolves.

Get started

Code example: California Housing dataset

import lightgbm as lgb
from sklearn.datasets import fetch_california_housing
from factrainer.core import CvModelContainer
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"},
        callbacks=[lgb.early_stopping(100, verbose=False)],
    ),
)
k_fold = KFold(n_splits=4, shuffle=True, random_state=1)
model = CvModelContainer(config, k_fold, n_jobs_train=4)
model.train(dataset)

# trained models
model.raw_model

# OOF prediction
y_pred = model.predict(dataset)
print(r2_score(data.target, y_pred))

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.13.tar.gz (97.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.13-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: factrainer-0.1.13.tar.gz
  • Upload date:
  • Size: 97.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.13.tar.gz
Algorithm Hash digest
SHA256 467a6f8dcc4bc8d8bd34cc3e12f3c1d58c00990818251172c9e0377b43b020c4
MD5 9897d85a3d9e63de962fe3c8255267d7
BLAKE2b-256 7c49f4723826b03318f230a065dbd7dd34e9f8c6997675ea502ebb73c1aa2f88

See more details on using hashes here.

Provenance

The following attestation bundles were made for factrainer-0.1.13.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.13-py3-none-any.whl.

File metadata

  • Download URL: factrainer-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 15.1 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.13-py3-none-any.whl
Algorithm Hash digest
SHA256 d2a2d2995d1746448dbbbbb9dc85a74035a9fc78afe4cb8f4770236d6686aef8
MD5 10933b1403143272ccec961f0f011c5f
BLAKE2b-256 1698532327c28226eb58cbbbeaaa509b4145269a59eaaae3fe9d5cdc44a2a8cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for factrainer-0.1.13-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