MCPcopy Create free account
hub / github.com/AssemblyAI-Community/Machine-Learning-From-Scratch / predict

Method predict

10 KMeans/kmeans.py:21–48  ·  view source on GitHub ↗
(self, X)

Source from the content-addressed store, hash-verified

19
20
21 def predict(self, X):
22 self.X = X
23 self.n_samples, self.n_features = X.shape
24
25 # initialize
26 random_sample_idxs = np.random.choice(self.n_samples, self.K, replace=False)
27 self.centroids = [self.X[idx] for idx in random_sample_idxs]
28
29 # optimize clusters
30 for _ in range(self.max_iters):
31 # assign samples to closest centroids (create clusters)
32 self.clusters = self._create_clusters(self.centroids)
33
34 if self.plot_steps:
35 self.plot()
36
37 # calculate new centroids from the clusters
38 centroids_old = self.centroids
39 self.centroids = self._get_centroids(self.clusters)
40
41 if self._is_converged(centroids_old, self.centroids):
42 break
43
44 if self.plot_steps:
45 self.plot()
46
47 # classify samples as the index of their clusters
48 return self._get_cluster_labels(self.clusters)
49
50
51 def _get_cluster_labels(self, clusters):

Callers 1

kmeans.pyFile · 0.45

Calls 5

_create_clustersMethod · 0.95
plotMethod · 0.95
_get_centroidsMethod · 0.95
_is_convergedMethod · 0.95
_get_cluster_labelsMethod · 0.95

Tested by

no test coverage detected