Commit ·
609cbbd
1
Parent(s): 218d38e
- app.py +93 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.cluster import KMeans
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# App Title
|
| 8 |
+
st.title("Unsupervised Learning: K-Means Clustering")
|
| 9 |
+
|
| 10 |
+
# Sidebar Section: Tab for downloading a sample dataset
|
| 11 |
+
st.sidebar.subheader("Sample Dataset")
|
| 12 |
+
st.sidebar.write("Download a sample dataset to test the app. This sample contains two numerical features for demonstration purposes.")
|
| 13 |
+
sample_data = {
|
| 14 |
+
"Feature1": [1.0, 1.5, 3.0, 5.0, 3.5, 4.5, 3.5],
|
| 15 |
+
"Feature2": [1.0, 2.0, 4.0, 7.0, 5.0, 5.0, 4.5],
|
| 16 |
+
}
|
| 17 |
+
sample_df = pd.DataFrame(sample_data)
|
| 18 |
+
sample_csv = sample_df.to_csv(index=False)
|
| 19 |
+
st.sidebar.download_button(
|
| 20 |
+
label="Download Sample CSV",
|
| 21 |
+
data=sample_csv,
|
| 22 |
+
file_name="sample_data.csv",
|
| 23 |
+
mime="text/csv"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Main Section: Upload dataset
|
| 27 |
+
st.header("Step 1: Upload Your Dataset")
|
| 28 |
+
st.write("Upload a CSV file containing your data. Ensure it includes numerical features for clustering.")
|
| 29 |
+
uploaded_file = st.file_uploader("Upload your dataset (CSV format)", type="csv")
|
| 30 |
+
if uploaded_file:
|
| 31 |
+
data = pd.read_csv(uploaded_file)
|
| 32 |
+
st.write("Preview of the uploaded data:")
|
| 33 |
+
st.dataframe(data)
|
| 34 |
+
|
| 35 |
+
# Step 2: Select features for clustering
|
| 36 |
+
st.subheader("Step 2: Feature Selection")
|
| 37 |
+
st.write("Select the numerical features you want to use for clustering.")
|
| 38 |
+
selected_features = st.multiselect(
|
| 39 |
+
"Select features for clustering:", data.columns.tolist()
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if selected_features:
|
| 43 |
+
X = data[selected_features]
|
| 44 |
+
|
| 45 |
+
# Step 3: Configure clustering parameters
|
| 46 |
+
st.subheader("Step 3: Clustering Configuration")
|
| 47 |
+
st.write("Choose the number of clusters you want to create using the slider below.")
|
| 48 |
+
n_clusters = st.slider("Select the number of clusters:", min_value=2, max_value=10, value=3)
|
| 49 |
+
|
| 50 |
+
# Apply K-Means Clustering
|
| 51 |
+
model = KMeans(n_clusters=n_clusters, random_state=42)
|
| 52 |
+
cluster_labels = model.fit_predict(X)
|
| 53 |
+
|
| 54 |
+
# Step 4: Add cluster labels to the dataset
|
| 55 |
+
data['Cluster'] = cluster_labels
|
| 56 |
+
st.write("Clustered Data:")
|
| 57 |
+
st.dataframe(data)
|
| 58 |
+
|
| 59 |
+
# Step 5: Visualize the clusters
|
| 60 |
+
st.subheader("Step 5: Cluster Visualization")
|
| 61 |
+
st.write("Visualize the clustering results. Select at least two features for plotting.")
|
| 62 |
+
if len(selected_features) >= 2:
|
| 63 |
+
fig, ax = plt.subplots()
|
| 64 |
+
scatter = ax.scatter(
|
| 65 |
+
X[selected_features[0]],
|
| 66 |
+
X[selected_features[1]],
|
| 67 |
+
c=cluster_labels,
|
| 68 |
+
cmap="viridis",
|
| 69 |
+
s=50
|
| 70 |
+
)
|
| 71 |
+
ax.set_xlabel(selected_features[0])
|
| 72 |
+
ax.set_ylabel(selected_features[1])
|
| 73 |
+
ax.set_title("K-Means Clustering")
|
| 74 |
+
legend = ax.legend(*scatter.legend_elements(), title="Clusters")
|
| 75 |
+
ax.add_artist(legend)
|
| 76 |
+
st.pyplot(fig)
|
| 77 |
+
else:
|
| 78 |
+
st.warning("Select at least 2 features for visualization.")
|
| 79 |
+
|
| 80 |
+
# Step 6: Download the clustered data
|
| 81 |
+
st.subheader("Step 6: Download Clustered Data")
|
| 82 |
+
st.write("Download the dataset with the cluster labels added.")
|
| 83 |
+
csv = data.to_csv(index=False)
|
| 84 |
+
st.download_button(
|
| 85 |
+
label="Download CSV",
|
| 86 |
+
data=csv,
|
| 87 |
+
file_name="clustered_data.csv",
|
| 88 |
+
mime="text/csv"
|
| 89 |
+
)
|
| 90 |
+
else:
|
| 91 |
+
st.warning("Please select features for clustering.")
|
| 92 |
+
else:
|
| 93 |
+
st.info("Awaiting file upload. Use the sample dataset in the sidebar if you don’t have a file.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
scikit-learn
|
| 4 |
+
matplotlib
|