@@ -60,9 +60,11 @@ def _initialize_cetroids(self, init):
60
60
raise ValueError ('Unknown type of init parameter' )
61
61
62
62
def _predict (self , X = None ):
63
- """Perform the clustering on the dataset."""
63
+ """Perform clustering on the dataset."""
64
64
self ._initialize_cetroids (self .init )
65
65
centroids = self .centroids
66
+
67
+ # Optimize clusters
66
68
for _ in range (self .max_iters ):
67
69
self ._assign (centroids )
68
70
centroids_old = centroids
@@ -95,6 +97,7 @@ def _assign(self, centroids):
95
97
self .clusters [closest ].append (row )
96
98
97
99
def _closest (self , fpoint , centroids ):
100
+ """Find the closest centroid for a point."""
98
101
closest_index = None
99
102
closest_distance = None
100
103
for i , point in enumerate (centroids ):
@@ -109,6 +112,7 @@ def _get_centroid(self, cluster):
109
112
return [np .mean (np .take (self .X [:, i ], cluster )) for i in range (self .n_features )]
110
113
111
114
def _dist_from_centers (self ):
115
+ """Calculate distance from centers."""
112
116
return np .array ([min ([euclidean_distance (x , c ) for c in self .centroids ]) for x in self .X ])
113
117
114
118
def _choose_next_center (self ):
@@ -120,7 +124,11 @@ def _choose_next_center(self):
120
124
return self .X [ind ]
121
125
122
126
def _is_converged (self , centroids_old , centroids ):
123
- return True if sum ([euclidean_distance (centroids_old [i ], centroids [i ]) for i in range (self .K )]) == 0 else False
127
+ """Check if the distance between old and new centroids is zero."""
128
+ distance = 0
129
+ for i in range (self .K ):
130
+ distance += euclidean_distance (centroids_old [i ], centroids [i ])
131
+ return distance == 0
124
132
125
133
def plot (self , data = None ):
126
134
sns .set (style = "white" )
0 commit comments