Spaces:
Build error
Build error
Commit
·
04b61ad
1
Parent(s):
c2687ce
kmeans clustering works and returns centroids and labeled data
Browse files- app.py +2 -1
- cluster/clusterer.py +0 -7
- cluster/kmeans.py +2 -2
- cluster/main.py +5 -4
- cluster/opts.py +2 -2
- example/kmeans.py +2 -2
- example/neural_network.py +3 -1
app.py
CHANGED
|
@@ -36,6 +36,7 @@ def index():
|
|
| 36 |
# parse arguments
|
| 37 |
algorithm = options[request.json["algorithm"]]
|
| 38 |
args = request.json["arguments"]
|
|
|
|
| 39 |
|
| 40 |
# using the iris data set for every algorithm
|
| 41 |
X, y = iris()
|
|
@@ -48,4 +49,4 @@ def index():
|
|
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
app.run(debug=
|
|
|
|
| 36 |
# parse arguments
|
| 37 |
algorithm = options[request.json["algorithm"]]
|
| 38 |
args = request.json["arguments"]
|
| 39 |
+
args["algorithm"] = request.json["algorithm"]
|
| 40 |
|
| 41 |
# using the iris data set for every algorithm
|
| 42 |
X, y = iris()
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
+
app.run(debug=True)
|
cluster/clusterer.py
CHANGED
|
@@ -7,7 +7,6 @@ import numpy as np
|
|
| 7 |
@dataclass
|
| 8 |
class Clusterer:
|
| 9 |
cluster_func: Callable
|
| 10 |
-
options: dict
|
| 11 |
|
| 12 |
def eval(
|
| 13 |
self,
|
|
@@ -19,9 +18,3 @@ class Clusterer:
|
|
| 19 |
@classmethod
|
| 20 |
def from_dict(cls, dct: dict):
|
| 21 |
return cls(**dct)
|
| 22 |
-
|
| 23 |
-
def to_dict(self):
|
| 24 |
-
return {
|
| 25 |
-
"cluster_method": self.cluster_func.__name__,
|
| 26 |
-
"options": self.options,
|
| 27 |
-
}
|
|
|
|
| 7 |
@dataclass
|
| 8 |
class Clusterer:
|
| 9 |
cluster_func: Callable
|
|
|
|
| 10 |
|
| 11 |
def eval(
|
| 12 |
self,
|
|
|
|
| 18 |
@classmethod
|
| 19 |
def from_dict(cls, dct: dict):
|
| 20 |
return cls(**dct)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cluster/kmeans.py
CHANGED
|
@@ -61,6 +61,6 @@ class Kmeans(Clusterer):
|
|
| 61 |
return {
|
| 62 |
"k": self.k,
|
| 63 |
"max_iter": self.max_iter,
|
| 64 |
-
"centroids": self.centroids,
|
| 65 |
-
"clusters": self.clusters,
|
| 66 |
}
|
|
|
|
| 61 |
return {
|
| 62 |
"k": self.k,
|
| 63 |
"max_iter": self.max_iter,
|
| 64 |
+
"centroids": self.centroids.tolist(),
|
| 65 |
+
"clusters": self.clusters.tolist(),
|
| 66 |
}
|
cluster/main.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
from sklearn.model_selection import train_test_split
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
from cluster.clusterer import Clusterer
|
|
@@ -11,9 +10,11 @@ def main(
|
|
| 11 |
y: np.array,
|
| 12 |
args: dict,
|
| 13 |
) -> dict:
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
alg.
|
| 18 |
|
|
|
|
| 19 |
return alg.to_dict()
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
|
| 3 |
from cluster.clusterer import Clusterer
|
|
|
|
| 10 |
y: np.array,
|
| 11 |
args: dict,
|
| 12 |
) -> dict:
|
| 13 |
+
cluster_func = args.pop("algorithm")
|
| 14 |
+
cluster_alg: Clusterer = clustering_methods[cluster_func]
|
| 15 |
|
| 16 |
+
cluster_args: dict = {"cluster_func": cluster_func} | args
|
| 17 |
+
alg = cluster_alg.from_dict(cluster_args)
|
| 18 |
|
| 19 |
+
alg.build(X)
|
| 20 |
return alg.to_dict()
|
cluster/opts.py
CHANGED
|
@@ -4,6 +4,6 @@ from cluster.kmeans import Kmeans
|
|
| 4 |
|
| 5 |
|
| 6 |
clustering_methods: dict[str, Clusterer] = {
|
| 7 |
-
"kmeans": Kmeans,
|
| 8 |
-
"kmedoids": Kmedoids,
|
| 9 |
}
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
clustering_methods: dict[str, Clusterer] = {
|
| 7 |
+
"kmeans-clustering": Kmeans,
|
| 8 |
+
"kmedoids-clustering": Kmedoids,
|
| 9 |
}
|
example/kmeans.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
|
| 4 |
-
ENDPOINT: str = "
|
| 5 |
|
| 6 |
request_params = {
|
| 7 |
-
"algorithm": "kmeans",
|
| 8 |
"arguments": {
|
| 9 |
"k": 3,
|
| 10 |
"max_iter": 10,
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
|
| 4 |
+
ENDPOINT: str = "http://127.0.0.1:5000/"
|
| 5 |
|
| 6 |
request_params = {
|
| 7 |
+
"algorithm": "kmeans-clustering",
|
| 8 |
"arguments": {
|
| 9 |
"k": 3,
|
| 10 |
"max_iter": 10,
|
example/neural_network.py
CHANGED
|
@@ -3,7 +3,9 @@ import seaborn as sns
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
ENDPOINT: str = "https://data-mining-from-scratch-backend.onrender.com/"
|
|
|
|
|
|
|
| 7 |
|
| 8 |
request_params = {
|
| 9 |
"algorithm": "neural-network",
|
|
|
|
| 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 |
|
| 10 |
request_params = {
|
| 11 |
"algorithm": "neural-network",
|