how to find the closest centroid k means clustering python code example
Example: assign each point to the cluster with the closest centroid python
def kmeans(X, k, maxiter, seed = None):
"""
specify the number of clusters k and
the maximum iteration to run the algorithm
"""
n_row, n_col = X.shape
if seed is not None:
np.random.seed(seed)
rand_indices = np.random.choice(n_row, size = k)
centroids = X[rand_indices]
for itr in range(maxiter):
distances_to_centroids = pairwise_distances(X, centroids, metric = 'euclidean')
cluster_assignment = np.argmin(distances_to_centroids, axis = 1)
new_centroids = np.array([X[cluster_assignment == i].mean(axis = 0) for i in range(k)])
if np.all(centroids == new_centroids):
break
centroids = new_centroids
return centroids, cluster_assignment