Model¶
churn_prediction.model.training
¶
XGBoost Training Module.
This module provides functionality to train an XGBoost model for churn prediction, evaluate its performance, and generate a comprehensive model card in JSON format.
train_xgb_model(train_data_path, val_data_path, model_filepath, model_card_path, report_path)
¶
Train, evaluate, and export the churn prediction XGBoost model.
Orchestrates the complete model training stage by loading processed datasets, constructing XGBoost matrices, calculating class-imbalance weighting, training the booster, evaluating validation performance, and exporting the model, model card, and training report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_data_path
|
str or Path
|
Path to the processed training Parquet dataset. |
required |
val_data_path
|
str or Path
|
Path to the processed validation Parquet dataset. |
required |
model_filepath
|
str or Path
|
Destination path for the trained XGBoost model. |
required |
model_card_path
|
str or Path
|
Destination path for the machine-readable model card. |
required |
report_path
|
str or Path
|
Destination path for the comprehensive training report. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If either processed dataset cannot be found. |
RuntimeError
|
If model training, model serialization, model-card generation, or training-report generation fails. |
ValueError
|
If the processed datasets are incompatible with the configured training schema or model. |
Source code in src/churn_prediction/model/training.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
unscale_probabilities(y_prob_scaled, scale_pos_weight)
¶
Convert class-weighted probabilities to calibrated likelihoods.
Reverses the probability distortion introduced by scale_pos_weight
during model training. When no effective class weighting is present, the
original probabilities are returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_prob_scaled
|
ndarray
|
Probabilities predicted by the class-weighted model. |
required |
scale_pos_weight
|
float
|
Positive-class weighting factor used during model training. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Probabilities adjusted to represent the estimated likelihood under the original class distribution. |
Source code in src/churn_prediction/model/training.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
churn_prediction.model.inference
¶
XGBoost Inference Module.
This module provides the run_inference function, which takes
a preprocessed DataFrame, a trained XGBoost booster model, and
a path to a model card JSON file. It generates churn probabilities,
calibrates them based on class imbalance, and applies a decision
threshold to produce final predictions.
run_inference(booster, raw_filepath, input_filepath, output_filepath, model_card_path)
¶
Generate churn predictions for a processed inference dataset.
Loads model metadata from the model card, generates raw XGBoost probabilities, reverses class-imbalance probability scaling, applies the configured decision threshold, and persists the resulting predictions alongside the original customer records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
booster
|
Booster
|
Trained XGBoost model used to generate predictions. |
required |
raw_filepath
|
str or Path
|
Path to the original raw input dataset. |
required |
input_filepath
|
str or Path
|
Path to the processed inference dataset supplied to the model. |
required |
output_filepath
|
str or Path
|
Destination path for the generated prediction dataset. |
required |
model_card_path
|
str or Path
|
Path to the JSON model card containing the class-imbalance weight and classification threshold. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the model card or required input files do not exist. |
ValueError
|
If required model metadata is missing or the inference data cannot be converted into an XGBoost input matrix. |
RuntimeError
|
If prediction generation or output persistence fails. |
Source code in src/churn_prediction/model/inference.py
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
churn_prediction.model.explainability
¶
Module for exporting SHAP explainability artifacts for dynamic dashboards.
This module computes SHAP values for XGBoost models and exports structured, high-performance data artifacts (Parquet, JSON) designed for direct consumption by interactive frontend dashboards (e.g., Dash, React, FastAPI backends).
SHAPArtifactExporter(batch_id, df, booster, output_dir, *, max_samples=10000, random_state=42)
¶
Compute and export SHAP artifacts for churn model interpretation.
This class transforms model predictions and SHAP explanations into dashboard-ready Parquet and JSON artifacts. It supports deterministic sampling for large datasets and produces both global feature importance metrics and customer-level churn/retention drivers.
Initialize the SHAP artifact exporter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_id
|
str
|
Identifier for the current batch of explanations. |
required |
df
|
DataFrame
|
Feature matrix containing the customer records to explain. The DataFrame index is treated as the customer identifier. |
required |
booster
|
Booster
|
Trained XGBoost booster for which SHAP explanations will be generated. |
required |
output_dir
|
Path
|
Directory in which explainability artifacts will be written. |
required |
max_samples
|
int
|
Maximum number of customer records to evaluate with SHAP. Larger datasets are sampled deterministically. |
10000
|
random_state
|
int
|
Random seed used when sampling the evaluation dataset. |
42
|
Raises:
| Type | Description |
|---|---|
Exception
|
If the SHAP tree explainer cannot be initialized for the supplied XGBoost model. |
Source code in src/churn_prediction/model/explainability.py
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
export_artifacts(top_k_drivers=5)
¶
Generate and persist dashboard-ready SHAP artifacts.
The export contains the raw SHAP matrix, the aligned input feature matrix, customer-level risk profiles, and global feature-importance metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_k_drivers
|
int
|
Maximum number of positive churn drivers and negative retention factors retained for each customer. |
5
|
Returns:
| Type | Description |
|---|---|
Path
|
Directory containing all generated explainability artifacts. |
Raises:
| Type | Description |
|---|---|
OSError
|
If an artifact cannot be written to disk. |
Exception
|
If SHAP computation or artifact generation fails. |
Source code in src/churn_prediction/model/explainability.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
generate_shap_artifacts(batch_id, df, booster, shap_report_dir)
¶
Generate SHAP explainability artifacts for a trained churn model.
This is the public entry point for the explainability workflow. It
initializes :class:SHAPArtifactExporter, computes the requested SHAP
outputs, and persists the resulting artifacts to the configured report
directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_id
|
str
|
Identifier for the current batch of explanations. |
required |
df
|
DataFrame
|
Model feature matrix to explain. |
required |
booster
|
Booster
|
Trained XGBoost model. |
required |
shap_report_dir
|
Path
|
Directory in which SHAP artifacts will be generated. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Directory containing the generated SHAP artifacts. |
Raises:
| Type | Description |
|---|---|
Exception
|
If SHAP initialization, computation, or artifact export fails. |
Source code in src/churn_prediction/model/explainability.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |