Jensen-holm commited on
Commit
5e9f4d3
·
1 Parent(s): b401388

kmeans with example plot done and ready for deployment

Browse files
Files changed (2) hide show
  1. example/kmeans.py +33 -3
  2. example/kmedoids.py +0 -0
example/kmeans.py CHANGED
@@ -1,5 +1,9 @@
1
- import requests
2
  import json
 
 
 
 
3
 
4
  ENDPOINT: str = "http://127.0.0.1:5000/"
5
 
@@ -7,7 +11,7 @@ request_params = {
7
  "algorithm": "kmeans-clustering",
8
  "arguments": {
9
  "k": 3,
10
- "max_iter": 10,
11
  },
12
  }
13
 
@@ -23,5 +27,31 @@ r = requests.post(
23
  )
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  if __name__ == "__main__":
27
- print(r.json())
 
1
+
2
  import json
3
+ import requests
4
+ import matplotlib.pyplot as plt
5
+ import seaborn as sns
6
+
7
 
8
  ENDPOINT: str = "http://127.0.0.1:5000/"
9
 
 
11
  "algorithm": "kmeans-clustering",
12
  "arguments": {
13
  "k": 3,
14
+ "max_iter": 100,
15
  },
16
  }
17
 
 
27
  )
28
 
29
 
30
+ def plot():
31
+ cluster_data = r.json()["clusters"]
32
+ # plot the clusters and data points
33
+ fig, ax = plt.subplots(figsize=(8, 6))
34
+
35
+ sns.set()
36
+ for cluster in cluster_data:
37
+ sns.scatterplot(
38
+ x=[point[0] for point in cluster["points"]],
39
+ y=[point[1] for point in cluster["points"]],
40
+ label=f"Cluster {cluster['cluster_id']}",
41
+ ax=ax,
42
+ )
43
+ ax.scatter(
44
+ x=cluster["centroid"][0],
45
+ y=cluster["centroid"][1],
46
+ marker="x",
47
+ s=100,
48
+ linewidth=2,
49
+ color="black"
50
+ )
51
+ ax.set_title("K-means Clustering")
52
+ ax.legend()
53
+ plt.show()
54
+
55
+
56
  if __name__ == "__main__":
57
+ plot()
example/kmedoids.py ADDED
File without changes