How jedizr/credit-risk-and-loan-default-analysis works
Purpose and Entry Points
This is a Python ML system for predicting loan approval from applicant data, with reproducible training, batch scoring, explanations, and report generation. The primary entry point is the credit-risk CLI in src/credit_risk/cli.py; orchestration is shared with notebooks through src/credit_risk/workflow.py. Container execution is defined by Dockerfile.
Data Lifecycle and Provenance
Immutable CSV input is read from data/raw/loan_risk_prediction_dataset.csv. The workflow produces typed, hashed parquet stages:
• repaired/typed data: data/processed/applicants.parquet
• stateless engineered model input: data/preprocessed/applicants.parquet
• stage hashes and provenance: data/registry.json
Data contracts, quality repair, and I/O sit behind src/credit_risk/data/schema.py, src/credit_risk/data/quality.py, and src/credit_risk/data/io.py. Stateful imputation, scaling, and encoding are intentionally deferred to training folds to prevent holdout leakage.
Workflow Orchestration
src/credit_risk/workflow.py coordinates prepare → preprocess → train → evaluate, callable as a full run or an individual stage. src/credit_risk/config.py centralizes paths, seed, domain thresholds, and training options, optionally overridden by root config.toml. CLI commands also expose prediction and explanation paths for new CSV inputs.
Feature and Anomaly Stage
Stateless domain feature creation and optional feature selection are implemented in src/credit_risk/features/engineering.py and src/credit_risk/features/selection.py. Training-row outliers are identified through interchangeable detectors in src/credit_risk/anomaly/detectors.py and removed/retained through src/credit_risk/anomaly/handling.py. The optional autoencoder feature transformer is adapted to sklearn in src/credit_risk/features/autoencoder.py.
Model Pipeline, Training, and Tuning
src/credit_risk/pipeline.py builds the sklearn composite that owns fold-fitted preprocessing plus classifier, ensuring training and inference share transformations. Model strategies and runtime selection are defined in src/credit_risk/models/base.py, src/credit_risk/models/implementations.py, and src/credit_risk/models/registry.py; supported implementations include LightGBM, logistic regression, random forest, and gradient boosting. src/credit_risk/train.py performs split, fit, calibration, scoring, and persistence; src/credit_risk/tuning.py runs Optuna cross-validation searches.
Evaluation, Decisioning, and Diagnostics
The headline quality metric is PR-AUC because approvals are imbalanced. src/credit_risk/evaluation.py performs validation and selects a decision threshold using configured false-approval versus missed-approval cost. Fairness, error slices, and model explanations are separate boundaries in src/credit_risk/fairness.py, src/credit_risk/error_analysis.py, and src/credit_risk/explain.py; SHAP supports global and applicant-level decision reasons.
Artefacts, Runtime, and Delivery
Versioned model artefacts and reproducibility manifests are managed by src/credit_risk/versioning.py; models are persisted as joblib plus content-addressed metadata, model cards, and a current-run registry under models/. Runtime requires Python managed by uv and ML dependencies including scikit-learn, LightGBM, PyArrow, SHAP, and Optuna. Docker mounts raw data read-only and derived data, models, reports, and output read-write; GitHub Actions in .github/workflows/ci.yml and .github/workflows/release.yml validate and publish the image to GHCR.
Main entry points: train.py Training & calibration · workflow.py Workflow stages · schema.py Data contracts & repair · engineering.py Stateless features
Components 18
Entry & orchestration
- credit-risk CLI · Runs workflow, scoring, and explanation commands.
src/credit_risk/cli.py - Workflow stages · Coordinates prepare, preprocess, train, and evaluate.
src/credit_risk/workflow.py - Configuration · Central paths, seed, thresholds, and training options.
src/credit_risk/config.py
Data lifecycle
- Immutable loan CSV
data/raw/loan_risk_prediction_dataset.csv - Data contracts & repair · Validates, repairs, types, and reads data.
src/credit_risk/data/schema.py - Typed applicants
data/processed/applicants.parquet - Model input
data/preprocessed/applicants.parquet - Stage provenance · Stores stage hashes and provenance.
data/registry.json
ML pipeline
- Stateless features · Creates domain features and optionally selects them.
src/credit_risk/features/engineering.py - Training outliers · Interchangeable detectors remove or retain training rows.
src/credit_risk/anomaly/handling.py - Fold-fitted ML pipeline · Owns imputation, scaling, encoding, optional autoencoder, and classifier.
src/credit_risk/pipeline.py - Model strategies · Selects LightGBM, logistic regression, random forest, or gradient boosting.
src/credit_risk/models/registry.py - Optuna tuning
src/credit_risk/tuning.py - Training & calibration · Splits, fits, calibrates, scores, and persists a model.
src/credit_risk/train.py
Assessment & decisioning
- PR-AUC & thresholding · Selects the cost-sensitive approval threshold.
src/credit_risk/evaluation.py - Fairness, slices & SHAP · Produces fairness analysis, error slices, and decision reasons.
src/credit_risk/explain.py
Artefacts & delivery
- Versioned model artefacts · Manages joblib models, manifests, model cards, and current-run metadata.
src/credit_risk/versioning.py - Container CI/CD · Docker runtime with CI validation and GHCR image publishing.
Dockerfile
Connections 23
- credit-risk CLI → Workflow stages: invokes
- Workflow stages → Configuration: loads
- Workflow stages → Data contracts & repair: prepare
- Immutable loan CSV → Data contracts & repair: read
- Data contracts & repair → Typed applicants: typed output
- Data contracts & repair → Stage provenance: records stage hash
- Workflow stages → Stateless features: preprocess
- Typed applicants → Stateless features: input
- Stateless features → Model input: engineered output
- Stateless features → Stage provenance: records stage hash
- Workflow stages → Training & calibration: train
- Model input → Training outliers: training rows
- Training outliers → Training & calibration: filtered rows
- Model strategies → Fold-fitted ML pipeline: selects classifier
- Optuna tuning → Training & calibration: best parameters
- Fold-fitted ML pipeline → Training & calibration: shared fit/inference pipeline
- Training & calibration → PR-AUC & thresholding: validation scores
- PR-AUC & thresholding → Fairness, slices & SHAP: predictions and threshold
- Training & calibration → Versioned model artefacts: persist model
- PR-AUC & thresholding → Versioned model artefacts: metrics and model card
- credit-risk CLI → Versioned model artefacts: batch scoring
- Versioned model artefacts → Fairness, slices & SHAP: applicant explanations
- Container CI/CD → credit-risk CLI: runs