Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
from sklearn.cluster import KMeans
|
| 8 |
+
from sklearn.decomposition import PCA
|
| 9 |
+
|
| 10 |
+
# Konfigurasi Halaman
|
| 11 |
+
st.set_page_config(page_title='Customer Segmentation Dashboard', layout='wide')
|
| 12 |
+
st.title('๐๏ธ Customer Segmentation Dashboard')
|
| 13 |
+
|
| 14 |
+
# Upload File
|
| 15 |
+
uploaded_file = st.file_uploader("Upload dataset (CSV)", type=['csv'])
|
| 16 |
+
|
| 17 |
+
@st.cache_data
|
| 18 |
+
def load_data(file):
|
| 19 |
+
return pd.read_csv(file)
|
| 20 |
+
|
| 21 |
+
if uploaded_file is not None:
|
| 22 |
+
df = load_data(uploaded_file)
|
| 23 |
+
st.success("Dataset berhasil diunggah!")
|
| 24 |
+
|
| 25 |
+
# Sidebar Navigasi
|
| 26 |
+
page = st.sidebar.radio("Navigasi", ["Visualisasi Data", "Segmentasi Pelanggan", "Ekspor Data"])
|
| 27 |
+
|
| 28 |
+
if page == "Visualisasi Data":
|
| 29 |
+
st.subheader("๐ Visualisasi Data")
|
| 30 |
+
numeric_columns = df.select_dtypes(include=['number']).columns.tolist()
|
| 31 |
+
|
| 32 |
+
if numeric_columns:
|
| 33 |
+
x_axis = st.selectbox("Pilih sumbu X", options=numeric_columns)
|
| 34 |
+
y_axis = st.selectbox("Pilih sumbu Y", options=numeric_columns)
|
| 35 |
+
chart_type = st.selectbox("Pilih jenis chart", ["Scatter Plot", "Bar Chart", "Histogram"])
|
| 36 |
+
|
| 37 |
+
if chart_type == "Scatter Plot":
|
| 38 |
+
fig = px.scatter(df, x=x_axis, y=y_axis, title=f"{chart_type} antara {x_axis} dan {y_axis}")
|
| 39 |
+
elif chart_type == "Bar Chart":
|
| 40 |
+
fig = px.bar(df, x=x_axis, y=y_axis, title=f"{chart_type} antara {x_axis} dan {y_axis}")
|
| 41 |
+
elif chart_type == "Histogram":
|
| 42 |
+
fig = px.histogram(df, x=x_axis, title=f"{chart_type} dari {x_axis}")
|
| 43 |
+
|
| 44 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 45 |
+
else:
|
| 46 |
+
st.warning("Dataset tidak memiliki kolom numerik untuk divisualisasikan.")
|
| 47 |
+
|
| 48 |
+
elif page == "Segmentasi Pelanggan":
|
| 49 |
+
st.subheader("๐ Segmentasi Pelanggan dengan K-Means Clustering")
|
| 50 |
+
|
| 51 |
+
# Pilih fitur untuk clustering
|
| 52 |
+
selected_features = st.multiselect("Pilih fitur untuk segmentasi", options=df.select_dtypes(include=['number']).columns.tolist())
|
| 53 |
+
|
| 54 |
+
if selected_features:
|
| 55 |
+
X = df[selected_features]
|
| 56 |
+
X_scaled = StandardScaler().fit_transform(X)
|
| 57 |
+
|
| 58 |
+
# Tentukan jumlah cluster
|
| 59 |
+
n_clusters = st.slider("Pilih jumlah cluster", min_value=2, max_value=10, value=3)
|
| 60 |
+
|
| 61 |
+
# K-Means Clustering
|
| 62 |
+
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
|
| 63 |
+
df['Cluster'] = kmeans.fit_predict(X_scaled)
|
| 64 |
+
|
| 65 |
+
# Reduksi Dimensi untuk Visualisasi
|
| 66 |
+
pca = PCA(n_components=2)
|
| 67 |
+
pca_result = pca.fit_transform(X_scaled)
|
| 68 |
+
df['PCA1'] = pca_result[:, 0]
|
| 69 |
+
df['PCA2'] = pca_result[:, 1]
|
| 70 |
+
|
| 71 |
+
# Visualisasi Cluster
|
| 72 |
+
fig = px.scatter(df, x='PCA1', y='PCA2', color=df['Cluster'].astype(str), title="Visualisasi Cluster")
|
| 73 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 74 |
+
else:
|
| 75 |
+
st.warning("Silakan pilih fitur untuk segmentasi pelanggan.")
|
| 76 |
+
|
| 77 |
+
elif page == "Ekspor Data":
|
| 78 |
+
st.subheader("๐ฅ Unduh Hasil Segmentasi")
|
| 79 |
+
st.download_button("Download Data CSV", df.to_csv(index=False).encode("utf-8"), "segmented_data.csv", "text/csv")
|
| 80 |
+
else:
|
| 81 |
+
st.info("Silakan unggah file CSV untuk mulai analisis segmentasi pelanggan.")
|
| 82 |
+
|
| 83 |
+
# Tambahkan file requirements.txt untuk deploy di Hugging Face
|
| 84 |
+
with open("requirements.txt", "w") as f:
|
| 85 |
+
f.write("streamlit\npandas\nplotly\nseaborn\nsklearn\nmatplotlib")
|