-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1280 lines (1103 loc) · 50 KB
/
app.py
File metadata and controls
1280 lines (1103 loc) · 50 KB
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
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import joblib
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from sklearn.metrics import roc_curve, auc
import time
import os
from time import strftime
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import numpy as np
from scipy.stats import gaussian_kde
from model_metrics import (
engineer_features,
align_features,
plot_roc,
plot_precision_recall,
plot_confusion_matrix,
generate_classification_report
)
# Configuration
st.set_page_config(
page_title="LoanRisk AI | Smart Credit Analytics",
page_icon="📊",
layout="wide",
initial_sidebar_state="expanded"
)
# Premium Dark Color Scheme
COLOR_SCHEME = {
'primary': '#636EFA', # Vibrant blue
'secondary': '#EF553B', # Vibrant red
'background': '#121721', # Dark blue-gray
'success': '#00CC96', # Teal
'danger': '#FF6692', # Pink
'warning': '#FECB52', # Yellow
'info': '#AB63FA', # Purple
'text': '#E2E2E2', # Light gray
'light': '#2A3A4E', # Lighter dark
'white': '#FFFFFF',
'header': '#A6B7D4', # Light blue-gray
'dark': '#0D1117', # Dark background
'card': '#1E293B', # Card background
'highlight': '#FFA500' # Orange for highlighting
}
import streamlit as st
from streamlit_lottie import st_lottie
import requests
# Load JSON animation from Lottie URL
def load_lottieurl(url: str):
try:
r = requests.get(url)
if r.status_code == 200:
return r.json()
except Exception as e:
print(f"Error loading Lottie: {e}")
return None
# 🎨 Lottie animation URLs (verified & working)
sidebar_anim = load_lottieurl("https://assets2.lottiefiles.com/packages/lf20_t9gkkhz4.json")
# main_anim = load_lottieurl("https://assets9.lottiefiles.com/packages/lf20_cyuxhbnc.json")
main_anim = load_lottieurl("https://assets4.lottiefiles.com/packages/lf20_mjlh3hcy.json")
chart_anim = load_lottieurl("https://assets10.lottiefiles.com/packages/lf20_UJNc2t.json")
footer_anim = load_lottieurl("https://assets1.lottiefiles.com/packages/lf20_jtbfg2nb.json")
skills_anim = load_lottieurl("https://assets3.lottiefiles.com/private_files/lf30_5ttqpi.json")
# 🎯 Sidebar with animation
with st.sidebar:
st.markdown("### 🤖 Smart AI Dashboard")
if sidebar_anim:
st_lottie(sidebar_anim, height=180)
st.markdown("Welcome! Use the menu to explore predictions and risk factors.")
# 🎯 Header
st.markdown("## 📊 Loan Risk Prediction AI Dashboard")
st.markdown("""
### 📌 Overview
This tool helps banks evaluate loan applications using AI-driven analytics.
""")
# 🎯 Main AI animation
if main_anim:
st_lottie(main_anim, height=260)
# Analytics mascot
# Data analysis
chart_anim = load_lottieurl("https://assets2.lottiefiles.com/packages/lf20_49rdyysj.json") # Graphs interaction # Thank you # Project showreel
loan_risk_anim = load_lottieurl("https://assets4.lottiefiles.com/packages/lf20_kyu7xb1v.json")
# Premium Custom CSS with Dark Theme
st.markdown(f"""
<style>
/* Base Styles */
.main {{
background: {COLOR_SCHEME['background']};
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: {COLOR_SCHEME['text']};
}}
/* Text and Headings */
h1 {{
color: {COLOR_SCHEME['primary']} !important;
font-weight: 700;
margin-bottom: 0.5rem;
border-bottom: 2px solid {COLOR_SCHEME['primary']};
padding-bottom: 10px;
}}
h2 {{
color: {COLOR_SCHEME['secondary']} !important;
font-weight: 600;
margin-bottom: 0.5rem;
}}
h3 {{
color: {COLOR_SCHEME['header']} !important;
font-weight: 600;
}}
.stMarkdown p {{
color: {COLOR_SCHEME['text']} !important;
line-height: 1.6;
}}
/* Form Elements */
.stNumberInput input, .stSlider input {{
border: 1px solid {COLOR_SCHEME['primary']} !important;
}}
.stSlider .st-ae {{
background: {COLOR_SCHEME['primary']} !important;
}}
/* Cards and Containers */
.metric-card {{
background: {COLOR_SCHEME['card']};
border-radius: 12px;
padding: 20px;
margin: 10px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
border-left: 4px solid {COLOR_SCHEME['primary']};
transition: all 0.3s ease;
}}
.metric-card:hover {{
transform: translateY(-3px);
box-shadow: 0 8px 16px rgba(0,0,0,0.4);
}}
.graph-container {{
background: {COLOR_SCHEME['card']};
border-radius: 12px;
padding: 20px;
margin-bottom: 25px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}}
/* Tabs */
.stTabs [data-baseweb="tab-list"] {{
gap: 10px;
}}
.stTabs [data-baseweb="tab"] {{
background: {COLOR_SCHEME['light']};
border-radius: 8px 8px 0 0 !important;
padding: 10px 20px;
transition: all 0.3s ease;
color: {COLOR_SCHEME['text']};
}}
.stTabs [aria-selected="true"] {{
background: {COLOR_SCHEME['primary']} !important;
color: {COLOR_SCHEME['white']} !important;
}}
/* Buttons */
.stButton>button {{
background-color: {COLOR_SCHEME['primary']};
color: white;
border-radius: 8px;
padding: 10px 24px;
border: none;
font-weight: 500;
transition: all 0.3s ease;
}}
.stButton>button:hover {{
background-color: {COLOR_SCHEME['secondary']};
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}}
/* Form Elements */
.stNumberInput, .stSlider, .stSelectbox {{
margin-bottom: 20px;
}}
.stTextInput>div>div>input {{
color: {COLOR_SCHEME['text']};
background-color: {COLOR_SCHEME['card']};
}}
/* Footer */
.footer {{
background: linear-gradient(135deg, {COLOR_SCHEME['primary']}, {COLOR_SCHEME['secondary']});
color: white;
padding: 20px;
text-align: center;
margin-top: 40px;
border-radius: 12px;
}}
.footer p {{
color: white !important;
margin: 5px 0;
}}
/* Animations */
@keyframes fadeIn {{
from {{ opacity: 0; transform: translateY(20px); }}
to {{ opacity: 1; transform: translateY(0); }}
}}
.animated-card {{
animation: fadeIn 0.6s ease-out;
}}
/* Highlight Box */
.highlight-box {{
padding: 20px;
border-radius: 10px;
color:black !important;
margin: 20px 0;
border-left: 5px solid {COLOR_SCHEME['highlight']};
}}
/* Plotly chart background */
.js-plotly-plot .plotly, .js-plotly-plot .plotly div {{
background: {COLOR_SCHEME['card']} !important;
}}
</style>
""", unsafe_allow_html=True)
# Load resources with error handling and caching optimizations
@st.cache_resource(show_spinner=False)
def load_model():
try:
model_path = os.path.join('best_model', 'best_model_Random_Forest.pkl')
model = joblib.load(model_path)
return model
except Exception as e:
st.error(f"Error loading model: {str(e)}")
return None
@st.cache_data(show_spinner=False)
def load_data():
try:
data_path = os.path.join('notebooks', 'cleaned_loan_predictions.csv')
df = pd.read_csv(data_path)
# Convert categorical columns to numeric for ROC curve
if 'REASON' in df.columns:
df['REASON'] = df['REASON'].map({'HomeImp': 0, 'DebtCon': 1})
if 'JOB' in df.columns:
df['JOB'] = pd.factorize(df['JOB'])[0]
return df
except Exception as e:
st.error(f"Error loading data: {str(e)}")
return pd.DataFrame()
# Load resources with progress indicators
with st.spinner('Loading model and data...'):
model = load_model()
df = load_data()
# import os
# import json
# import joblib
# import pandas as pd
# import streamlit as st
# from datetime import datetime
# # Configuration
# MODEL_DIR = "best_model"
# DATA_PATH = os.path.join('notebooks', 'cleaned_loan_predictions.csv')
# def get_latest_model():
# """Find the latest model and its metadata"""
# try:
# # Get all metadata files
# meta_files = [f for f in os.listdir(MODEL_DIR)
# if f.endswith('_metadata.json')]
# if not meta_files:
# st.error("No model metadata files found")
# return None, None, None
# # Sort by creation date (newest first)
# meta_files.sort(reverse=True)
# latest_meta = meta_files[0]
# # Load metadata
# meta_path = os.path.join(MODEL_DIR, latest_meta)
# with open(meta_path) as f:
# metadata = json.load(f)
# # Verify model file exists - fix path issue
# model_path = metadata.get('model_path')
# if model_path and not os.path.isabs(model_path):
# model_path = os.path.join(MODEL_DIR, os.path.basename(model_path))
# if not model_path or not os.path.exists(model_path):
# st.error(f"Model file not found: {model_path}")
# return None, None, None
# return model_path, meta_path, metadata
# except Exception as e:
# st.error(f"Model discovery error: {str(e)}")
# return None, None, None
# def get_data_status():
# """Check data file status for freshness"""
# try:
# return os.path.getmtime(DATA_PATH) if os.path.exists(DATA_PATH) else None
# except Exception as e:
# st.error(f"Data status check failed: {str(e)}")
# return None
# # Resource loading with smart caching
# @st.cache_resource(show_spinner=False, ttl=3600)
# def load_model(model_path):
# """Load model with validation"""
# try:
# model = joblib.load(model_path)
# # Moved toast notification outside cached function
# return model
# except Exception as e:
# st.error(f"Model load failed: {str(e)}")
# return None
# # Modified load_data to remove Streamlit elements from cached function
# @st.cache_data(show_spinner=False, ttl=300)
# def load_data(_data_status):
# """Load and preprocess data without Streamlit elements"""
# try:
# df = pd.read_csv(DATA_PATH)
# # Dynamic preprocessing
# category_mappings = {
# 'REASON': {'HomeImp': 0, 'DebtCon': 1},
# 'JOB': lambda x: pd.factorize(x)[0]
# }
# for col, mapping in category_mappings.items():
# if col in df.columns:
# df[col] = df[col].map(mapping) if isinstance(mapping, dict) else mapping(df[col])
# return df
# except Exception as e:
# return pd.DataFrame()
# # Main loading process
# with st.spinner('Initializing application...'):
# # Dynamic model loading
# model_path, meta_path, metadata = get_latest_model()
# model = load_model(model_path) if model_path else None
# # Real-time data loading
# data_status = get_data_status()
# df = load_data(data_status) if data_status else pd.DataFrame()
# # Show notifications after loading
# if model_path:
# st.toast(f"Model loaded: {os.path.basename(model_path)}", icon="🤖")
# if not df.empty:
# st.toast("Data successfully refreshed", icon="🔄")
# Custom CSS for cohesive visual design
st.markdown("""
<style>
:root {
--primary-dark: #0A192F; /* Deep Navy */
--secondary-dark: #172A45; /* Medium Navy */
--accent-teal: #64FFDA; /* Bright Teal */
--text-light: #CCD6F6; /* Soft White */
}
.dashboard-header {
background: var(--primary-dark);
border-left: 5px solid var(--accent-teal);
padding: 2rem;
border-radius: 8px;
margin-bottom: 2rem;
}
.metric-card {
background: var(--secondary-dark);
border-radius: 10px;
padding: 1.5rem;
border: 1px solid rgba(100, 255, 218, 0.15);
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.metric-card:hover {
transform: translateY(-3px);
box-shadow: 0 4px 15px rgba(100, 255, 218, 0.2);
}
.metric-value {
font-size: 2.2rem;
font-weight: 600;
color: var(--accent-teal);
margin: 0.5rem 0;
}
.info-badge {
background: rgba(100, 255, 218, 0.1);
color: var(--accent-teal);
padding: 6px 12px;
border-radius: 20px;
font-size: 0.9rem;
}
</style>
""", unsafe_allow_html=True)
# # Dashboard Implementation
# if model and metadata:
# st.markdown(f"""
# <div class="dashboard-header">
# <h1 style="color: var(--text-light); margin-bottom: 1rem;">🌌 Model Intelligence Dashboard</h1>
# <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 2rem; color: var(--text-light);">
# <div>
# <div class="info-badge">Model Name</div>
# <p style="font-size: 1.2rem; margin: 0.5rem 0;">{metadata['model_name']}</p>
# </div>
# <div>
# <div class="info-badge">Version</div>
# <p style="font-size: 1.2rem; margin: 0.5rem 0;">{metadata['version'].split('_')[0]}</p>
# </div>
# <div>
# <div class="info-badge">Created</div>
# <p style="font-size: 1.2rem; margin: 0.5rem 0;">
# {datetime.strptime(metadata['version'], "%Y%m%d_%H%M%S").strftime("%Y-%m-%d %H:%M")}
# </p>
# </div>
# </div>
# </div>
# """, unsafe_allow_html=True)
# # Performance Metrics Grid
# metrics = metadata.get('performance_metrics', {})
# st.markdown("""
# <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.5rem; margin-bottom: 2rem;">
# """, unsafe_allow_html=True)
# metric_config = [
# ("🎯 Accuracy", metrics.get('Accuracy', 0), "Percentage of correct predictions"),
# ("🎯 Precision", metrics.get('Precision', 0), "True positive rate"),
# ("🎯 Recall", metrics.get('Recall', 0), "Positive class coverage"),
# ("🎯 F1 Score", metrics.get('F1 Score', 0), "Harmonic mean balance"),
# ("📈 AUC-ROC", metrics.get('AUC', 0), "Classification strength score")
# ]
# for title, value, desc in metric_config:
# # Format value based on metric type first
# if title == "📈 AUC-ROC":
# formatted_value = f"{float(value):.2f}" # Ensure float conversion
# else:
# formatted_value = f"{float(value):.2%}" # Ensure float conversion
# st.markdown(f"""
# <div class="metric-card">
# <h4 style="color: var(--accent); margin: 0 0 1rem 0;">{title}</h4>
# <div class="metric-value">{formatted_value}</div>
# <p style="color: var(--text); opacity: 0.8; font-size: 0.9rem; margin: 0;">{desc}</p>
# </div>
# """, unsafe_allow_html=True)
# st.markdown("</div>", unsafe_allow_html=True)
# Sidebar Configuration
with st.sidebar:
st.markdown(f"""
<div style="text-align:center; margin-bottom:30px;">
<h3 style="color:{COLOR_SCHEME['primary']};">Dashboard Filters</h3>
</div>
""", unsafe_allow_html=True)
# Initialize filters with safe defaults
job_options = df['JOB'].unique() if 'JOB' in df.columns else []
reason_options = ['HomeImp', 'DebtCon'] if 'REASON' in df.columns else []
job_filter = st.multiselect("Select Occupation", options=job_options)
reason_filter = st.multiselect("Select Loan Purpose", options=reason_options)
# Apply filters to data safely
filtered_df = df.copy()
if job_filter and 'JOB' in df.columns:
filtered_df = filtered_df[filtered_df['JOB'].isin(job_filter)]
if reason_filter and 'REASON' in df.columns:
filtered_df = filtered_df[filtered_df['REASON'].isin(reason_filter)]
# Create tabs with custom styling
tab1, tab2 = st.tabs(["🔍 Risk Assessment", "📊 Data Insights"])
# Risk Assessment Tab
with tab1:
st.markdown(f"""
<div style="margin-bottom:30px;">
<h1 style="color:{COLOR_SCHEME['header']};">Loan Risk Assessment</h1>
<p style="font-size:1.1rem; color:{COLOR_SCHEME['text']};">Evaluate borrower risk using our AI-powered scoring model</p>
</div>
""", unsafe_allow_html=True)
# Prediction Form
# Prediction Form
with st.form("loan_appraisal"):
col1, col2 = st.columns(2)
with col1:
st.markdown(f"#### <span style='color:{COLOR_SCHEME['primary']}'>🏦 Borrower Information</span>", unsafe_allow_html=True)
loan = st.number_input("💰 Loan Amount (USD) *", 1000, 1000000, 5000,
help="Total loan amount requested by the borrower.")
value = st.number_input("🏠 Property Value (USD) *", 10000, 2000000, 250000,
help="Market value of the property used as collateral.")
mortdue = st.number_input("📉 Existing Mortgage (USD)", 0, 1000000, 100000,
help="Outstanding balance on any existing mortgages.")
debtinc = st.slider("⚖️ Debt-to-Income Ratio (%) *", 0.0, 100.0, 35.0, 0.5,
help="A high ratio indicates higher financial risk.")
with col2:
st.markdown(f"#### <span style='color:{COLOR_SCHEME['secondary']}'>📋 Credit History</span>", unsafe_allow_html=True)
yoj = st.slider("🧑💼 Years at Current Job", 0, 40, 5,
help="Stability of employment, relevant to creditworthiness.")
derog = st.number_input("❗ Derogatory Reports *", 0, 10, 0,
help="Any serious negative credit reports. Affects risk significantly.")
delinq = st.number_input("⚠️ Delinquent Accounts", 0, 10, 0,
help="Number of accounts overdue or in collections.")
clage = st.number_input("📆 Oldest Credit Line (Months)", 0, 500, 120,
help="Age of the oldest credit line (longer is usually better).")
ninq = st.number_input("🔍 Recent Credit Inquiries", 0, 10, 0,
help="Too many inquiries may lower credit scores.")
st.markdown("#### 📌 Additional Information")
reason = st.selectbox("🎯 Loan Purpose (REASON) *", ['DebtCon', 'HomeImp'],
help="Reason for loan: 'DebtCon' = debt consolidation, 'HomeImp' = home improvement.")
job = st.selectbox("🧑 Employment Type (JOB) *", ['Other', 'Mgr', 'Office', 'Sales', 'ProfExe', 'Self'],
help="Job category affects financial stability prediction.")
submitted = st.form_submit_button("🔍 Assess Risk", type="primary")
# Optional Tip Box for User
st.markdown(f"""
<div style="padding:1rem; background-color:{COLOR_SCHEME['card']}; border-left: 5px solid {COLOR_SCHEME['primary']}; color:{COLOR_SCHEME['text']};">
<b>🧠 Tip:</b> Fields marked with <span style="color:red">*</span> are critical to your credit risk assessment.
Focus especially on <b>Loan Amount</b>, <b>Debt-to-Income Ratio</b>, and <b>Derogatory Reports</b> as they significantly influence the model's classification.
</div>
""", unsafe_allow_html=True)
if submitted and model is not None:
try:
# Input validation
if value <= 0:
st.warning("Property value must be positive")
if loan <= 0:
st.warning("Loan amount must be positive")
if mortdue < 0:
st.warning("Mortgage balance cannot be negative")
if debtinc < 0 or debtinc > 100:
st.warning("Debt-to-income ratio must be between 0-100%")
# Prepare input data
input_data = {
'LOAN': loan,
'VALUE': value,
'MORTDUE': mortdue,
'DEBTINC': debtinc,
'YOJ': yoj,
'DEROG': derog,
'DELINQ': delinq,
'CLAGE': clage,
'NINQ': ninq,
'CLNO': 20,
'REASON': reason,
'JOB': job
}
input_df = pd.DataFrame([input_data])
# Feature Engineering
input_df['LOAN_TO_VALUE'] = input_df['LOAN'] / input_df['VALUE']
input_df['LOAN_TO_MORTDUE'] = input_df['LOAN'] / (input_df['MORTDUE'] + 1)
input_df['DEROG_DELINQ_SUM'] = input_df['DEROG'] + input_df['DELINQ']
input_df['CLAGE_PER_CLNO'] = input_df['CLAGE'] / (input_df['CLNO'] + 1)
# EMI calculation
monthly_rate = 0.07 / 12
loan_term = 60
input_df['EMI'] = (input_df['LOAN'] * monthly_rate * (1 + monthly_rate)**loan_term) / \
((1 + monthly_rate)**loan_term - 1)
# Binning
input_df['YOJ_BINNED'] = pd.cut(input_df['YOJ'],
bins=[-1, 2, 5, 10, 20, 40],
labels=['0-2', '2-5', '5-10', '10-20', '20+'])
input_df['CLAGE_BINNED'] = pd.cut(input_df['CLAGE'],
bins=[-1, 60, 120, 180, 240, 500],
labels=['0-5', '5-10', '10-15', '15-20', '20+'])
# Encoding
input_df['REASON'] = input_df['REASON'].map({'HomeImp': 0, 'DebtCon': 1})
input_df['JOB'] = pd.factorize(input_df['JOB'])[0]
input_df['YOJ_BINNED'] = pd.factorize(input_df['YOJ_BINNED'])[0]
input_df['CLAGE_BINNED'] = pd.factorize(input_df['CLAGE_BINNED'])[0]
# Strict feature alignment with model
if hasattr(model, 'feature_names_in_'):
# Remove any extra columns not in model's features
input_df = input_df.reindex(columns=model.feature_names_in_, fill_value=0)
else:
# Fallback for models without feature names (legacy)
st.warning("⚠️ Model feature names not available - ensure manual alignment")
input_df = input_df.iloc[:, :19] # Keep only first 19 features if needed
# Prediction
prediction = model.predict(input_df)[0]
probability = model.predict_proba(input_df)[0][1]
# Result Display
if prediction == 1:
st.markdown(f"""
<div class="highlight-box">
<h2 style="color:{COLOR_SCHEME['danger']};">🚨 High Risk Alert</h2>
<p style="font-size:1.2rem;">Probability of Default: <strong>{probability:.1%}</strong></p>
<p>This application shows significant risk factors based on our analysis.</p>
</div>
""", unsafe_allow_html=True)
else:
st.markdown(f"""
<div class="highlight-box">
<h2 style="color:{COLOR_SCHEME['success']};">✅ Low Risk</h2>
<p style="font-size:1.2rem;">Probability of Default: <strong>{probability:.1%}</strong></p>
<p>This application meets our credit standards.</p>
</div>
""", unsafe_allow_html=True)
# SHAP Explanation
except Exception as e:
st.error(f"Error during prediction: {str(e)}")
# # Model Insights Section
# if model is not None:
# st.markdown("---")
# st.markdown("## Model Performance Analytics")
# # ROC Curve with proper data handling
# if 'BAD' in df.columns:
# try:
# with st.spinner('Calculating model performance metrics...'):
# X = df.drop('BAD', axis=1)
# y = df['BAD']
# # Align columns with model's expected features
# if hasattr(model, 'feature_names_in_'):
# available_features = [col for col in model.feature_names_in_ if col in X.columns]
# X = X[available_features]
# # Add missing features with default values
# missing_features = set(model.feature_names_in_) - set(X.columns)
# for feature in missing_features:
# X[feature] = 0
# # Reorder columns to match model expectations
# X = X[model.feature_names_in_]
# probas = model.predict_proba(X)[:, 1]
# fpr, tpr, thresholds = roc_curve(y, probas)
# roc_auc = auc(fpr, tpr)
# with st.container():
# st.markdown("#### Model Discrimination Ability (ROC Curve)")
# fig = go.Figure()
# fig.add_trace(go.Scatter(
# x=fpr, y=tpr,
# mode='lines',
# line=dict(color=COLOR_SCHEME['primary'], width=3),
# name=f'ROC Curve (AUC = {roc_auc:.2f})'
# ))
# fig.add_trace(go.Scatter(
# x=[0, 1], y=[0, 1],
# mode='lines',
# line=dict(color=COLOR_SCHEME['danger'], dash='dash'),
# name='Random Guessing'
# ))
# fig.update_layout(
# xaxis_title='False Positive Rate',
# yaxis_title='True Positive Rate',
# height=500,
# plot_bgcolor=COLOR_SCHEME['card'],
# paper_bgcolor=COLOR_SCHEME['background'],
# font=dict(color=COLOR_SCHEME['text']),
# margin=dict(l=50, r=50, b=50, t=50),
# legend=dict(
# orientation="h",
# yanchor="bottom",
# y=1.02,
# xanchor="right",
# x=1
# )
# )
# st.plotly_chart(fig, use_container_width=True)
# st.markdown(f"""
# <div style="color:{COLOR_SCHEME['text']};">
# <p>The ROC curve shows the trade-off between true positive rate (sensitivity) and false positive rate (1-specificity).
# Our model achieves an AUC of <strong>{roc_auc:.2f}</strong>, indicating good discrimination ability.
# An AUC of 1.0 represents perfect prediction, while 0.5 represents random guessing.</p>
# </div>
# """, unsafe_allow_html=True)
# except Exception as e:
# st.error(f"Error generating ROC curve: {str(e)}")
# Assuming COLOR_SCHEME and df, model are already defined
if model is not None:
st.markdown("---")
st.markdown("## Model Performance Analytics")
if 'BAD' in df.columns:
try:
with st.spinner('Calculating model performance metrics...'):
# --- Feature Engineering ---
input_df = engineer_features(df)
# --- Prepare Input Data ---
X = input_df.drop('BAD', axis=1)
y = input_df['BAD']
# --- Align with model's expected input ---
X = align_features(X, model)
# --- Predict Probabilities and Labels ---
y_proba = model.predict_proba(X)[:, 1]
y_pred = model.predict(X)
# --- ROC Curve ---
roc_fig, auc_val = plot_roc(y, y_proba, COLOR_SCHEME)
st.markdown("### ROC Curve")
st.plotly_chart(roc_fig, use_container_width=True)
st.markdown(f"""
<div style="color:{COLOR_SCHEME['text']};">
<p>The ROC curve shows the trade-off between true positive rate (sensitivity) and false positive rate (1-specificity).
Our model achieves an AUC of <strong>{auc_val:.2f}</strong>.</p>
</div>
""", unsafe_allow_html=True)
# --- Precision-Recall Curve ---
st.markdown("### Precision-Recall Curve")
pr_fig = plot_precision_recall(y, y_proba, COLOR_SCHEME)
st.plotly_chart(pr_fig, use_container_width=True)
# --- Confusion Matrix ---
st.markdown("### Confusion Matrix")
cm_fig = plot_confusion_matrix(y, y_pred, COLOR_SCHEME)
st.plotly_chart(cm_fig, use_container_width=True)
# --- Classification Report ---
st.markdown("### Classification Report")
report = generate_classification_report(y, y_pred)
st.text(report)
except Exception as e:
st.error(f"Error generating performance metrics: {e}")
# Feature Importance with proper handling
# Feature mapping dictionary - replace with your actual feature names
FEATURE_MAPPING = {
'Feature 0': 'Loan Amount',
'Feature 1': 'Credit Score',
'Feature 2': 'Debt-to-Income Ratio',
'Feature 4': 'Employment Length',
'Feature 5': 'Annual Income',
'Feature 8': 'Loan Purpose',
'Feature 9': 'Years at Current Address',
'Feature 10': 'Number of Open Accounts',
'Feature 11': 'Number of Credit Problems',
'Feature 12': 'Current Credit Balance',
'Feature 13': 'Maximum Open Credit',
'Feature 14': 'Bankruptcies',
'Feature 15': 'Tax Liens',
'Feature 17': 'Number of Late Payments (30-59 days)',
'Feature 18': 'Number of Late Payments (60-89 days)'
}
# Feature Importance with proper names
if hasattr(model, 'feature_importances_'):
try:
with st.container():
st.markdown("#### Key Risk Drivers (Feature Importance)")
# Get features and importances
if hasattr(model, 'feature_names_in_'):
features = model.feature_names_in_
importances = model.feature_importances_
else:
features = [f"Feature {i}" for i in range(len(model.feature_importances_))]
importances = model.feature_importances_
# Map feature numbers to proper names
named_features = [FEATURE_MAPPING.get(f, f) for f in features]
# Create DataFrame and sort
feat_imp = pd.DataFrame({
'Feature': named_features,
'Importance': importances,
'Original_Feature': features # Keep original for reference
}).sort_values('Importance', ascending=True)
# Create horizontal bar chart
fig = px.bar(
feat_imp.tail(15), # Show top 15 features
x='Importance',
y='Feature',
orientation='h',
color='Importance',
color_continuous_scale='Viridis',
title='',
hover_data=['Original_Feature'] # Show original feature number in tooltip
)
fig.update_layout(
height=500,
yaxis={'categoryorder':'total ascending'},
margin=dict(l=100, r=50, b=50, t=50),
plot_bgcolor=COLOR_SCHEME['card'],
paper_bgcolor=COLOR_SCHEME['background'],
font=dict(color=COLOR_SCHEME['text']),
coloraxis_colorbar=dict(
title='Importance',
tickfont=dict(color=COLOR_SCHEME['text']))
)
st.plotly_chart(fig, use_container_width=True)
st.markdown(f"""
<div style="color:{COLOR_SCHEME['text']};">
<p>This chart shows which factors most influence the model's risk assessment.
Higher values indicate features that contribute more to predicting loan defaults.
Understanding these drivers helps in making informed lending decisions and explaining model behavior.</p>
<p><small>Hover over bars to see the original feature numbers</small></p>
</div>
""", unsafe_allow_html=True)
except Exception as e:
st.error(f"Error generating feature importance: {str(e)}")
# Data Analytics Tab
with tab2:
st.markdown(f"""
<div style="margin-bottom:30px;">
<h1 style="color:{COLOR_SCHEME['header']};">Loan Portfolio Insights</h1>
<p style="font-size:1.1rem; color:{COLOR_SCHEME['text']};">Interactive visualizations of our loan portfolio characteristics</p>
</div>
""", unsafe_allow_html=True)
if not filtered_df.empty:
# KPI Cards
st.markdown("### Portfolio Overview")
cols = st.columns(4)
with cols[0]:
st.markdown(f"""
<div class="metric-card animated-card">
<h3>Total Loans</h3>
<h2>{len(filtered_df):,}</h2>
</div>
""", unsafe_allow_html=True)
with cols[1]:
default_rate = filtered_df['BAD'].mean() if 'BAD' in filtered_df.columns else 0
st.markdown(f"""
<div class="metric-card animated-card">
<h3>Default Rate</h3>
<h2>{default_rate:.1%}</h2>
</div>
""", unsafe_allow_html=True)
with cols[2]:
avg_loan = filtered_df['LOAN'].mean() if 'LOAN' in filtered_df.columns else 0
st.markdown(f"""
<div class="metric-card animated-card">
<h3>Avg Loan Amount</h3>
<h2>${avg_loan:,.0f}</h2>
</div>
""", unsafe_allow_html=True)
with cols[3]:
avg_debtinc = filtered_df['DEBTINC'].mean() if 'DEBTINC' in filtered_df.columns else 0
st.markdown(f"""
<div class="metric-card animated-card">
<h3>Avg Debt-to-Income</h3>
<h2>{avg_debtinc:.1f}%</h2>
</div>
""", unsafe_allow_html=True)
# Pie Charts Section
st.markdown("### Portfolio Composition")
pie_col1, pie_col2 = st.columns(2)
with pie_col1:
if 'REASON' in filtered_df.columns:
reason_counts = filtered_df['REASON'].value_counts()
fig = px.pie(
reason_counts,
values=reason_counts.values,
names=reason_counts.index.map({0: 'Home Improvement', 1: 'Debt Consolidation'}),
title='Loan Purpose Distribution',
color_discrete_sequence=[COLOR_SCHEME['primary'], COLOR_SCHEME['secondary']]
)
fig.update_layout(
plot_bgcolor=COLOR_SCHEME['card'],
paper_bgcolor=COLOR_SCHEME['background'],
font=dict(color=COLOR_SCHEME['text']),
height=400
)
st.plotly_chart(fig, use_container_width=True)
with pie_col2:
if 'BAD' in filtered_df.columns:
status_counts = filtered_df['BAD'].value_counts()
fig = px.pie(
status_counts,
values=status_counts.values,
names=status_counts.index.map({0: 'Good Loans', 1: 'Bad Loans'}),
title='Loan Status Distribution',
color_discrete_sequence=[COLOR_SCHEME['success'], COLOR_SCHEME['danger']]
)
fig.update_layout(
plot_bgcolor=COLOR_SCHEME['card'],
paper_bgcolor=COLOR_SCHEME['background'],
font=dict(color=COLOR_SCHEME['text']),
height=400
)
st.plotly_chart(fig, use_container_width=True)
# Distribution Plots
st.markdown("### Feature Distributions")
dist_col1, dist_col2 = st.columns(2)
with dist_col1:
if 'LOAN' in filtered_df.columns:
# Extract loan data for KDE
loan_data = filtered_df['LOAN'].dropna()
# Create the Histogram
hist = go.Histogram(
x=loan_data,
nbinsx=30,
marker_color=COLOR_SCHEME['primary'],
opacity=0.6,
name='Loan Amount Distribution'
)
# Generate the KDE line
kde = gaussian_kde(loan_data)
x_range = np.linspace(loan_data.min(), loan_data.max(), 500)
kde_y = kde(x_range) * len(loan_data) * (loan_data.max() - loan_data.min()) / 30 # Normalize to match histogram scale
# KDE Line plot
kde_line = go.Scatter(
x=x_range,
y=kde_y,
mode='lines',
line=dict(color=COLOR_SCHEME['secondary'], width=2),
name='KDE Curve'
)
# Combine histogram and KDE line
fig = go.Figure(data=[hist, kde_line])
# Update Layout
fig.update_layout(
plot_bgcolor=COLOR_SCHEME['card'],
paper_bgcolor=COLOR_SCHEME['background'],
font=dict(color=COLOR_SCHEME['text']),
height=400,
xaxis_title='Loan Amount (USD)',