Spaces:
Sleeping
Sleeping
Download app.py from abizaregi7/customer-segmentation: direct link, hf CLI and curl.
- Browser
- Download file 4.66 kB
-
https://huggingface.co/spaces/abizaregi7/customer-segmentation/resolve/main/app.py
- Command line
-
hf download hf://spaces/abizaregi7/customer-segmentation/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abizaregi7/customer-segmentation/resolve/main/app.py
4.66 kB
| import streamlit as st | |
| import pandas as pd | |
| import plotly.express as px | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.cluster import KMeans | |
| # Konfigurasi Halaman | |
| st.set_page_config(page_title='Customer Segmentation Dashboard', layout='wide') | |
| st.title('ποΈ Customer Segmentation Dashboard') | |
| # Upload File | |
| uploaded_file = st.file_uploader("Upload dataset (CSV)", type=['csv']) | |
| def load_data(file): | |
| return pd.read_csv(file) | |
| if uploaded_file is not None: | |
| df = load_data(uploaded_file) | |
| st.success("Dataset berhasil diunggah!") | |
| # Sidebar Navigasi | |
| page = st.sidebar.radio("Navigasi", ["Visualisasi Data", "Segmentasi Pelanggan", "Ekspor Data"]) | |
| if page == "Visualisasi Data": | |
| st.subheader("π Visualisasi Data") | |
| numeric_columns = df.select_dtypes(include=['number']).columns.tolist() | |
| if numeric_columns: | |
| x_axis = st.selectbox("Pilih sumbu X", options=numeric_columns) | |
| y_axis = st.selectbox("Pilih sumbu Y", options=numeric_columns) | |
| chart_type = st.selectbox("Pilih jenis chart", ["Scatter Plot", "Bar Chart", "Histogram"]) | |
| if chart_type == "Scatter Plot": | |
| fig = px.scatter(df, x=x_axis, y=y_axis, title=f"{chart_type} antara {x_axis} dan {y_axis}") | |
| elif chart_type == "Bar Chart": | |
| fig = px.bar(df, x=x_axis, y=y_axis, title=f"{chart_type} antara {x_axis} dan {y_axis}") | |
| elif chart_type == "Histogram": | |
| fig = px.histogram(df, x=x_axis, title=f"{chart_type} dari {x_axis}") | |
| st.plotly_chart(fig, use_container_width=True) | |
| else: | |
| st.warning("Dataset tidak memiliki kolom numerik untuk divisualisasikan.") | |
| elif page == "Segmentasi Pelanggan": | |
| st.subheader("π Segmentasi Pelanggan dengan K-Means Clustering") | |
| # Pilih fitur untuk clustering | |
| selected_features = st.multiselect("Pilih fitur untuk segmentasi", options=df.select_dtypes(include=['number']).columns.tolist()) | |
| if selected_features: | |
| X = df[selected_features] | |
| X_scaled = StandardScaler().fit_transform(X) | |
| # Tentukan jumlah cluster | |
| n_clusters = st.slider("Pilih jumlah cluster", min_value=2, max_value=10, value=3) | |
| # K-Means Clustering | |
| kmeans = KMeans(n_clusters=n_clusters, random_state=42) | |
| df['Cluster'] = kmeans.fit_predict(X_scaled) | |
| # Pilih dua fitur asli untuk visualisasi cluster | |
| st.subheader("π Pilih Fitur untuk Visualisasi Cluster") | |
| x_feature = st.selectbox("Pilih fitur untuk sumbu X", options=selected_features) | |
| y_feature = st.selectbox("Pilih fitur untuk sumbu Y", options=selected_features) | |
| # Visualisasi Cluster dengan fitur asli | |
| fig = px.scatter(df, x=x_feature, y=y_feature, color=df['Cluster'].astype(str), | |
| title=f"Visualisasi Cluster berdasarkan {x_feature} dan {y_feature}") | |
| st.plotly_chart(fig, use_container_width=True) | |
| # Statistik Ringkasan per Cluster | |
| st.subheader("π Statistik Ringkasan per Cluster") | |
| summary = df.groupby("Cluster")[selected_features].mean() | |
| st.dataframe(summary) | |
| # Kesimpulan Segmentasi Pelanggan | |
| st.subheader("π Kesimpulan Segmentasi Berdasarkan Data") | |
| st.write("Dari hasil clustering, kita dapat mengidentifikasi perbedaan karakteristik pelanggan di setiap segmen:") | |
| for cluster in summary.index: | |
| cluster_data = summary.loc[cluster] | |
| st.write(f"- **Cluster {cluster}** memiliki rata-rata nilai {', '.join([f'{col}: {cluster_data[col]:.2f}' for col in selected_features])}.") | |
| st.write("Kesimpulan ini dapat membantu dalam menentukan strategi pemasaran yang lebih tepat sasaran.") | |
| else: | |
| st.warning("Silakan pilih fitur untuk segmentasi pelanggan.") | |
| elif page == "Ekspor Data": | |
| st.subheader("π₯ Unduh Hasil Segmentasi") | |
| st.download_button("Download Data CSV", df.to_csv(index=False).encode("utf-8"), "segmented_data.csv", "text/csv") | |
| else: | |
| st.info("Silakan unggah file CSV untuk mulai analisis segmentasi pelanggan.") | |
| # Tambahkan file requirements.txt untuk deploy di Hugging Face | |
| with open("requirements.txt", "w") as f: | |
| f.write("streamlit\npandas\nplotly\nseaborn\nscikit-learn\nmatplotlib") |