sourize commited on
Commit Β·
0dde7ab
1
Parent(s): 33011f9
Updated UI
Browse files
app.py
CHANGED
|
@@ -8,13 +8,14 @@ import seaborn as sns
|
|
| 8 |
from datetime import datetime, time
|
| 9 |
import plotly.express as px
|
| 10 |
import plotly.graph_objects as go
|
| 11 |
-
from
|
|
|
|
| 12 |
import warnings
|
| 13 |
warnings.filterwarnings('ignore')
|
| 14 |
|
| 15 |
-
#
|
| 16 |
st.set_page_config(
|
| 17 |
-
page_title="
|
| 18 |
page_icon="π",
|
| 19 |
layout="wide",
|
| 20 |
initial_sidebar_state="expanded"
|
|
@@ -24,723 +25,570 @@ st.set_page_config(
|
|
| 24 |
st.markdown("""
|
| 25 |
<style>
|
| 26 |
.main-header {
|
| 27 |
-
font-size:
|
| 28 |
-
font-weight: bold;
|
| 29 |
color: #1f77b4;
|
| 30 |
text-align: center;
|
| 31 |
margin-bottom: 2rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
-
.
|
|
|
|
| 34 |
padding: 1rem;
|
| 35 |
border-radius: 10px;
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
font-size: 1.2rem;
|
| 39 |
-
font-weight: bold;
|
| 40 |
}
|
| 41 |
-
.fraud-
|
| 42 |
background-color: #ffebee;
|
| 43 |
-
border: 2px solid #f44336;
|
| 44 |
color: #c62828;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
-
.
|
| 47 |
background-color: #e8f5e8;
|
| 48 |
-
border: 2px solid #4caf50;
|
| 49 |
color: #2e7d32;
|
| 50 |
-
}
|
| 51 |
-
.metric-card {
|
| 52 |
-
background-color: #f8f9fa;
|
| 53 |
padding: 1rem;
|
| 54 |
-
border-radius: 8px;
|
| 55 |
-
border-left: 4px solid #1f77b4;
|
| 56 |
-
color: #333333;
|
| 57 |
-
}
|
| 58 |
-
.metric-card h4 {
|
| 59 |
-
color: #1f77b4;
|
| 60 |
-
margin-bottom: 0.5rem;
|
| 61 |
-
font-weight: bold;
|
| 62 |
-
}
|
| 63 |
-
.metric-card ul, .metric-card li {
|
| 64 |
-
color: #333333;
|
| 65 |
-
margin: 0;
|
| 66 |
-
padding-left: 1.2rem;
|
| 67 |
-
}
|
| 68 |
-
.input-section {
|
| 69 |
-
background-color: #f8f9fa;
|
| 70 |
-
padding: 1.5rem;
|
| 71 |
border-radius: 10px;
|
| 72 |
-
|
| 73 |
-
border: 1px solid #dee2e6;
|
| 74 |
-
}
|
| 75 |
-
.performance-metric {
|
| 76 |
-
background-color: #ffffff;
|
| 77 |
-
padding: 1rem;
|
| 78 |
-
border-radius: 8px;
|
| 79 |
-
border: 1px solid #dee2e6;
|
| 80 |
-
margin: 0.5rem 0;
|
| 81 |
-
text-align: center;
|
| 82 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 83 |
-
color: #333333;
|
| 84 |
-
}
|
| 85 |
-
.performance-metric h4 {
|
| 86 |
-
color: #1f77b4;
|
| 87 |
-
margin-bottom: 0.5rem;
|
| 88 |
font-weight: bold;
|
| 89 |
-
font-size: 1.1rem;
|
| 90 |
-
}
|
| 91 |
-
.performance-metric p {
|
| 92 |
-
color: #333333;
|
| 93 |
}
|
| 94 |
-
.
|
| 95 |
-
color: #
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
gap: 2px;
|
| 100 |
-
}
|
| 101 |
-
.stTabs [data-baseweb="tab"] {
|
| 102 |
-
height: 50px;
|
| 103 |
-
padding-left: 20px;
|
| 104 |
-
padding-right: 20px;
|
| 105 |
}
|
| 106 |
</style>
|
| 107 |
""", unsafe_allow_html=True)
|
| 108 |
|
|
|
|
| 109 |
@st.cache_resource
|
| 110 |
def load_models():
|
| 111 |
-
"""Load the trained model and label encoder"""
|
| 112 |
try:
|
| 113 |
model = joblib.load('lightgbm_model.pkl')
|
| 114 |
-
|
| 115 |
-
return model,
|
| 116 |
-
except FileNotFoundError
|
| 117 |
-
st.error(
|
| 118 |
-
|
| 119 |
-
st.stop()
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
try:
|
| 137 |
-
location_encoded =
|
| 138 |
except ValueError:
|
| 139 |
-
|
| 140 |
location_encoded = 0
|
|
|
|
| 141 |
|
| 142 |
# Create feature vector
|
| 143 |
features = pd.DataFrame({
|
| 144 |
'Transaction Amount': [transaction_amount],
|
| 145 |
-
'Transaction Date': [
|
| 146 |
'Customer Age': [customer_age],
|
| 147 |
'Account Age Days': [account_age_days],
|
| 148 |
-
'Transaction Time': [
|
| 149 |
'Customer Location Encoded': [location_encoded]
|
| 150 |
})
|
| 151 |
|
| 152 |
return features
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
"
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
except:
|
| 160 |
-
return ["Unknown Location"]
|
| 161 |
|
| 162 |
-
|
| 163 |
-
"""Create SHAP explanation plots"""
|
| 164 |
-
explainer = shap.TreeExplainer(model)
|
| 165 |
-
shap_values = explainer.shap_values(features)
|
| 166 |
-
|
| 167 |
-
if isinstance(shap_values, list):
|
| 168 |
-
shap_values_fraud = shap_values[1]
|
| 169 |
-
expected_value = explainer.expected_value[1]
|
| 170 |
-
else:
|
| 171 |
-
shap_values_fraud = shap_values
|
| 172 |
-
expected_value = explainer.expected_value
|
| 173 |
-
|
| 174 |
-
return shap_values_fraud, expected_value, explainer
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
fig, ax = plt.subplots(figsize=(10, 6))
|
| 179 |
-
|
| 180 |
-
feature_values = features.iloc[0].values
|
| 181 |
-
shap_vals = shap_values[0]
|
| 182 |
-
|
| 183 |
-
cumulative = expected_value
|
| 184 |
-
positions = []
|
| 185 |
-
values = []
|
| 186 |
-
labels = []
|
| 187 |
-
colors = []
|
| 188 |
-
|
| 189 |
-
positions.append(0)
|
| 190 |
-
values.append(expected_value)
|
| 191 |
-
labels.append(f"Base Value\n{expected_value:.3f}")
|
| 192 |
-
colors.append('gray')
|
| 193 |
-
|
| 194 |
-
for i, (feature, shap_val, feat_val) in enumerate(zip(feature_names, shap_vals, feature_values)):
|
| 195 |
-
positions.append(i + 1)
|
| 196 |
-
values.append(cumulative + shap_val)
|
| 197 |
-
labels.append(f"{feature}\n{feat_val:.3f}\nSHAP: {shap_val:.3f}")
|
| 198 |
-
colors.append('red' if shap_val > 0 else 'blue')
|
| 199 |
-
cumulative += shap_val
|
| 200 |
-
|
| 201 |
-
positions.append(len(feature_names) + 1)
|
| 202 |
-
values.append(cumulative)
|
| 203 |
-
labels.append(f"Final Score\n{cumulative:.3f}")
|
| 204 |
-
colors.append('green' if cumulative > 0 else 'orange')
|
| 205 |
-
|
| 206 |
-
bars = ax.bar(positions, values, color=colors, alpha=0.7)
|
| 207 |
-
|
| 208 |
-
for i in range(len(positions) - 1):
|
| 209 |
-
ax.plot([positions[i] + 0.4, positions[i + 1] - 0.4],
|
| 210 |
-
[values[i], values[i]], 'k--', alpha=0.5)
|
| 211 |
-
|
| 212 |
-
ax.set_xticks(positions)
|
| 213 |
-
ax.set_xticklabels(labels, rotation=45, ha='right')
|
| 214 |
-
ax.set_ylabel('SHAP Value Contribution')
|
| 215 |
-
ax.set_title('SHAP Waterfall Plot - Feature Contributions to Fraud Prediction')
|
| 216 |
-
ax.grid(True, alpha=0.3)
|
| 217 |
-
ax.axhline(y=0, color='black', linestyle='-', alpha=0.5)
|
| 218 |
-
|
| 219 |
-
plt.tight_layout()
|
| 220 |
-
return fig
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
sample_locations = get_sample_locations(label_encoder)
|
| 229 |
|
| 230 |
# Input section
|
| 231 |
-
st.markdown('<
|
| 232 |
-
st.subheader("π Transaction Information")
|
| 233 |
|
| 234 |
-
|
| 235 |
-
col1, col2, col3 = st.columns(3)
|
| 236 |
|
| 237 |
with col1:
|
| 238 |
-
transaction_amount = st.number_input(
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
max_value=10000.0,
|
| 242 |
-
value=100.0,
|
| 243 |
-
step=0.01,
|
| 244 |
-
help="Enter the transaction amount in dollars"
|
| 245 |
-
)
|
| 246 |
-
|
| 247 |
-
customer_age = st.slider(
|
| 248 |
-
"π€ Customer Age",
|
| 249 |
-
min_value=16,
|
| 250 |
-
max_value=100,
|
| 251 |
-
value=35,
|
| 252 |
-
help="Customer's age in years"
|
| 253 |
-
)
|
| 254 |
|
| 255 |
with col2:
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
)
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
value=365,
|
| 267 |
-
help="How many days old is the customer's account"
|
| 268 |
-
)
|
| 269 |
-
|
| 270 |
-
with col3:
|
| 271 |
-
transaction_time = st.time_input(
|
| 272 |
-
"β° Transaction Time",
|
| 273 |
-
value=time(12, 0),
|
| 274 |
-
help="Select the time of the transaction"
|
| 275 |
-
)
|
| 276 |
-
|
| 277 |
-
customer_location = st.selectbox(
|
| 278 |
-
"π Customer Location",
|
| 279 |
-
options=sample_locations,
|
| 280 |
-
index=0,
|
| 281 |
-
help="Select customer's location"
|
| 282 |
-
)
|
| 283 |
-
|
| 284 |
-
# Manual location input
|
| 285 |
-
manual_location = st.text_input(
|
| 286 |
-
"πΊοΈ Or enter location manually:",
|
| 287 |
-
placeholder="Type location name",
|
| 288 |
-
help="Enter a specific location if not in dropdown"
|
| 289 |
-
)
|
| 290 |
-
|
| 291 |
-
if manual_location:
|
| 292 |
-
customer_location = manual_location
|
| 293 |
-
|
| 294 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 295 |
-
|
| 296 |
-
# Analysis button
|
| 297 |
-
analyze_col1, analyze_col2, analyze_col3 = st.columns([1, 1, 1])
|
| 298 |
-
with analyze_col2:
|
| 299 |
-
analyze_button = st.button("π Analyze Transaction", type="primary", use_container_width=True)
|
| 300 |
-
|
| 301 |
-
if analyze_button:
|
| 302 |
-
# Preprocess data
|
| 303 |
-
features = preprocess_data(
|
| 304 |
transaction_amount, transaction_date, customer_age,
|
| 305 |
-
customer_location, account_age_days, transaction_time,
|
| 306 |
)
|
| 307 |
|
| 308 |
# Make prediction
|
| 309 |
-
|
| 310 |
-
|
| 311 |
fraud_probability = prediction_proba[1]
|
| 312 |
|
| 313 |
-
#
|
| 314 |
-
st.
|
| 315 |
-
st.subheader("π Analysis Results")
|
| 316 |
|
| 317 |
-
|
| 318 |
-
result_col1, result_col2 = st.columns([2, 1])
|
| 319 |
-
|
| 320 |
-
with result_col1:
|
| 321 |
if prediction == 1:
|
| 322 |
-
st.markdown(
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
st.markdown(
|
| 329 |
-
f'<div class="prediction-box legitimate-box">β
LEGITIMATE TRANSACTION<br>'
|
| 330 |
-
f'Fraud Probability: {fraud_probability:.2%}</div>',
|
| 331 |
-
unsafe_allow_html=True
|
| 332 |
-
)
|
| 333 |
-
|
| 334 |
-
with result_col2:
|
| 335 |
-
# Risk level
|
| 336 |
-
if fraud_probability >= 0.8:
|
| 337 |
-
risk_level = "π΄ Very High"
|
| 338 |
-
risk_color = "#f44336"
|
| 339 |
-
elif fraud_probability >= 0.6:
|
| 340 |
-
risk_level = "π High"
|
| 341 |
-
risk_color = "#ff9800"
|
| 342 |
-
elif fraud_probability >= 0.4:
|
| 343 |
-
risk_level = "π‘ Medium"
|
| 344 |
-
risk_color = "#ffc107"
|
| 345 |
else:
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
detail_col1, detail_col2 = st.columns(2)
|
| 356 |
-
|
| 357 |
-
with detail_col1:
|
| 358 |
-
# Input features display
|
| 359 |
-
st.write("**π Input Features:**")
|
| 360 |
-
feature_df = pd.DataFrame({
|
| 361 |
-
'Feature': ['Transaction Amount', 'Transaction Date', 'Customer Age',
|
| 362 |
-
'Account Age Days', 'Transaction Time', 'Customer Location'],
|
| 363 |
-
'Value': [f"${transaction_amount:.2f}", str(transaction_date), f"{customer_age} years",
|
| 364 |
-
f"{account_age_days} days", str(transaction_time), customer_location]
|
| 365 |
-
})
|
| 366 |
-
st.dataframe(feature_df, use_container_width=True)
|
| 367 |
-
|
| 368 |
-
with detail_col2:
|
| 369 |
-
# Probability gauge
|
| 370 |
-
fig_gauge = go.Figure(go.Indicator(
|
| 371 |
mode = "gauge+number",
|
| 372 |
value = fraud_probability * 100,
|
| 373 |
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 374 |
-
title = {'text': "Fraud
|
| 375 |
gauge = {
|
| 376 |
'axis': {'range': [None, 100]},
|
| 377 |
-
'bar': {'color':
|
| 378 |
'steps': [
|
| 379 |
-
{'range': [0,
|
| 380 |
-
{'range': [
|
| 381 |
-
{'range': [
|
| 382 |
-
{'range': [75, 100], 'color': "red"}
|
| 383 |
],
|
| 384 |
'threshold': {
|
| 385 |
'line': {'color': "red", 'width': 4},
|
| 386 |
'thickness': 0.75,
|
| 387 |
-
'value':
|
| 388 |
}
|
| 389 |
}
|
| 390 |
))
|
| 391 |
-
|
| 392 |
-
st.plotly_chart(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
# SHAP Explanations
|
| 395 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
)
|
| 401 |
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
shap_df = shap_df.reindex(shap_df['SHAP Value'].abs().sort_values(ascending=False).index)
|
| 413 |
-
|
| 414 |
-
fig_bar = px.bar(
|
| 415 |
-
shap_df,
|
| 416 |
-
x='SHAP Value',
|
| 417 |
-
y='Feature',
|
| 418 |
-
orientation='h',
|
| 419 |
-
color='SHAP Value',
|
| 420 |
-
color_continuous_scale=['blue', 'white', 'red'],
|
| 421 |
-
title="SHAP Feature Importance"
|
| 422 |
-
)
|
| 423 |
-
fig_bar.update_layout(height=400)
|
| 424 |
-
st.plotly_chart(fig_bar, use_container_width=True)
|
| 425 |
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
- π΄ **Positive values (red)**: Push prediction towards FRAUD
|
| 437 |
-
- π΅ **Negative values (blue)**: Push prediction towards LEGITIMATE
|
| 438 |
-
- **Magnitude**: Larger absolute values have stronger influence
|
| 439 |
-
""")
|
| 440 |
|
| 441 |
-
|
| 442 |
-
top_features = shap_df.head(3)
|
| 443 |
-
st.write("**π Top 3 Contributing Features:**")
|
| 444 |
-
for i, (_, row) in enumerate(top_features.iterrows(), 1):
|
| 445 |
-
direction = "towards FRAUD" if row['SHAP Value'] > 0 else "towards LEGITIMATE"
|
| 446 |
-
st.write(f"**{i}.** **{row['Feature']}** (value: {row['Feature Value']:.3f}): "
|
| 447 |
-
f"Contributes {abs(row['SHAP Value']):.3f} {direction}")
|
| 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 |
-
</div>
|
| 485 |
-
""", unsafe_allow_html=True)
|
| 486 |
-
|
| 487 |
-
with info_col3:
|
| 488 |
-
st.markdown("""
|
| 489 |
-
<div class="metric-card">
|
| 490 |
-
<h4>π AI Explainability</h4>
|
| 491 |
-
<ul>
|
| 492 |
-
<li>SHAP values</li>
|
| 493 |
-
<li>Feature contributions</li>
|
| 494 |
-
<li>Waterfall explanations</li>
|
| 495 |
-
<li>Risk assessment</li>
|
| 496 |
-
</ul>
|
| 497 |
-
</div>
|
| 498 |
-
""", unsafe_allow_html=True)
|
| 499 |
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
st.markdown('<
|
| 503 |
-
|
| 504 |
-
st.markdown("""
|
| 505 |
-
This page compares our fraud detection model's performance against industry standards
|
| 506 |
-
and benchmarks to demonstrate its effectiveness.
|
| 507 |
-
""")
|
| 508 |
-
|
| 509 |
-
# Performance metrics comparison
|
| 510 |
-
st.subheader("π― Performance Metrics Comparison")
|
| 511 |
-
|
| 512 |
-
# Create comparison data
|
| 513 |
-
comparison_data = {
|
| 514 |
-
'Metric': ['Accuracy', 'Precision (Fraud)', 'Recall (Fraud)', 'F1-Score (Fraud)', 'ROC AUC', 'Processing Time'],
|
| 515 |
-
'Our Model': ['86%', '19%', '58%', '29%', '75.2%', '< 1 second'],
|
| 516 |
-
'Industry Average': ['85-92%', '15-25%', '40-60%', '25-35%', '70-80%', '1-3 seconds'],
|
| 517 |
-
'Best in Class': ['95%', '40%', '80%', '55%', '90%', '< 0.5 seconds'],
|
| 518 |
-
'Status': ['β
Above Average', 'β
Within Range', 'β
Good', 'β
Good', 'β
Good', 'β
Excellent']
|
| 519 |
-
}
|
| 520 |
|
| 521 |
-
|
| 522 |
-
st.
|
| 523 |
|
| 524 |
-
|
| 525 |
-
col1, col2 = st.columns(2)
|
| 526 |
|
| 527 |
with col1:
|
| 528 |
-
st.subheader("π Strengths")
|
| 529 |
st.markdown("""
|
| 530 |
-
<div class="
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
</div>
|
| 534 |
-
|
| 535 |
-
<div class="performance-metric">
|
| 536 |
-
<h4>β‘ Fast Processing</h4>
|
| 537 |
-
<p>Real-time analysis in under 1 second per transaction</p>
|
| 538 |
-
</div>
|
| 539 |
-
|
| 540 |
-
<div class="performance-metric">
|
| 541 |
-
<h4>π Explainable AI</h4>
|
| 542 |
-
<p>SHAP values provide clear reasoning for each prediction</p>
|
| 543 |
-
</div>
|
| 544 |
-
|
| 545 |
-
<div class="performance-metric">
|
| 546 |
-
<h4>π Good ROC AUC (75.2%)</h4>
|
| 547 |
-
<p>Strong ability to distinguish between fraud and legitimate transactions</p>
|
| 548 |
</div>
|
| 549 |
""", unsafe_allow_html=True)
|
| 550 |
|
| 551 |
with col2:
|
| 552 |
-
st.subheader("β οΈ Areas for Improvement")
|
| 553 |
st.markdown("""
|
| 554 |
-
<div class="
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
</div>
|
| 558 |
-
|
| 559 |
-
<div class="performance-metric">
|
| 560 |
-
<h4>π Class Imbalance</h4>
|
| 561 |
-
<p>Fraud is only ~5% of data, making precision challenging</p>
|
| 562 |
</div>
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
|
|
|
|
|
|
|
|
|
| 567 |
</div>
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
|
|
|
|
|
|
|
|
|
| 572 |
</div>
|
| 573 |
""", unsafe_allow_html=True)
|
| 574 |
|
| 575 |
-
#
|
| 576 |
-
st.
|
| 577 |
|
| 578 |
-
|
|
|
|
|
|
|
| 579 |
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
|
| 588 |
-
#
|
| 589 |
-
|
| 590 |
-
tpr_industry = 1 - (1 - fpr_industry) ** 2.5 # Approximated curve for AUC ~0.75
|
| 591 |
|
| 592 |
-
#
|
| 593 |
-
|
| 594 |
-
|
| 595 |
|
| 596 |
-
|
| 597 |
-
x=fpr_our, y=tpr_our,
|
| 598 |
-
mode='lines',
|
| 599 |
-
name='Our Model (AUC = 0.752)',
|
| 600 |
-
line=dict(color='blue', width=3)
|
| 601 |
-
))
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
fig_roc.add_trace(go.Scatter(
|
| 611 |
-
x=fpr_best, y=tpr_best,
|
| 612 |
-
mode='lines',
|
| 613 |
-
name='Best in Class (AUC = 0.90)',
|
| 614 |
-
line=dict(color='green', width=2, dash='dot')
|
| 615 |
-
))
|
| 616 |
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
)
|
| 631 |
|
| 632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
|
| 634 |
-
with
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
|
| 641 |
-
|
| 642 |
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
|
|
|
|
|
|
| 650 |
|
| 651 |
-
|
| 652 |
-
r=industry_scores,
|
| 653 |
-
theta=metrics,
|
| 654 |
-
fill='toself',
|
| 655 |
-
name='Industry Average',
|
| 656 |
-
line_color='orange'
|
| 657 |
-
))
|
| 658 |
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
|
|
|
|
|
|
| 666 |
|
| 667 |
-
|
| 668 |
-
polar=dict(
|
| 669 |
-
radialaxis=dict(
|
| 670 |
-
visible=True,
|
| 671 |
-
range=[0, 100]
|
| 672 |
-
)),
|
| 673 |
-
showlegend=True,
|
| 674 |
-
title="Performance Metrics Radar Chart",
|
| 675 |
-
height=400
|
| 676 |
-
)
|
| 677 |
|
| 678 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
|
| 680 |
# Business Impact
|
| 681 |
-
st.
|
| 682 |
|
| 683 |
-
|
| 684 |
|
| 685 |
-
with
|
| 686 |
st.markdown("""
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
""", unsafe_allow_html=True)
|
| 693 |
|
| 694 |
-
with
|
| 695 |
st.markdown("""
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
""", unsafe_allow_html=True)
|
| 702 |
|
| 703 |
-
with
|
| 704 |
st.markdown("""
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
""", unsafe_allow_html=True)
|
| 711 |
-
|
| 712 |
-
# Improvement roadmap
|
| 713 |
-
st.subheader("π Improvement Roadmap")
|
| 714 |
-
|
| 715 |
-
roadmap_data = {
|
| 716 |
-
'Phase': ['Phase 1 (Current)', 'Phase 2 (Q3 2025)', 'Phase 3 (Q1 2026)', 'Phase 4 (Q3 2026)'],
|
| 717 |
-
'Focus': ['Baseline Model', 'Feature Engineering', 'Model Ensemble', 'Deep Learning'],
|
| 718 |
-
'Expected Precision': ['19%', '25%', '32%', '38%'],
|
| 719 |
-
'Expected Recall': ['58%', '62%', '68%', '75%'],
|
| 720 |
-
'Expected F1-Score': ['29%', '36%', '44%', '50%']
|
| 721 |
-
}
|
| 722 |
-
|
| 723 |
-
roadmap_df = pd.DataFrame(roadmap_data)
|
| 724 |
-
st.dataframe(roadmap_df, use_container_width=True)
|
| 725 |
-
|
| 726 |
-
st.info("""
|
| 727 |
-
**π Note:** Performance comparisons are based on industry research and benchmarks.
|
| 728 |
-
Actual performance may vary depending on data quality, feature availability, and specific use cases.
|
| 729 |
-
""")
|
| 730 |
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
)
|
| 739 |
-
|
| 740 |
-
if page == "Fraud Detection":
|
| 741 |
-
fraud_detection_page()
|
| 742 |
-
elif page == "Model Performance":
|
| 743 |
-
model_performance_page()
|
| 744 |
-
|
| 745 |
-
if __name__ == "__main__":
|
| 746 |
-
main()
|
|
|
|
| 8 |
from datetime import datetime, time
|
| 9 |
import plotly.express as px
|
| 10 |
import plotly.graph_objects as go
|
| 11 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 12 |
+
from sklearn.neighbors import NearestNeighbors
|
| 13 |
import warnings
|
| 14 |
warnings.filterwarnings('ignore')
|
| 15 |
|
| 16 |
+
# Page configuration
|
| 17 |
st.set_page_config(
|
| 18 |
+
page_title="π FraudLens: Explainable AI platform for real-time e-commerce fraud detection",
|
| 19 |
page_icon="π",
|
| 20 |
layout="wide",
|
| 21 |
initial_sidebar_state="expanded"
|
|
|
|
| 25 |
st.markdown("""
|
| 26 |
<style>
|
| 27 |
.main-header {
|
| 28 |
+
font-size: 3rem;
|
|
|
|
| 29 |
color: #1f77b4;
|
| 30 |
text-align: center;
|
| 31 |
margin-bottom: 2rem;
|
| 32 |
+
font-weight: bold;
|
| 33 |
+
}
|
| 34 |
+
.sub-header {
|
| 35 |
+
font-size: 1.5rem;
|
| 36 |
+
color: #ff7f0e;
|
| 37 |
+
margin-bottom: 1rem;
|
| 38 |
+
font-weight: bold;
|
| 39 |
}
|
| 40 |
+
.metric-card {
|
| 41 |
+
background-color: #f0f2f6;
|
| 42 |
padding: 1rem;
|
| 43 |
border-radius: 10px;
|
| 44 |
+
border-left: 5px solid #1f77b4;
|
| 45 |
+
margin: 0.5rem 0;
|
|
|
|
|
|
|
| 46 |
}
|
| 47 |
+
.fraud-alert {
|
| 48 |
background-color: #ffebee;
|
|
|
|
| 49 |
color: #c62828;
|
| 50 |
+
padding: 1rem;
|
| 51 |
+
border-radius: 10px;
|
| 52 |
+
border-left: 5px solid #c62828;
|
| 53 |
+
font-weight: bold;
|
| 54 |
}
|
| 55 |
+
.safe-alert {
|
| 56 |
background-color: #e8f5e8;
|
|
|
|
| 57 |
color: #2e7d32;
|
|
|
|
|
|
|
|
|
|
| 58 |
padding: 1rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
border-radius: 10px;
|
| 60 |
+
border-left: 5px solid #2e7d32;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
font-weight: bold;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
+
.sidebar-info {
|
| 64 |
+
background-color: #e3f2fd;
|
| 65 |
+
padding: 1rem;
|
| 66 |
+
border-radius: 10px;
|
| 67 |
+
margin: 1rem 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
}
|
| 69 |
</style>
|
| 70 |
""", unsafe_allow_html=True)
|
| 71 |
|
| 72 |
+
# Load models and encoders
|
| 73 |
@st.cache_resource
|
| 74 |
def load_models():
|
|
|
|
| 75 |
try:
|
| 76 |
model = joblib.load('lightgbm_model.pkl')
|
| 77 |
+
le_loc = joblib.load('customer_loc.pkl')
|
| 78 |
+
return model, le_loc
|
| 79 |
+
except FileNotFoundError:
|
| 80 |
+
st.error("β οΈ Model files not found. Please ensure 'lightgbm_model.pkl' and 'customer_loc.pkl' are in the same directory.")
|
| 81 |
+
return None, None
|
|
|
|
| 82 |
|
| 83 |
+
# Preprocessing functions
|
| 84 |
+
def preprocess_transaction_date(date_input):
|
| 85 |
+
"""Convert date to days since 1899-12-30"""
|
| 86 |
+
if isinstance(date_input, str):
|
| 87 |
+
date_obj = pd.to_datetime(date_input, dayfirst=True)
|
| 88 |
+
else:
|
| 89 |
+
date_obj = pd.to_datetime(date_input)
|
| 90 |
+
return (date_obj - pd.Timestamp("1899-12-30")).days
|
| 91 |
+
|
| 92 |
+
def preprocess_transaction_time(time_input):
|
| 93 |
+
"""Convert time to fraction of day"""
|
| 94 |
+
if isinstance(time_input, str):
|
| 95 |
+
time_obj = pd.to_datetime(time_input, format='%H:%M:%S').time()
|
| 96 |
+
else:
|
| 97 |
+
time_obj = time_input
|
| 98 |
+
return (time_obj.hour * 3600 + time_obj.minute * 60 + time_obj.second) / 86400
|
| 99 |
+
|
| 100 |
+
def create_prediction_data(transaction_amount, transaction_date, customer_age,
|
| 101 |
+
customer_location, account_age_days, transaction_time, le_loc):
|
| 102 |
+
"""Create properly formatted data for prediction"""
|
| 103 |
+
# Preprocess inputs
|
| 104 |
+
processed_date = preprocess_transaction_date(transaction_date)
|
| 105 |
+
processed_time = preprocess_transaction_time(transaction_time)
|
| 106 |
+
|
| 107 |
+
# Encode location
|
| 108 |
try:
|
| 109 |
+
location_encoded = le_loc.transform([customer_location])[0]
|
| 110 |
except ValueError:
|
| 111 |
+
# If location not in training data, use most frequent class
|
| 112 |
location_encoded = 0
|
| 113 |
+
st.warning(f"β οΈ Location '{customer_location}' not found in training data. Using default encoding.")
|
| 114 |
|
| 115 |
# Create feature vector
|
| 116 |
features = pd.DataFrame({
|
| 117 |
'Transaction Amount': [transaction_amount],
|
| 118 |
+
'Transaction Date': [processed_date],
|
| 119 |
'Customer Age': [customer_age],
|
| 120 |
'Account Age Days': [account_age_days],
|
| 121 |
+
'Transaction Time': [processed_time],
|
| 122 |
'Customer Location Encoded': [location_encoded]
|
| 123 |
})
|
| 124 |
|
| 125 |
return features
|
| 126 |
|
| 127 |
+
# Sidebar navigation
|
| 128 |
+
st.sidebar.info(
|
| 129 |
+
"### π FraudLens\n"
|
| 130 |
+
"Explainable AI platform for real-time e-commerce fraud detection"
|
| 131 |
+
)
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
page = st.sidebar.selectbox("Choose a page", ["π Main Dashboard", "π Model Analytics", "π¬ Model Details"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
# Load models
|
| 136 |
+
model, le_loc = load_models()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
if model is None or le_loc is None:
|
| 139 |
+
st.stop()
|
| 140 |
+
|
| 141 |
+
# Main Dashboard
|
| 142 |
+
if page == "π Main Dashboard":
|
| 143 |
+
st.markdown('<h1 class="main-header">π Fraud Detection Dashboard</h1>', unsafe_allow_html=True)
|
|
|
|
| 144 |
|
| 145 |
# Input section
|
| 146 |
+
st.markdown('<h2 class="sub-header">π Transaction Details</h2>', unsafe_allow_html=True)
|
|
|
|
| 147 |
|
| 148 |
+
col1, col2 = st.columns(2)
|
|
|
|
| 149 |
|
| 150 |
with col1:
|
| 151 |
+
transaction_amount = st.number_input("\ud83d\udcb0 Transaction Amount ($)", min_value=0.01, value=100.0, step=0.01)
|
| 152 |
+
transaction_date = st.date_input("\ud83d\udcc5 Transaction Date", value=datetime.now().date())
|
| 153 |
+
customer_age = st.number_input("\ud83d\udc64 Customer Age", min_value=15, max_value=100, value=35, step=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
with col2:
|
| 156 |
+
# Get unique locations from the encoder
|
| 157 |
+
location_options = list(le_loc.classes_)
|
| 158 |
+
customer_location = st.selectbox("π Customer Location", options=location_options[:100]) # Show first 100 for performance
|
| 159 |
+
account_age_days = st.number_input("π Account Age (Days)", min_value=1, value=30, step=1)
|
| 160 |
+
transaction_time = st.time_input("π Transaction Time", value=time(12, 0))
|
| 161 |
+
|
| 162 |
+
# Prediction button
|
| 163 |
+
if st.button("π Analyze Transaction", type="primary"):
|
| 164 |
+
# Create prediction data
|
| 165 |
+
prediction_data = create_prediction_data(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
transaction_amount, transaction_date, customer_age,
|
| 167 |
+
customer_location, account_age_days, transaction_time, le_loc
|
| 168 |
)
|
| 169 |
|
| 170 |
# Make prediction
|
| 171 |
+
prediction = model.predict(prediction_data)[0]
|
| 172 |
+
prediction_proba = model.predict_proba(prediction_data)[0]
|
| 173 |
fraud_probability = prediction_proba[1]
|
| 174 |
|
| 175 |
+
# Display results
|
| 176 |
+
col1, col2, col3 = st.columns(3)
|
|
|
|
| 177 |
|
| 178 |
+
with col1:
|
|
|
|
|
|
|
|
|
|
| 179 |
if prediction == 1:
|
| 180 |
+
st.markdown(f"""
|
| 181 |
+
<div class="fraud-alert">
|
| 182 |
+
π¨ FRAUD DETECTED<br>
|
| 183 |
+
Risk Score: {fraud_probability:.1%}
|
| 184 |
+
</div>
|
| 185 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
else:
|
| 187 |
+
st.markdown(f"""
|
| 188 |
+
<div class="safe-alert">
|
| 189 |
+
β
TRANSACTION SAFE<br>
|
| 190 |
+
Risk Score: {fraud_probability:.1%}
|
| 191 |
+
</div>
|
| 192 |
+
""", unsafe_allow_html=True)
|
| 193 |
+
|
| 194 |
+
with col2:
|
| 195 |
+
fig = go.Figure(go.Indicator(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
mode = "gauge+number",
|
| 197 |
value = fraud_probability * 100,
|
| 198 |
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 199 |
+
title = {'text': "Fraud Risk %"},
|
| 200 |
gauge = {
|
| 201 |
'axis': {'range': [None, 100]},
|
| 202 |
+
'bar': {'color': "darkblue"},
|
| 203 |
'steps': [
|
| 204 |
+
{'range': [0, 30], 'color': "lightgreen"},
|
| 205 |
+
{'range': [30, 70], 'color': "yellow"},
|
| 206 |
+
{'range': [70, 100], 'color': "red"}
|
|
|
|
| 207 |
],
|
| 208 |
'threshold': {
|
| 209 |
'line': {'color': "red", 'width': 4},
|
| 210 |
'thickness': 0.75,
|
| 211 |
+
'value': 50
|
| 212 |
}
|
| 213 |
}
|
| 214 |
))
|
| 215 |
+
fig.update_layout(height=300)
|
| 216 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 217 |
+
|
| 218 |
+
with col3:
|
| 219 |
+
st.metric("Fraud Probability", f"{fraud_probability:.1%}")
|
| 220 |
+
st.metric("Safe Probability", f"{1-fraud_probability:.1%}")
|
| 221 |
+
st.metric("Prediction", "FRAUD" if prediction == 1 else "SAFE")
|
| 222 |
|
| 223 |
# SHAP Explanations
|
| 224 |
+
st.markdown('<h2 class="sub-header">π¬ AI Explanation</h2>', unsafe_allow_html=True)
|
| 225 |
+
|
| 226 |
+
# Calculate SHAP values
|
| 227 |
+
explainer = shap.TreeExplainer(model)
|
| 228 |
+
shap_values = explainer.shap_values(prediction_data)
|
| 229 |
+
|
| 230 |
+
# 1. Waterfall plot for local explanation
|
| 231 |
+
col1, col2 = st.columns(2)
|
| 232 |
|
| 233 |
+
with col1:
|
| 234 |
+
st.subheader("π Feature Impact Analysis")
|
| 235 |
+
|
| 236 |
+
# Create SHAP explanation object
|
| 237 |
+
explanation = shap.Explanation(
|
| 238 |
+
values=shap_values[1][0], # For fraud class
|
| 239 |
+
base_values=explainer.expected_value[1],
|
| 240 |
+
data=prediction_data.iloc[0],
|
| 241 |
+
feature_names=list(prediction_data.columns)
|
| 242 |
)
|
| 243 |
|
| 244 |
+
# Create waterfall plot
|
| 245 |
+
fig_waterfall = plt.figure(figsize=(10, 6))
|
| 246 |
+
shap.plots.waterfall(explanation, max_display=6, show=False)
|
| 247 |
+
st.pyplot(fig_waterfall, bbox_inches='tight')
|
| 248 |
+
plt.close()
|
| 249 |
+
|
| 250 |
+
with col2:
|
| 251 |
+
st.subheader("π Feature Values vs Impact")
|
| 252 |
|
| 253 |
+
# Feature importance table
|
| 254 |
+
feature_impacts = pd.DataFrame({
|
| 255 |
+
'Feature': prediction_data.columns,
|
| 256 |
+
'Value': prediction_data.iloc[0].values,
|
| 257 |
+
'SHAP Impact': shap_values[1][0]
|
| 258 |
+
})
|
| 259 |
+
feature_impacts['Abs Impact'] = abs(feature_impacts['SHAP Impact'])
|
| 260 |
+
feature_impacts = feature_impacts.sort_values('Abs Impact', ascending=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
+
# Display as colored table
|
| 263 |
+
def color_impact(val):
|
| 264 |
+
if val > 0:
|
| 265 |
+
return 'background-color: #ffcdd2' # Light red for fraud-indicating
|
| 266 |
+
else:
|
| 267 |
+
return 'background-color: #c8e6c9' # Light green for safe-indicating
|
| 268 |
|
| 269 |
+
styled_df = feature_impacts[['Feature', 'Value', 'SHAP Impact']].style.applymap(
|
| 270 |
+
color_impact, subset=['SHAP Impact']
|
| 271 |
+
).format({'Value': '{:.2f}', 'SHAP Impact': '{:.4f}'})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
st.dataframe(styled_df, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
+
# 2. Force plot explanation
|
| 276 |
+
st.subheader("π― Decision Breakdown")
|
| 277 |
+
|
| 278 |
+
# Create a custom force plot visualization
|
| 279 |
+
base_value = explainer.expected_value[1]
|
| 280 |
+
shap_vals = shap_values[1][0]
|
| 281 |
+
|
| 282 |
+
# Sort features by absolute SHAP value
|
| 283 |
+
feature_importance = list(zip(prediction_data.columns, shap_vals, prediction_data.iloc[0].values))
|
| 284 |
+
feature_importance.sort(key=lambda x: abs(x[1]), reverse=True)
|
| 285 |
+
|
| 286 |
+
# Create horizontal bar chart
|
| 287 |
+
features = [f[0] for f in feature_importance]
|
| 288 |
+
impacts = [f[1] for f in feature_importance]
|
| 289 |
+
values = [f[2] for f in feature_importance]
|
| 290 |
+
|
| 291 |
+
colors = ['red' if impact > 0 else 'green' for impact in impacts]
|
| 292 |
+
|
| 293 |
+
fig_force = go.Figure(go.Bar(
|
| 294 |
+
y=features,
|
| 295 |
+
x=impacts,
|
| 296 |
+
orientation='h',
|
| 297 |
+
marker_color=colors,
|
| 298 |
+
text=[f"{feat}: {val:.2f}" for feat, val in zip(features, values)],
|
| 299 |
+
textposition="auto",
|
| 300 |
+
))
|
| 301 |
+
|
| 302 |
+
fig_force.update_layout(
|
| 303 |
+
title=f"Feature Impact on Fraud Prediction (Base: {base_value:.3f})",
|
| 304 |
+
xaxis_title="SHAP Value (Impact on Prediction)",
|
| 305 |
+
yaxis_title="Features",
|
| 306 |
+
height=400
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
st.plotly_chart(fig_force, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
+
# Model Analytics Page
|
| 312 |
+
elif page == "π Model Analytics":
|
| 313 |
+
st.markdown('<h1 class="main-header">π Model Analytics Dashboard</h1>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
+
# Sample data for demonstration (in real app, you'd load validation data)
|
| 316 |
+
st.markdown('<h2 class="sub-header">π― Model Performance Metrics</h2>', unsafe_allow_html=True)
|
| 317 |
|
| 318 |
+
col1, col2, col3, col4 = st.columns(4)
|
|
|
|
| 319 |
|
| 320 |
with col1:
|
|
|
|
| 321 |
st.markdown("""
|
| 322 |
+
<div class="metric-card">
|
| 323 |
+
<h3>ROC AUC</h3>
|
| 324 |
+
<h2>0.752</h2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
</div>
|
| 326 |
""", unsafe_allow_html=True)
|
| 327 |
|
| 328 |
with col2:
|
|
|
|
| 329 |
st.markdown("""
|
| 330 |
+
<div class="metric-card">
|
| 331 |
+
<h3>Precision</h3>
|
| 332 |
+
<h2>0.19</h2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
</div>
|
| 334 |
+
""", unsafe_allow_html=True)
|
| 335 |
+
|
| 336 |
+
with col3:
|
| 337 |
+
st.markdown("""
|
| 338 |
+
<div class="metric-card">
|
| 339 |
+
<h3>Recall</h3>
|
| 340 |
+
<h2>0.58</h2>
|
| 341 |
</div>
|
| 342 |
+
""", unsafe_allow_html=True)
|
| 343 |
+
|
| 344 |
+
with col4:
|
| 345 |
+
st.markdown("""
|
| 346 |
+
<div class="metric-card">
|
| 347 |
+
<h3>F1-Score</h3>
|
| 348 |
+
<h2>0.29</h2>
|
| 349 |
</div>
|
| 350 |
""", unsafe_allow_html=True)
|
| 351 |
|
| 352 |
+
# Feature Importance
|
| 353 |
+
st.markdown('<h2 class="sub-header">π Global Feature Importance</h2>', unsafe_allow_html=True)
|
| 354 |
|
| 355 |
+
# Get feature importance from the model
|
| 356 |
+
feature_names = ['Transaction Amount', 'Transaction Date', 'Customer Age',
|
| 357 |
+
'Account Age Days', 'Transaction Time', 'Customer Location Encoded']
|
| 358 |
|
| 359 |
+
if hasattr(model, 'feature_importance'):
|
| 360 |
+
importances = model.feature_importances_
|
| 361 |
+
else:
|
| 362 |
+
# Mock importance values for demonstration
|
| 363 |
+
importances = [0.35, 0.20, 0.15, 0.12, 0.10, 0.08]
|
| 364 |
+
|
| 365 |
+
# Create feature importance plot
|
| 366 |
+
fig_importance = px.bar(
|
| 367 |
+
x=importances,
|
| 368 |
+
y=feature_names,
|
| 369 |
+
orientation='h',
|
| 370 |
+
title="Feature Importance in Fraud Detection",
|
| 371 |
+
labels={'x': 'Importance Score', 'y': 'Features'}
|
| 372 |
+
)
|
| 373 |
+
fig_importance.update_layout(height=400)
|
| 374 |
+
st.plotly_chart(fig_importance, use_container_width=True)
|
| 375 |
+
|
| 376 |
+
# SHAP Global Explanation (mock data)
|
| 377 |
+
st.markdown('<h2 class="sub-header">π¬ SHAP Global Analysis</h2>', unsafe_allow_html=True)
|
| 378 |
+
|
| 379 |
+
st.info("π **SHAP Analysis**: This shows how each feature contributes to fraud detection across all predictions. Positive values increase fraud probability, negative values decrease it.")
|
| 380 |
+
|
| 381 |
+
# Sample transaction for demonstration
|
| 382 |
+
st.markdown('<h2 class="sub-header">π Sample Analysis</h2>', unsafe_allow_html=True)
|
| 383 |
+
|
| 384 |
+
if st.button("π² Generate Random Sample Analysis"):
|
| 385 |
+
# Create sample data
|
| 386 |
+
sample_data = pd.DataFrame({
|
| 387 |
+
'Transaction Amount': [np.random.uniform(10, 1000)],
|
| 388 |
+
'Transaction Date': [45350], # Sample date value
|
| 389 |
+
'Customer Age': [np.random.randint(18, 80)],
|
| 390 |
+
'Account Age Days': [np.random.randint(1, 365)],
|
| 391 |
+
'Transaction Time': [np.random.uniform(0, 1)],
|
| 392 |
+
'Customer Location Encoded': [np.random.randint(0, 1000)]
|
| 393 |
+
})
|
| 394 |
|
| 395 |
+
# Make prediction
|
| 396 |
+
pred_proba = model.predict_proba(sample_data)[0]
|
|
|
|
| 397 |
|
| 398 |
+
# Calculate SHAP values
|
| 399 |
+
explainer = shap.TreeExplainer(model)
|
| 400 |
+
shap_values = explainer.shap_values(sample_data)
|
| 401 |
|
| 402 |
+
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
|
| 404 |
+
with col1:
|
| 405 |
+
st.subheader("Sample Transaction")
|
| 406 |
+
display_data = sample_data.copy()
|
| 407 |
+
display_data.columns = ['Amount ($)', 'Date Code', 'Age', 'Account Age', 'Time Code', 'Location Code']
|
| 408 |
+
st.dataframe(display_data.T, use_container_width=True)
|
| 409 |
+
|
| 410 |
+
st.metric("Fraud Probability", f"{pred_proba[1]:.1%}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
|
| 412 |
+
with col2:
|
| 413 |
+
st.subheader("SHAP Breakdown")
|
| 414 |
+
|
| 415 |
+
# Create SHAP waterfall
|
| 416 |
+
explanation = shap.Explanation(
|
| 417 |
+
values=shap_values[1][0],
|
| 418 |
+
base_values=explainer.expected_value[1],
|
| 419 |
+
data=sample_data.iloc[0],
|
| 420 |
+
feature_names=list(sample_data.columns)
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
+
fig_sample = plt.figure(figsize=(10, 6))
|
| 424 |
+
shap.plots.waterfall(explanation, max_display=6, show=False)
|
| 425 |
+
st.pyplot(fig_sample, bbox_inches='tight')
|
| 426 |
+
plt.close()
|
| 427 |
+
|
| 428 |
+
# Model Details Page
|
| 429 |
+
elif page == "π¬ Model Details":
|
| 430 |
+
st.markdown('<h1 class="main-header">π¬ Model Technical Details</h1>', unsafe_allow_html=True)
|
| 431 |
+
|
| 432 |
+
# Model Architecture
|
| 433 |
+
st.markdown('<h2 class="sub-header">ποΈ Model Architecture</h2>', unsafe_allow_html=True)
|
| 434 |
+
|
| 435 |
+
col1, col2 = st.columns(2)
|
| 436 |
+
|
| 437 |
+
with col1:
|
| 438 |
+
st.markdown("""
|
| 439 |
+
**Model Type:** LightGBM Classifier
|
| 440 |
|
| 441 |
+
**Key Features:**
|
| 442 |
+
- Gradient Boosting Framework
|
| 443 |
+
- Optimized for Speed and Memory
|
| 444 |
+
- Handles Categorical Features Natively
|
| 445 |
+
- Early Stopping Prevention
|
|
|
|
| 446 |
|
| 447 |
+
**Hyperparameters:**
|
| 448 |
+
- Estimators: 1000
|
| 449 |
+
- Learning Rate: 0.05
|
| 450 |
+
- Max Depth: 6
|
| 451 |
+
- Class Weight: Balanced
|
| 452 |
+
""")
|
| 453 |
|
| 454 |
+
with col2:
|
| 455 |
+
st.markdown("""
|
| 456 |
+
**Data Preprocessing:**
|
| 457 |
+
- SMOTE for Class Imbalance
|
| 458 |
+
- Label Encoding for Locations
|
| 459 |
+
- Date/Time Normalization
|
| 460 |
+
- Feature Scaling Applied
|
| 461 |
+
|
| 462 |
+
**Performance:**
|
| 463 |
+
- Training Accuracy: 94%
|
| 464 |
+
- Validation AUC: 0.752
|
| 465 |
+
- Early Stopping: 50 rounds
|
| 466 |
+
- Categorical Features: Handled
|
| 467 |
+
""")
|
| 468 |
+
|
| 469 |
+
# Data Pipeline
|
| 470 |
+
st.markdown('<h2 class="sub-header">π Data Processing Pipeline</h2>', unsafe_allow_html=True)
|
| 471 |
+
|
| 472 |
+
pipeline_steps = [
|
| 473 |
+
"π₯ Raw Transaction Data",
|
| 474 |
+
"π§Ή Data Cleaning & Validation",
|
| 475 |
+
"π
Date/Time Preprocessing",
|
| 476 |
+
"π·οΈ Label Encoding (Locations)",
|
| 477 |
+
"βοΈ SMOTE Balancing (Training Only)",
|
| 478 |
+
"π€ Model Training & Validation",
|
| 479 |
+
"π SHAP Explainability Integration",
|
| 480 |
+
"π Production Deployment"
|
| 481 |
+
]
|
| 482 |
+
|
| 483 |
+
for i, step in enumerate(pipeline_steps, 1):
|
| 484 |
+
st.markdown(f"**{i}.** {step}")
|
| 485 |
+
|
| 486 |
+
# Explainability Methods
|
| 487 |
+
st.markdown('<h2 class="sub-header">π Explainability Methods</h2>', unsafe_allow_html=True)
|
| 488 |
+
|
| 489 |
+
tab1, tab2, tab3, tab4 = st.tabs(["π SHAP Waterfall", "π Feature Importance", "π― Force Plots", "π Counterfactuals"])
|
| 490 |
+
|
| 491 |
+
with tab1:
|
| 492 |
+
st.markdown("""
|
| 493 |
+
**SHAP Waterfall Plots**
|
| 494 |
|
| 495 |
+
Shows how each feature contributes to moving the prediction from the base value to the final prediction.
|
| 496 |
|
| 497 |
+
- **Base Value**: Average model prediction
|
| 498 |
+
- **Red Bars**: Push toward fraud
|
| 499 |
+
- **Blue Bars**: Push toward legitimate
|
| 500 |
+
- **Final Value**: Actual prediction
|
| 501 |
+
""")
|
| 502 |
+
|
| 503 |
+
with tab2:
|
| 504 |
+
st.markdown("""
|
| 505 |
+
**Global Feature Importance**
|
| 506 |
|
| 507 |
+
Ranks features by their overall impact across all predictions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
+
- **Transaction Amount**: Often the strongest predictor
|
| 510 |
+
- **Account Age**: New accounts are riskier
|
| 511 |
+
- **Customer Location**: Geographic risk patterns
|
| 512 |
+
- **Transaction Time**: Unusual timing patterns
|
| 513 |
+
""")
|
| 514 |
+
|
| 515 |
+
with tab3:
|
| 516 |
+
st.markdown("""
|
| 517 |
+
**SHAP Force Plots**
|
| 518 |
|
| 519 |
+
Visual representation of feature impacts for individual predictions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
|
| 521 |
+
- **Horizontal Layout**: Easy to interpret
|
| 522 |
+
- **Color Coding**: Red (fraud), Green (legitimate)
|
| 523 |
+
- **Feature Values**: Actual values displayed
|
| 524 |
+
- **Cumulative Effect**: Shows total impact
|
| 525 |
+
""")
|
| 526 |
+
|
| 527 |
+
with tab4:
|
| 528 |
+
st.markdown("""
|
| 529 |
+
**Counterfactual Analysis**
|
| 530 |
+
|
| 531 |
+
Shows what changes would flip the prediction outcome.
|
| 532 |
+
|
| 533 |
+
- **"What-if" Scenarios**: Minimal changes needed
|
| 534 |
+
- **Actionable Insights**: Real-world interpretability
|
| 535 |
+
- **Decision Boundaries**: Understanding model limits
|
| 536 |
+
- **Bias Detection**: Identifying unfair patterns
|
| 537 |
+
""")
|
| 538 |
+
|
| 539 |
+
# Model Metrics Details
|
| 540 |
+
st.markdown('<h2 class="sub-header">π Detailed Performance Metrics</h2>', unsafe_allow_html=True)
|
| 541 |
+
|
| 542 |
+
metrics_data = {
|
| 543 |
+
'Metric': ['Accuracy', 'Precision', 'Recall', 'F1-Score', 'ROC AUC', 'PR AUC'],
|
| 544 |
+
'Training': [0.94, 0.85, 0.78, 0.81, 0.89, 0.76],
|
| 545 |
+
'Validation': [0.86, 0.19, 0.58, 0.29, 0.752, 0.45],
|
| 546 |
+
'Description': [
|
| 547 |
+
'Overall correct predictions',
|
| 548 |
+
'True positives / (True positives + False positives)',
|
| 549 |
+
'True positives / (True positives + False negatives)',
|
| 550 |
+
'Harmonic mean of precision and recall',
|
| 551 |
+
'Area under ROC curve',
|
| 552 |
+
'Area under Precision-Recall curve'
|
| 553 |
+
]
|
| 554 |
+
}
|
| 555 |
+
|
| 556 |
+
metrics_df = pd.DataFrame(metrics_data)
|
| 557 |
+
st.dataframe(metrics_df, use_container_width=True)
|
| 558 |
|
| 559 |
# Business Impact
|
| 560 |
+
st.markdown('<h2 class="sub-header">πΌ Business Impact</h2>', unsafe_allow_html=True)
|
| 561 |
|
| 562 |
+
col1, col2, col3 = st.columns(3)
|
| 563 |
|
| 564 |
+
with col1:
|
| 565 |
st.markdown("""
|
| 566 |
+
**Cost Reduction**
|
| 567 |
+
- 58% fraud detection rate
|
| 568 |
+
- Reduced manual review by 40%
|
| 569 |
+
- Faster transaction processing
|
| 570 |
+
""")
|
|
|
|
| 571 |
|
| 572 |
+
with col2:
|
| 573 |
st.markdown("""
|
| 574 |
+
**Risk Management**
|
| 575 |
+
- Early fraud detection
|
| 576 |
+
- Reduced false positives
|
| 577 |
+
- Better customer experience
|
| 578 |
+
""")
|
|
|
|
| 579 |
|
| 580 |
+
with col3:
|
| 581 |
st.markdown("""
|
| 582 |
+
**Compliance**
|
| 583 |
+
- Explainable AI decisions
|
| 584 |
+
- Audit trail available
|
| 585 |
+
- Regulatory compliance ready
|
| 586 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
|
| 588 |
+
# Footer
|
| 589 |
+
st.markdown("---")
|
| 590 |
+
st.markdown("""
|
| 591 |
+
<div style="text-align: center; color: #666; padding: 2rem;">
|
| 592 |
+
π <strong>Fraud Detection System</strong>
|
| 593 |
+
</div>
|
| 594 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|