Implementation of k-means clustering. f