File size: 2,160 Bytes
75b9644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Train and persist the credit risk model and evaluation artifacts."""

from __future__ import annotations

import sys
import pickle
from pathlib import Path

import pandas as pd
from sklearn.model_selection import train_test_split

PROJECT_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT_ROOT / "src"))

from credit_risk.config import DATA_PROCESSED_DIR, DATA_RAW_PATH, MODEL_DIR, REPORTS_DIR
from credit_risk.features import build_training_frame
from credit_risk.modeling import evaluate_model, save_metrics, save_model, train_model


def main() -> None:
    """Main training flow used by local runs and CI validations."""
    raw_df = pd.read_csv(DATA_RAW_PATH)
    features, target = build_training_frame(raw_df)

    # Stratification preserves class balance between train and test sets.
    x_train, x_test, y_train, y_test = train_test_split(
        features,
        target,
        test_size=0.3,
        random_state=42,
        stratify=target,
    )

    model = train_model(x_train=x_train, y_train=y_train, random_state=42)
    metrics, y_hat = evaluate_model(model=model, x_test=x_test, y_test=y_test)

    DATA_PROCESSED_DIR.mkdir(parents=True, exist_ok=True)
    x_train.to_parquet(DATA_PROCESSED_DIR / "x_train.parquet", index=False)
    x_test.to_parquet(DATA_PROCESSED_DIR / "x_test.parquet", index=False)
    y_train.to_frame(name="target").to_parquet(DATA_PROCESSED_DIR / "y_train.parquet", index=False)
    y_test.to_frame(name="target").to_parquet(DATA_PROCESSED_DIR / "y_test.parquet", index=False)
    y_hat.to_frame(name="prediction").to_parquet(DATA_PROCESSED_DIR / "yhat.parquet", index=False)

    save_model(model=model, model_path=MODEL_DIR / "model.joblib")
    # Backward-compatible artifact used by the existing Gradio Space app.
    with (MODEL_DIR / "model.pickle").open("wb") as file:
        pickle.dump(model, file)
    save_metrics(metrics=metrics, path=REPORTS_DIR / "metrics.json")

    print("Training finished successfully.")
    print(f"Saved model to: {MODEL_DIR / 'model.joblib'}")
    print(f"Saved metrics to: {REPORTS_DIR / 'metrics.json'}")


if __name__ == "__main__":
    main()