def step(self):
"""
Half of the step of k-means
"""
if self.step_completed:
d = self.data.X
points = [d[self.clusters == i] for i in range(len(self.centroids))]
for i in range(len(self.centroids)):
c_points = points[i]
self.centroids[i, :] = (np.average(c_points, axis=0)
if len(c_points) > 0 else np.nan)
# reinitialize empty centroids
nan_c = np.isnan(self.centroids).any(axis=1)
if np.count_nonzero(nan_c) > 0:
self.centroids[nan_c] = self.random_positioning(
np.count_nonzero(nan_c))
self.centroids_moved = True
else:
self.clusters = self.find_clusters(self.centroids)
self.centroids_moved = False
self.step_no += 1
self.centroids_history = self.set_list(
self.centroids_history, self.step_no, np.copy(self.centroids))
评论列表
文章目录