Spaces:
Sleeping
Sleeping
File size: 1,074 Bytes
3cab2dd 932b3cb 3cab2dd 932b3cb 3cab2dd |
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 |
from dataclasses import dataclass
import numpy as np
from cluster.distance import euclidean
from cluster.clusterer import Clusterer
@dataclass
class Kmeans(Clusterer):
k: int
max_iter: int
def build(
self,
X_train: np.array,
):
# Randomly select centroid start points, uniformly distributed across the domain of the dataset
minimum = np.min(X_train, axis=0)
maximum = np.max(X_train, axis=0)
centroids = [np.uniform(minimum, maximum) for _ in range(self.k)]
# loop through and cluster data
prev_centroids = 0
iteration = 0
while True:
sorted_pts = [[] for _ in range(self.k)]
for x in X_train:
dists = euclidean(x, centroids)
if not np.not_equal(
centroids,
prev_centroids,
).any():
break
if not iteration < self.k:
break
iteration += 1
def label():
...
def main(self):
return self.from_dict()
|