File size: 16,888 Bytes
c742ac4 |
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 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 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 |
import marimo
__generated_with = "0.14.16"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _(mo):
mo.center(mo.md("# Home Credit Default Risk Prediction"))
return
@app.cell
def _():
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import RandomizedSearchCV
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder, OrdinalEncoder
from lightgbm import LGBMClassifier
from src.plots import (
plot_target_distribution,
plot_credit_amounts,
plot_education_levels,
plot_occupation,
plot_family_status,
plot_income_type,
)
from src.utils import get_dataset, get_features_target, get_train_test_sets
from src.preprocessing import preprocess_data_pipeline
return (
get_dataset,
get_features_target,
get_train_test_sets,
pd,
plot_credit_amounts,
plot_education_levels,
plot_family_status,
plot_income_type,
plot_occupation,
plot_target_distribution,
preprocess_data_pipeline,
)
@app.cell
def _(get_dataset, get_features_target):
df = get_dataset()
X, y = get_features_target(df)
return X, df, y
@app.cell
def _(mo):
mo.md("""## 1. Exploratory Data Analysis""")
return
@app.cell
def _(mo):
mo.callout(
kind="info",
value=mo.md(
"""π‘ **Want a step-by-step walkthrough instead?**
Check the Jupyter notebook version here: π [Jupyter notebook](https://huggingface.co/spaces/iBrokeTheCode/Home_Credit_Default_Risk_Prediction/blob/main/tutorial_app.ipynb)""",
),
)
return
@app.cell
def _(mo):
mo.md("""### 1.1 Dataset Information""")
return
@app.cell
def _(mo):
mo.md("""**a. Shape of the train and test datasets**""")
return
@app.cell
def _(X_test, X_train, df):
train_samples = "Train dataset samples: {}".format(X_train.shape[0])
test_samples = "Test dataset samples: {}".format(X_test.shape[0])
columns_number = "Number of columns: {}".format(df.shape[1])
train_samples, test_samples, columns_number
return
@app.cell
def _(mo):
mo.md("""**b. Dataset features**""")
return
@app.cell
def _(X):
X.columns
return
@app.cell
def _(mo):
mo.md("""**c. Sample from dataset**""")
return
@app.cell
def _(X):
sample = X.head(5).T
sample.columns = [
str(col) for col in sample.columns
] # fix integer name warning
sample = sample.astype(str) # avoid numeric conversion issues in viewer
sample
return
@app.cell
def _(mo):
mo.md("""**d. Target variable Distribution**""")
return
@app.cell
def _(df, plot_target_distribution):
target_table, target_plot = plot_target_distribution(df=df)
target_table
return (target_plot,)
@app.cell
def _(target_plot):
target_plot
return
@app.cell
def _(mo):
mo.md("""**e. Number of columns of each data type**""")
return
@app.cell
def _(X):
X.dtypes.value_counts().sort_values(ascending=False)
return
@app.cell
def _(X):
categorical_cols = (
X.select_dtypes(include=["object"]).nunique().sort_values(ascending=False)
)
categorical_cols
return
@app.cell
def _(mo):
mo.md("""**f. Missing data**""")
return
@app.cell
def _(X, pd):
missing_count = X.isna().sum().sort_values(ascending=False)
missing_percentage = (missing_count / X.shape[0] * 100).round(2)
missing_data = pd.DataFrame(
data={"Count": missing_count, "percentage": missing_percentage}
)
missing_data
return
@app.cell
def _(mo):
mo.md("""### 1.2 Distribution of Variables""")
return
@app.cell
def _(mo):
mo.md(
r"""Want to see how these plots were created? You can find the source code for the visualizations in [plots.py](https://huggingface.co/spaces/iBrokeTheCode/Home_Credit_Default_Risk_Prediction/blob/main/src/plots.py)."""
)
return
@app.cell
def _(mo):
mo.md("""**a. Credit Amounts**""")
return
@app.cell
def _(X, plot_credit_amounts):
plot_credit_amounts(df=X)
return
@app.cell
def _(mo):
mo.md("""**b. Education Level of Credit Applicants**""")
return
@app.cell
def _(X, plot_education_levels):
education_table, education_plot = plot_education_levels(df=X)
education_table
return (education_plot,)
@app.cell
def _(education_plot):
education_plot
return
@app.cell
def _(mo):
mo.md("""**c. Ocupation of Credit Applicants**""")
return
@app.cell
def _(X, plot_occupation):
occupation_table, occupation_plot = plot_occupation(df=X)
occupation_table
return (occupation_plot,)
@app.cell
def _(occupation_plot):
occupation_plot
return
@app.cell
def _(mo):
mo.md("""**d. Family Status of Applicants**""")
return
@app.cell
def _(X, plot_family_status):
family_status_table, family_status_plot = plot_family_status(df=X)
family_status_table
return (family_status_plot,)
@app.cell
def _(family_status_plot):
family_status_plot
return
@app.cell
def _(mo):
mo.md("""**e. Income Type of Applicants by Target Variable**""")
return
@app.cell
def _(df, plot_income_type):
plot_income_type(df=df)
return
@app.cell
def _(mo):
mo.md("""## 2. Preprocessing""")
return
@app.cell
def _(mo):
mo.md("""**a. Separate Train and Test Datasets**""")
return
@app.cell
def _(X, get_train_test_sets, y):
X_train, y_train, X_test, y_test = get_train_test_sets(X, y)
X_train.shape, y_train.shape, X_test.shape, y_test.shape
return X_test, X_train
@app.cell
def _(mo):
mo.md("""**b. Preprocess Data**""")
return
@app.cell
def _(mo):
mo.md(
r"""
This preprocessing perform:
- Correct outliers/anomalous values in numerical columns (`DAYS_EMPLOYED` column).
- Encode string categorical features (`dtype object`).
- If the feature has 2 categories, Binary Encoding is applied.
- One Hot Encoding for more than 2 categories.
- Impute values for all columns with missing data (using median as imputing value).
- Feature scaling with Min-Max scaler
Want to see how the dataset was processed? You can find the code for the preprocessing steps in [preprocessing.py](https://huggingface.co/spaces/iBrokeTheCode/Home_Credit_Default_Risk_Prediction/blob/main/src/preprocessing.py).
"""
)
return
@app.cell
def _(X_test, X_train, preprocess_data_pipeline):
train_data, test_data = preprocess_data_pipeline(
train_df=X_train, test_df=X_test
)
train_data.shape, test_data.shape
return
@app.cell
def _(mo):
mo.md("""## 3. Training Models""")
return
@app.cell
def _(mo):
mo.md(
r"""At this points, we will work with `train_data` and `test_data` as features sets; also `y_train` and `y_test` as target sets."""
)
return
@app.cell
def _(mo):
mo.md(r"""### 3.1 Logistic Regression""")
return
@app.cell
def _(mo):
mo.callout(
mo.md("""
In Logistic Regression, C is the inverse of regularization strength:
- **Small C** β Stronger regularization β Simpler model, less overfitting risk, but may underfit.
- **Large C** β Weaker regularization β Model fits training data more closely, but may overfit.
"""),
kind="info",
)
return
@app.cell
def _(mo):
mo.md(
r"""
We trained our Logistic Regression model using the following code:
```py
# π Logistic Regression
log_reg = LogisticRegression(C=0.0001)
log_reg.fit(train_data, y_train)
# Train data predicton (class 1)
lr_train_pred = log_reg.predict_proba(train_data)[:, 1]
# Test data prediction (class 1)
lr_test_pred = log_reg.predict_proba(test_data)[:, 1]
# Get the ROC AUC Score on train and test datasets
log_reg_scores = {
"train_score": roc_auc_score(y_train, lr_train_pred),
"test_score": roc_auc_score(y_test, lr_test_pred),
}
log_reg_scores
```
π The ROC AUC scores obtained:
"""
)
return
@app.cell
def _():
lr_scores = {
"train_score": 0.6868418961663535,
"test_score": 0.6854973003347028,
}
lr_scores
return
@app.cell
def _(mo):
mo.md(r"""### 3.2 Random Forest Classifier""")
return
@app.cell
def _(mo):
mo.md(
r"""
We trained our Random Forest Classifier model using the following code:
```py
# π Random Forest Classifier
rf = RandomForestClassifier(random_state=42, n_jobs=-1)
rf.fit(train_data, y_train)
rf_train_pred = rf.predict_proba(train_data)[:, 1]
rf_test_pred = rf.predict_proba(test_data)[:, 1]
rf_scores = {
"train_score": roc_auc_score(y_train, rf_train_pred),
"test_score": roc_auc_score(y_test, rf_test_pred),
}
rf_scores
```
π The ROC AUC scores obtained:
"""
)
return
@app.cell
def _():
rf_scores = {"train_score": 1.0, "test_score": 0.7066811557903828}
rf_scores
return
@app.cell
def _(mo):
mo.md(r"""### 3.3. Randomized Search with Cross Validations""")
return
@app.cell
def _(mo):
mo.md(
r"""
We trained the Randomized Search CV using the following code:
```py
# π RandomizedSearchCV
param_dist = {"n_estimators": [50, 100, 150], "max_depth": [10, 20, 30]}
rf_optimized = RandomForestClassifier(random_state=42, n_jobs=-1)
rscv = RandomizedSearchCV(
estimator=rf_optimized,
param_distributions=param_dist,
n_iter=5,
scoring="roc_auc",
cv=3,
random_state=42,
n_jobs=-1,
)
rscv.fit(train_data, y_train)
rfo_train_pred = rscv.predict_proba(train_data)[:, 1]
rfo_test_pred = rscv.predict_proba(test_data)[:, 1]
rfo_scores = {
"train_score": roc_auc_score(y_train, rfo_train_pred),
"test_score": roc_auc_score(y_test, rfo_test_pred),
}
rfo_scores
```
π The ROC AUC scores obtained:
"""
)
return
@app.cell
def _():
rfo_scores = {
"train_score": 0.8196620915431655,
"test_score": 0.7308385425476998,
}
rfo_scores
return
@app.cell
def _(mo):
mo.md(r"""π₯The best results:""")
return
@app.cell
def _():
optimized_results = {
"best_params_": {"n_estimators": 100, "max_depth": 10},
"best_score_": 0.7296259755147781,
"best_estimator_": "RandomForestClassifier(max_depth=10, n_jobs=-1, random_state=42)",
}
optimized_results
return
@app.cell
def _(mo):
mo.md(r"""### 3.4 LightGBM""")
return
@app.cell
def _(mo):
mo.md(
r"""
We trained our LightGBM Classifier model using the following code:
```py
# π LightGBM
import warnings
warnings.filterwarnings(
"ignore", message="X does not have valid feature names"
)
# π Get numerical and categorical variables (binary and mutiple)
num_cols = X_train.select_dtypes(include="number").columns.to_list()
cat_cols = X_train.select_dtypes(include="object").columns.to_list()
binary_cols = [col for col in cat_cols if X_train[col].nunique() == 2]
multi_cols = [col for col in cat_cols if X_train[col].nunique() > 2]
# π [1] Create the pipelines for different data types
numerical_pipeline = Pipeline(
steps=[
("imputer", SimpleImputer(strategy="median")),
("scaler", MinMaxScaler()),
]
)
binary_pipeline = Pipeline(
steps=[
("imputer", SimpleImputer(strategy="most_frequent")),
("ordinal", OrdinalEncoder()),
("scaler", MinMaxScaler()),
]
)
multi_pipeline = Pipeline(
steps=[
("imputer", SimpleImputer(strategy="most_frequent")),
(
"onehot",
OneHotEncoder(handle_unknown="ignore", sparse_output=False),
),
("scaler", MinMaxScaler()),
]
)
# π [2] Create the preprocessor using ColumnTransformer
preprocessor = ColumnTransformer(
transformers=[
("binary", binary_pipeline, binary_cols),
("multi", multi_pipeline, multi_cols),
("numerical", numerical_pipeline, num_cols),
],
remainder="passthrough",
)
# π [3] Create the Final Pipeline that combines the preprocessor and the model
lgbm = LGBMClassifier(
n_estimators=500,
learning_rate=0.05,
max_depth=-1,
random_state=42,
class_weight="balanced",
n_jobs=-1,
)
lgbm_pipeline = Pipeline(
steps=[("preprocessor", preprocessor), ("classifier", lgbm)]
)
# π [4] Fit the Final Pipeline on the ORIGINAL, unprocessed data
# The pipeline takes care of all the preprocessing internally.
lgbm_pipeline.fit(X_train, y_train)
lgbm_train_pred = lgbm_pipeline.predict_proba(X_train)[:, 1]
lgbm_test_pred = lgbm_pipeline.predict_proba(X_test)[:, 1]
lgbm_scores = {
"train_score": roc_auc_score(y_train, lgbm_train_pred),
"test_score": roc_auc_score(y_test, lgbm_test_pred),
}
lgbm_scores
```
π The ROC AUC scores obtained:
"""
)
return
@app.cell
def _():
lgbm_scores = {
"train_score": 0.8523466410959462,
"test_score": 0.7514895868142193,
}
lgbm_scores
return
@app.cell
def _(mo):
mo.md(r"""## 4. Model Performance Analysis""")
return
@app.cell
def _(mo):
lg_stat = mo.stat(
label="Logistic Regression",
bordered=True,
value="ποΈ 0.687 π 0.685",
caption="Scores are consistent across train and test, indicating no overfitting. However, the overall AUC is low, suggesting underfitting β the model is too simple to capture complex patterns.",
direction="decrease",
)
rfc_stat = mo.stat(
label="Random Forest Classifier",
bordered=True,
value="ποΈ 1.0 π 0.707",
caption="Perfect training AUC indicates severe overfitting β the model memorized the training set. While the test score is better than Logistic Regression, the gap is too large for good generalization.",
direction="decrease",
)
rfo_stat = mo.stat(
label="Random Forest with Randomized Search",
bordered=True,
value="ποΈ 0.820 π 0.731",
caption="Hyperparameter tuning greatly reduced overfitting. The smaller trainβtest gap and improved test AUC show better generalization and a strong performance.",
direction="increase",
)
lgbm_stat = mo.stat(
label="LightGBM",
bordered=True,
value="ποΈ 0.852 π 0.751",
caption="Best overall performance. Small trainβtest gap and highest test AUC indicate a well-balanced model with strong generalization.",
direction="increase",
)
mo.vstack(
items=[
mo.hstack(items=[lg_stat, rfc_stat], widths="equal", gap=1),
mo.hstack(items=[rfo_stat, lgbm_stat], widths="equal", gap=1),
],
gap=1,
heights="equal",
align="center",
justify="center",
)
return
@app.cell
def _(mo):
mo.md(r"""## 5. Model Selection""")
return
@app.cell
def _(mo):
mo.md(
r"""
Based on a comparison of all the models, the final model selection is clear.
| Model | Train Score (AUC ROC) | Test Score (AUC ROC) |
| :--- | :---: | :---: |
| Logistic Regression | 0.687 | 0.685 |
| Random Forest Classifier | 1.000 | 0.707 |
| Randomized Search (Tuned RF) | 0.820 | 0.731 |
| **LightGBM** | 0.852 | **0.751** |
* The **Logistic Regression** model performed poorly due to underfitting.
* The base **Random Forest** model, while better, suffered from severe overfitting.
* The tuned **Random Forest** model was a significant improvement and a strong contender, achieving a solid `test_score`.
* However, the **LightGBM** model ultimately demonstrated the best performance, achieving the highest **ROC AUC test score of 0.751**. This indicates that it is the most robust and accurate model for predicting loan repayment risk on unseen data.
"""
)
return
@app.cell
def _(mo):
mo.callout(
kind="success",
value="π₯ Therefore, we will select the LightGBM model as our final choice for deployment.",
)
return
if __name__ == "__main__":
app.run()
|