-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathquickstart.py
More file actions
109 lines (86 loc) · 2.97 KB
/
Copy pathquickstart.py
File metadata and controls
109 lines (86 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Imports
# -----------------------------------------------------------
from scipy.sparse import data
import streamlit as st
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()
# -----------------------------------------------------------
# Helper functions
# -----------------------------------------------------------
# Load data from external source
@st.cache
def load_data():
df = pd.read_csv(
"https://raw.githubusercontent.com/ThuwarakeshM/PracticalML-KMeans-Election/master/voters_demo_sample.csv"
)
return df
df = load_data()
def run_kmeans(df, n_clusters=2):
kmeans = KMeans(n_clusters, random_state=0).fit(df[["Age", "Income"]])
fig, ax = plt.subplots(figsize=(16, 9))
ax.grid(False)
ax.set_facecolor("#FFF")
ax.spines[["left", "bottom"]].set_visible(True)
ax.spines[["left", "bottom"]].set_color("#4a4a4a")
ax.tick_params(labelcolor="#4a4a4a")
ax.yaxis.label.set(color="#4a4a4a", fontsize=20)
ax.xaxis.label.set(color="#4a4a4a", fontsize=20)
# --------------------------------------------------
# Create scatterplot
ax = sns.scatterplot(
ax=ax,
x=df.Age,
y=df.Income,
hue=kmeans.labels_,
palette=sns.color_palette("colorblind", n_colors=n_clusters),
legend=None,
)
# Annotate cluster centroids
for ix, [age, income] in enumerate(kmeans.cluster_centers_):
ax.scatter(age, income, s=200, c="#a8323e")
ax.annotate(
f"Cluster #{ix+1}",
(age, income),
fontsize=25,
color="#a8323e",
xytext=(age + 5, income + 3),
bbox=dict(boxstyle="round,pad=0.3", fc="white", ec="#a8323e", lw=2),
ha="center",
va="center",
)
return fig
# -----------------------------------------------------------
# SIDEBAR
# -----------------------------------------------------------
sidebar = st.sidebar
df_display = sidebar.checkbox("Display Raw Data", value=True)
n_clusters = sidebar.slider(
"Select Number of Clusters",
min_value=2,
max_value=10,
)
sidebar.write(
"""
Hey friend!It seems we have lots of common interests.
I'd love to connect with you on
- [LinkedIn](https://linkedin.com/in/thuwarakesh/)
- [Twitter](https://www.twitter.com/thuwarakesh/)
And please follow me on [Medium](https://thuwarakesh.medium.com/), because I write about data science.
"""
)
# -----------------------------------------------------------
# Main
# -----------------------------------------------------------
# Create a title for your app
st.title("Interactive K-Means Clustering")
"""
An illustration by [Thuwarakesh Murallie](https://thuwarakesh.medium.com) for the Streamlit introduction article on Medium.
"""
# Show cluster scatter plot
st.write(run_kmeans(df, n_clusters=n_clusters))
if df_display:
st.write(df)
# -----------------------------------------------------------