Client¶
churn_prediction.client.api_client
¶
HTTP wrappers around the churn-prediction FastAPI endpoints.
This module provides functions to interact with the churn-prediction
API endpoints for batch prediction, downloading predictions, and
explaining predictions. It uses the requests library to send HTTP
requests and handle responses.
check_health()
¶
Check the availability of the churn prediction API.
Sends a request to the configured health endpoint and returns the service health information.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
JSON health status returned by the API. |
Raises:
| Type | Description |
|---|---|
HTTPError
|
If the API returns an unsuccessful HTTP status. |
RequestException
|
If the health-check request fails. |
Source code in src/churn_prediction/client/api_client.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
download_predictions(batch_id, save_path)
¶
Download prediction results for a completed batch.
Requests the prediction artifact associated with batch_id from the
API and writes the returned file contents to save_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_id
|
str
|
Unique identifier of the prediction batch. |
required |
save_path
|
str
|
Local destination path for the downloaded prediction file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Path to the saved prediction file. |
Raises:
| Type | Description |
|---|---|
HTTPError
|
If the API returns an unsuccessful HTTP status. |
RequestException
|
If the download request fails. |
OSError
|
If the prediction file cannot be written to disk. |
Source code in src/churn_prediction/client/api_client.py
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 | |
explain_batch(batch_id)
¶
Request explainability artifacts for a prediction batch.
Sends an explainability request for the specified batch and returns the API response containing information about the generated SHAP artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_id
|
str
|
Unique identifier of the prediction batch to explain. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
JSON response returned by the explainability endpoint. |
Raises:
| Type | Description |
|---|---|
HTTPError
|
If the API returns an unsuccessful HTTP status. |
RequestException
|
If the HTTP request fails. |
Source code in src/churn_prediction/client/api_client.py
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 | |
predict_batch(csv_path)
¶
Submit a CSV dataset to the batch prediction API.
Opens the supplied CSV file and sends it as a multipart upload to the configured prediction endpoint. The API response is returned after validating the HTTP status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
str or Path
|
Path to the CSV file containing customer records to score. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
JSON response returned by the prediction API. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the CSV file does not exist. |
HTTPError
|
If the API returns an unsuccessful HTTP status. |
RequestException
|
If the HTTP request fails. |
Source code in src/churn_prediction/client/api_client.py
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 | |
churn_prediction.client.dashboard
¶
Churn Prediction Interpretation Module.
This module provides utilities for interpreting machine learning model outputs related to customer churn. It includes functionality to load model artifacts, decode ordinal features back to their original categories, map feature importance to human-readable recommendations, and extract SHAP-based risk breakdowns for individual customers.
customer_shap_breakdown(risk_profiles, customer_id)
¶
Extract the SHAP contribution breakdown for one customer.
Combines the customer's strongest churn drivers and retention factors, decodes categorical feature values, and returns the contributions sorted by SHAP value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
risk_profiles
|
DataFrame
|
Customer risk profile table produced by the explainability pipeline. |
required |
customer_id
|
int
|
Customer identifier for which the SHAP breakdown should be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
SHAP contribution table containing |
Source code in src/churn_prediction/client/dashboard.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
decode_features(feature, value)
¶
Decode an ordinal feature value into its original category.
Uses the fitted training encoder to translate a numeric ordinal value back into its original categorical representation. Features that are not ordinally encoded are returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature
|
str
|
Name of the feature containing the encoded value. |
required |
value
|
float
|
Numeric ordinal value produced by the preprocessing pipeline. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Original categorical value when the feature is ordinal and the value
is valid. Returns the string representation of |
Source code in src/churn_prediction/client/dashboard.py
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 | |
load_artifacts(artifacts_dir)
¶
Load dashboard artifacts generated by the explainability pipeline.
Reads global feature-importance metadata and customer-level SHAP risk profiles from the supplied artifact directory and converts them into dashboard-ready Python and pandas objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artifacts_dir
|
str or Path
|
Directory containing |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing:
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the artifact directory or required artifact files do not exist. |
NotADirectoryError
|
If |
Source code in src/churn_prediction/client/dashboard.py
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
recommend_for_feature(feature_name)
¶
Return a retention recommendation for a churn-risk feature.
Normalizes the supplied feature name and maps it to the corresponding business recommendation. Unknown features receive a generic customer health recommendation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_name
|
str
|
Feature name associated with the identified churn risk factor. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Business recommendation corresponding to the feature, or a generic account-health recommendation when no feature-specific recommendation exists. |
Source code in src/churn_prediction/client/dashboard.py
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 | |