Skip to content

Training

limen_ml.train

Train a hazard model on a matrix: tune, cross-validate, compare, calibrate, fit.

Every step works on spatial folds (:mod:limen_ml.folds), the same folds for all, because comparing two models on two partitions measures the luck of the partition. The pieces, in the order a training run uses them:

  1. :func:tune: Optuna (TPE, seeded) over LightGBM, maximising the AUC-PR averaged across folds;
  2. :func:cross_validate: the tuned model on each fold, its AUC-PR, Brier and the out-of-fold probabilities;
  3. :func:compare_algorithms: a random forest, a standardised logistic regression (the linear reference: if boosting does not beat it, the non-linear structure buys nothing) and LightGBM, on the same folds;
  4. :func:fit_isotonic on the out-of-fold probabilities, and :func:fit_final on all the data;
  5. :func:shap_explainer for the breakdown of a score.

No tracking, no registry, no database: the application logs what it wants where it wants. Needs the train extra (LightGBM, scikit-learn, Optuna, SHAP).

CrossValidation dataclass

The tuned model across the spatial folds.

too_few

too_few(labels: Sequence[int | bool]) -> bool

Whether a training set is too small or has a single class.

lightgbm_params

lightgbm_params(params: dict[str, Any], y: Any, *, seed: int) -> dict[str, Any]

params with the fixed settings: binary, balanced, deterministic, seeded.

objective

objective(matrix: TrainingMatrix, folds: Sequence[Sequence[str]]) -> Any

The Optuna objective: AUC-PR of LightGBM averaged across the spatial folds.

A fold whose validation part is empty or has no event is skipped; with none left the trial scores 0.

tune

tune(matrix: TrainingMatrix, folds: Sequence[Sequence[str]], *, trials: int, timeout_s: float | None = None, seed: int = CV_SEED, study_name: str | None = None) -> dict[str, Any]

The best LightGBM parameters found by a seeded TPE search.

cross_validate

cross_validate(matrix: TrainingMatrix, folds: Sequence[Sequence[str]], params: dict[str, Any], *, seed: int = CV_SEED) -> CrossValidation

Fit on all folds but one, predict that one, for each fold.

A fold with no event gives probabilities but no AUC-PR or Brier.

algorithm_candidates

algorithm_candidates(y: Any, *, seed: int) -> dict[str, Any]

The algorithms compared on the same folds: forest, logistic, LightGBM.

The logistic regression is standardised (indices in [0, 1] and millimetres of rain live together) and imputes a missing value with the median plus a "was missing" column, so that not measured is not read as zero. The forest and LightGBM handle missing values themselves.

compare_algorithms

compare_algorithms(matrix: TrainingMatrix, folds: Sequence[Sequence[str]], *, seed: int) -> dict[str, dict[str, float]]

AUC-PR and Brier of each candidate on the out-of-fold rows it could predict.

fit_final

fit_final(matrix: TrainingMatrix, params: dict[str, Any], *, seed: int) -> Any

LightGBM with the tuned parameters, fitted on every row.

fit_isotonic

fit_isotonic(y_prob: Any, y_true: Any) -> Any

An isotonic calibrator of probabilities, clipped outside the range it saw.

shap_explainer

shap_explainer(model: Any) -> Any | None

A SHAP tree explainer of the model; None without SHAP or if it cannot build one.