Spaces:
Sleeping
Sleeping
Jensen-holm
commited on
Commit
·
b378fde
1
Parent(s):
3dd9ab7
clustering plot for kmeans
Browse files- app.py +1 -1
- cluster/clusterer.py +1 -0
- cluster/kmeans.py +1 -0
- cluster/main.py +2 -0
- cluster/plot.py +43 -0
- example/neural_network.py +0 -1
- neural_network/neural_network.py +3 -1
app.py
CHANGED
@@ -54,4 +54,4 @@ def index():
|
|
54 |
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
-
app.run(debug=
|
|
|
54 |
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
+
app.run(debug=True)
|
cluster/clusterer.py
CHANGED
@@ -7,6 +7,7 @@ import numpy as np
|
|
7 |
@dataclass
|
8 |
class Clusterer:
|
9 |
cluster_func: Callable
|
|
|
10 |
|
11 |
def eval(
|
12 |
self,
|
|
|
7 |
@dataclass
|
8 |
class Clusterer:
|
9 |
cluster_func: Callable
|
10 |
+
plot = None
|
11 |
|
12 |
def eval(
|
13 |
self,
|
cluster/kmeans.py
CHANGED
@@ -74,4 +74,5 @@ class Kmeans(Clusterer):
|
|
74 |
"k": self.k,
|
75 |
"max_iter": self.max_iter,
|
76 |
"clusters": cluster_data,
|
|
|
77 |
}
|
|
|
74 |
"k": self.k,
|
75 |
"max_iter": self.max_iter,
|
76 |
"clusters": cluster_data,
|
77 |
+
"plot": self.plot,
|
78 |
}
|
cluster/main.py
CHANGED
@@ -3,6 +3,7 @@ import numpy as np
|
|
3 |
from cluster.clusterer import Clusterer
|
4 |
# for determing which clustering funciton to call
|
5 |
from cluster.opts import clustering_methods
|
|
|
6 |
|
7 |
|
8 |
def main(
|
@@ -17,4 +18,5 @@ def main(
|
|
17 |
alg = cluster_alg.from_dict(cluster_args)
|
18 |
|
19 |
alg.build(X)
|
|
|
20 |
return alg.to_dict(X)
|
|
|
3 |
from cluster.clusterer import Clusterer
|
4 |
# for determing which clustering funciton to call
|
5 |
from cluster.opts import clustering_methods
|
6 |
+
from cluster.plot import plot
|
7 |
|
8 |
|
9 |
def main(
|
|
|
18 |
alg = cluster_alg.from_dict(cluster_args)
|
19 |
|
20 |
alg.build(X)
|
21 |
+
plot(clusterer=alg, X=X)
|
22 |
return alg.to_dict(X)
|
cluster/plot.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import base64
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import seaborn as sns
|
7 |
+
from cluster.clusterer import Clusterer
|
8 |
+
|
9 |
+
|
10 |
+
matplotlib.use("Agg")
|
11 |
+
sns.set()
|
12 |
+
|
13 |
+
def plot(clusterer: Clusterer, X: np.array) -> None:
|
14 |
+
cluster_data = clusterer.to_dict(X)["clusters"]
|
15 |
+
# plot the clusters and data points
|
16 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
17 |
+
for cluster in cluster_data:
|
18 |
+
sns.scatterplot(
|
19 |
+
x=[point[0] for point in cluster["points"]],
|
20 |
+
y=[point[1] for point in cluster["points"]],
|
21 |
+
label=f"Cluster {cluster['cluster_id']}",
|
22 |
+
ax=ax,
|
23 |
+
)
|
24 |
+
ax.scatter(
|
25 |
+
x=cluster["centroid"][0],
|
26 |
+
y=cluster["centroid"][1],
|
27 |
+
marker="x",
|
28 |
+
s=100,
|
29 |
+
linewidth=2,
|
30 |
+
color="red",
|
31 |
+
)
|
32 |
+
ax.legend()
|
33 |
+
ax.set_title("K-means Clustering")
|
34 |
+
ax.set_ylabel("Normalized Petal Length (cm)")
|
35 |
+
ax.set_xlabel("Normalized Petal Length (cm)")
|
36 |
+
clusterer.plot = plt_bytes(fig)
|
37 |
+
|
38 |
+
|
39 |
+
def plt_bytes(fig) -> str:
|
40 |
+
buf = io.BytesIO()
|
41 |
+
fig.savefig(buf, format="png")
|
42 |
+
plt.close(fig)
|
43 |
+
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
example/neural_network.py
CHANGED
@@ -3,7 +3,6 @@ import seaborn as sns
|
|
3 |
import requests
|
4 |
import json
|
5 |
|
6 |
-
# ENDPOINT: str = "https://data-mining-from-scratch-backend.onrender.com/"
|
7 |
|
8 |
ENDPOINT: str = "http://127.0.0.1:5000/"
|
9 |
|
|
|
3 |
import requests
|
4 |
import json
|
5 |
|
|
|
6 |
|
7 |
ENDPOINT: str = "http://127.0.0.1:5000/"
|
8 |
|
neural_network/neural_network.py
CHANGED
@@ -51,6 +51,8 @@ class NeuralNetwork:
|
|
51 |
"func_prime": self.func_prime.__name__,
|
52 |
"hidden_size": self.hidden_size,
|
53 |
"mse": self.mse,
|
54 |
-
|
|
|
|
|
55 |
"plot": self.plot,
|
56 |
}
|
|
|
51 |
"func_prime": self.func_prime.__name__,
|
52 |
"hidden_size": self.hidden_size,
|
53 |
"mse": self.mse,
|
54 |
+
# not returning this because we are making our own
|
55 |
+
# plots and this can be a lot of data
|
56 |
+
# "loss_history": self.loss_history,
|
57 |
"plot": self.plot,
|
58 |
}
|