-
Notifications
You must be signed in to change notification settings - Fork 7
/
7_iris_app.py
181 lines (163 loc) · 5.59 KB
/
7_iris_app.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""=============================================================================
Filename: iris_app.py
Last updated: 2024-04-21
This application allows the user to run k-means clustering on the iris dataset.
============================================================================="""
"""=====================================
Imports
====================================="""
from dash import Dash, html, dash_table, dcc, callback, Output, Input, State
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash_bootstrap_components as dbc
from sklearn.cluster import KMeans
"""=====================================
Code execution
====================================="""
# Connect to the dataset.
iris_data = px.data.iris()
iris_data.rename(
columns={
"sepal_length": "Sepal length (cm)",
"sepal_width": "Sepal width (cm)",
"petal_length": "Petal length (cm)",
"petal_width": "Petal width (cm)",
"species": "Species",
"species_id": "Species ID",
},
inplace=True,
)
# We don't want species and species ID to be used for the machine learning.
iris_cols = [
"Sepal length (cm)",
"Sepal width (cm)",
"Petal length (cm)",
"Petal width (cm)",
]
# Initialize the app. Use a Dash Bootstrap theme for styling.
external_stylesheets = [dbc.themes.CERULEAN]
app = Dash(__name__, external_stylesheets=external_stylesheets)
app.title = "Iris Clustering"
# Define the app layout using DBC.
app.layout = dbc.Container(
[
# Page header
dbc.Row(
[html.Div("Iris Clustering", className="text-primary text-center fs-3")]
),
# Control items
dbc.Row(
[
dbc.Col(
[
dbc.Label("x-axis"),
dcc.Dropdown(
options=[{"label": col, "value": col} for col in iris_cols],
value="Sepal length (cm)",
id="x-dropdown",
),
],
width=4,
),
dbc.Col(
[
dbc.Label("y-axis"),
dcc.Dropdown(
options=[{"label": col, "value": col} for col in iris_cols],
value="Sepal width (cm)",
id="y-dropdown",
),
],
width=4,
),
dbc.Col(
[
dbc.Label("Number of clusters"),
dbc.Input(id="cluster-input", type="number", value=3),
],
width=3,
),
dbc.Col([dbc.Button("Apply", id="apply-button")], width=1, align="end"),
]
),
# Scatter plot
dbc.Row(
[
dbc.Col([dcc.Graph(figure={}, id="my-graph")], width=12),
]
),
# Table
dbc.Row(
[
dbc.Col(
[
dash_table.DataTable(
id="my-table",
page_size=10,
style_table={"overflowX": "auto"},
)
],
width=12,
),
]
),
],
fluid=True,
) # The fluid option allows the app to fill horizontal space and resize.
"""=====================================
Callback definitions
====================================="""
@app.callback(
Output("my-graph", "figure"),
Output("my-table", "data"),
[
State("x-dropdown", "value"),
State("y-dropdown", "value"),
State("cluster-input", "value"),
Input("apply-button", "n_clicks"),
],
)
def run_clustering(x_var, y_var, num_clusters, n_clicks):
"""
Runs k-means clustering and updates the graph and table based on the user's
selections. The user selects the x- and y-variables and chooses the number
of clusters for the algorithm. When the user clicks the button, the model
will be run.
Arguments:
x_var: The column of the iris dataset to be displayed on the x-axis.
y_var: The column of the iris dataset to be displayed on the y-axis.
num_clusters: The number of clusters to be used for k-means clustering.
n_clicks: Not used in the function. The model is run on button click.
"""
# Make sure there's at least one cluster.
num_clusters = max(num_clusters, 1)
# Make a copy of the iris data for our k-means clustering.
df = iris_data.copy(deep=True)
# Perform the k-means clustering and save the values.
k_means = KMeans(n_clusters=num_clusters)
k_means.fit(df[[x_var, y_var]].values)
df["Cluster"] = k_means.labels_.astype(str)
# Create the scatter plot.
cluster_ids_sorted = [str(x) for x in list(range(num_clusters))]
fig = px.scatter(
df,
x=x_var,
y=y_var,
color="Cluster",
category_orders={"Cluster": cluster_ids_sorted},
)
# Add the centroids to the scatter plot.
centroid_df = pd.DataFrame(k_means.cluster_centers_, columns=[x_var, y_var])
centroid_fig = go.Scatter(
x=centroid_df[x_var],
y=centroid_df[y_var],
mode="markers",
marker={"color": "black", "size": 16, "symbol": "star-triangle-up"},
name="Centroids",
)
fig.add_trace(centroid_fig)
return fig, df.to_dict("records")
# Run the application.
if __name__ == "__main__":
app.run_server(debug=True)