How can I segment the 'test' datasets based on k-means used in training datasets in R. For example, I run k-means clustering in R and get 7 segments from demographic and psychographic attributes of the customer in one state. Assigned the name to all the 7 segments based on the attributes present in each cluster. Now I want to segment other state data into the same 7 segments using previously run clustering on one state data? How to do this in R? Is it possible? Please do not confuse with supervised learning(Classification problem). My query is for unsupervised.


2 Answers 2


K-means produces a set of cluster centroids that approximately minimize the cost function. A cluster is defined as the set of points that share a common closest centroid. So, suppose you've trained k-means on some training set, and now want to cluster new points in a test set. Simply find the nearest centroid to each new point. Note that this applies to k-means, but not other clustering methods.

Geometrically, k-means induces a Voronoi partition on input space, with the centroids as seeds. All points within the Voronoi cell of a particular centroid are assigned to the same cluster. For example, in this image from Wikipedia, the black points would correspond to centroids, and all points in the corresponding colored regions (Voronoi cells) would be clustered together:

enter image description here

  • $\begingroup$ Thank you for your comment. I got you. Just checking Do you mean I have to run k-means clustering for train data again to find the centroid and then use these centroids to cluster test data? How can I pass-on these centroid as seed using R ? I am looking for:- Eg: suppose segment 1 in train data consist of high income-high end cars-big home-graduate plus-no child. the segment 1 in test data have the same attributes. Your help is appreciated. $\endgroup$
    – June
    Jul 22, 2018 at 18:47
  • 1
    $\begingroup$ You would run k-means once on the training set, which returns the centroids. For each point in the test set, find the nearest centroid, which gives the cluster assignment. $\endgroup$
    – user20160
    Jul 23, 2018 at 3:39

This question has been asked many many times here already. Please use the search functions to save time.

For k-means only the answer is trivial: your "test" samples should be assigned using nearest-neighbor classification to the k cluster centers. I.e. each sample is mapped to the least-squared-error center. This is fairly cheap: just k sum-of-squares computations.

For the general case - other clustering algorithms - that won't just work. Instead, the obvious strategy is to train a classifier, using the cluster labels as training data.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.