Below is a straightforward implementation of the k-means algorithm in Python: import numpy as np def kmeans ( data , k , max_iters = 100 ) : # Randomly initialize centroids centroids = data [ np . random . choice ( range ( data . shape [ 0 ] ) , size = k , replace = False ) ] for _ in range ( max_iters ) : # Assign each data point to the nearest centroid labels = np . argmin ( np . linalg . norm ( data [ : , np . newaxis ] - centroids , axis = - 1 ) , axis = - 1 ) # Recalculate centroids based on the clusters new_centroids = np . array ( [ data [ labels == i ] . mean ( axis = 0 ) for i in range ( k ) ] ) # Check if centroids have converged if np . all ( centroids == new_centroids ) : break centroids = new_centroids return centroids , labels This code begins by randomly selecting data points to serve as the centroids. It then loops through a predetermined maximum count of iterations, during which each point is assigned to the closest centroid, followed by a recalculation of the centroid positions. This cycle continues until either the maximum iterations are completed or the centroids no longer change, indicating convergence. Note: It is essential to understand that this is a basic implementation of k-means. In practical scenarios, you may need to incorporate additional features, such as eliminating empty clusters or stopping if the movement of any centroid is below a specified threshold.