ahmedheikal/AppliedMachineLearningProject
0
1import streamlit as st2import pandas as pd3import matplotlib.pyplot as plt4import seaborn as sns5import joblib6from pathlib import Path7 8st.set_page_config(9 page_title="Credit Card Customer Segmentation",10 layout="wide"11)12 13BASE = Path(".")14 15# =========================16# LOAD FILES17# =========================18@st.cache_data19def load_data():20 files = {21 "metrics": "evaluation_metrics.csv",22 "insights": "business_insights.csv",23 "processed": "data_scaled_with_kmeans.csv",24 "segments": "final_customer_segments.csv",25 "dbscan": "dbscan_labels.csv",26 "gmm_probs": "gmm_probabilities.csv"27 }28 29 data = {}30 31 for key, file in files.items():32 try:33 data[key] = pd.read_csv(BASE / file)34 except:35 data[key] = None36 37 return data38 39 40@st.cache_resource41def load_models():42 models = {}43 44 try:45 models["kmeans"] = joblib.load(BASE / "kmeans_model.pkl")46 except:47 models["kmeans"] = None48 49 try:50 models["gmm"] = joblib.load(BASE / "gmm_model.pkl")51 except:52 models["gmm"] = None53 54 try:55 models["scaler"] = joblib.load(BASE / "scaler.pkl")56 except:57 models["scaler"] = None58 59 try:60 models["pca"] = joblib.load(BASE / "pca_model.pkl")61 except:62 models["pca"] = None63 64 return models65 66 67data = load_data()68models = load_models()69 70# =========================71# HEADER72# =========================73st.title("Credit Card Customer Segmentation Dashboard")74st.markdown(75 """76Applied Machine Learning Project using:77 78- KMeans Clustering79- DBSCAN80- Gaussian Mixture Model (GMM)81- Deep Clustering using DEC82"""83)84 85# =========================86# TOP METRICS87# =========================88col1, col2, col3, col4 = st.columns(4)89 90with col1:91 st.metric("Models Used", "4")92 93with col2:94 if data["processed"] is not None:95 st.metric("Customers", len(data["processed"]))96 else:97 st.metric("Customers", "N/A")98 99with col3:100 st.metric("Best Deep Model", "DEC")101 102with col4:103 if data["metrics"] is not None:104 best_score = data["metrics"]["Silhouette"].max()105 st.metric("Best Silhouette", f"{best_score:.3f}")106 else:107 st.metric("Best Silhouette", "N/A")108 109st.divider()110 111# =========================112# PROJECT OVERVIEW113# =========================114left, right = st.columns([2, 1])115 116with left:117 st.subheader("Project Methodology")118 119 methodology = pd.DataFrame({120 "Stage": [121 "Preprocessing",122 "Feature Engineering",123 "Dimensionality Reduction",124 "Traditional Clustering",125 "Probabilistic Clustering",126 "Deep Clustering"127 ],128 "Description": [129 "Missing values, scaling, log transformation",130 "Behavioral financial feature creation",131 "PCA compression for efficient clustering",132 "KMeans and DBSCAN clustering",133 "Gaussian Mixture customer segmentation",134 "Autoencoder + DEC latent clustering"135 ]136 })137 138 st.dataframe(methodology, use_container_width=True)139 140with right:141 st.subheader("Dataset Overview")142 143 if data["processed"] is not None:144 st.write("Dataset Shape:")145 st.write(data["processed"].shape)146 147 st.write("Preview:")148 st.dataframe(data["processed"].head(), use_container_width=True)149 else:150 st.warning("Dataset file not found")151 152st.divider()153 154# =========================155# MODEL COMPARISON156# =========================157st.subheader("Model Performance Comparison")158 159if data["metrics"] is not None:160 st.dataframe(data["metrics"], use_container_width=True)161 162 fig = plt.figure(figsize=(10, 5))163 sns.barplot(164 data=data["metrics"],165 x="Model",166 y="Silhouette"167 )168 plt.xticks(rotation=30)169 plt.title("Silhouette Score Comparison")170 plt.tight_layout()171 st.pyplot(fig)172 173else:174 st.warning("evaluation_metrics.csv not found")175 176st.divider()177 178# =========================179# DBSCAN ANALYSIS180# =========================181col1, col2 = st.columns(2)182 183with col1:184 st.subheader("DBSCAN Analysis")185 186 if data["dbscan"] is not None:187 total = len(data["dbscan"])188 noise = (data["dbscan"]["DBSCAN_Cluster"] == -1).sum()189 clusters = data["dbscan"]["DBSCAN_Cluster"].nunique() - 1190 191 st.metric("Detected Clusters", clusters)192 st.metric("Noise Points", noise)193 st.metric("Noise Percentage", f"{(noise / total) * 100:.2f}%")194 195 st.info(196 "DBSCAN was useful for anomaly detection, but struggled due to overlapping density structures in customer behavior."197 )198 else:199 st.warning("dbscan_labels.csv not found")200 201with col2:202 st.subheader("GMM Confidence")203 204 if data["gmm_probs"] is not None:205 fig = plt.figure(figsize=(8, 4))206 207 numeric_probs = data["gmm_probs"].select_dtypes(include=["number"])208 209 sns.histplot(210 numeric_probs.max(axis=1),211 bins=30212 )213 214 plt.title("GMM Assignment Confidence")215 plt.xlabel("Maximum Cluster Probability")216 plt.tight_layout()217 218 st.pyplot(fig)219 220 else:221 st.info("gmm_probabilities.csv not found")222 223st.divider()224 225# =========================226# BUSINESS INSIGHTS227# =========================228st.subheader("Business Customer Segmentation Insights")229 230if data["insights"] is not None:231 st.dataframe(data["insights"], use_container_width=True)232else:233 st.warning("business_insights.csv not found")234 235st.divider()236 237# =========================238# FINAL DEC SEGMENTS239# =========================240st.subheader("Final Deep Clustering Segmentation (DEC)")241 242if data["segments"] is not None:243 st.dataframe(data["segments"].head(20), use_container_width=True)244 245 if "Customer_Type" in data["segments"].columns:246 counts = data["segments"]["Customer_Type"].value_counts()247 248 fig = plt.figure(figsize=(8, 5))249 counts.plot(kind="bar")250 plt.title("Customer Segment Distribution")251 plt.xticks(rotation=30)252 plt.tight_layout()253 st.pyplot(fig)254 255else:256 st.warning("final_customer_segments.csv not found")257 258st.divider()259 260# =========================261# FINAL RECOMMENDATION262# =========================263st.subheader("Final Recommendation")264 265recommendation = pd.DataFrame({266 "Category": [267 "Best Traditional Model",268 "Best Density Model",269 "Best Probabilistic Model",270 "Best Deep Learning Model",271 "Recommended Final Approach"272 ],273 "Model": [274 "KMeans",275 "DBSCAN",276 "GMM",277 "DEC",278 "DEC"279 ]280})281 282st.dataframe(recommendation, use_container_width=True)283 284st.success(285 """286Deep Clustering using DEC achieved the strongest customer separation performance.287This makes it the most effective segmentation approach for this project.288"""289)