How to assign new data to previous Centroid using K-means

19 views (last 30 days)
Hello everyone, I hope you are doing well.
I have written the following code. Now, i am going to apply on new incoming dataset.
for example one data come then i applied K-means, the output is save and the second data come the algorithm check if it belong to that centroid it assign that data to that centroid.
How can i modified the code for new incoming dataset.
%Read Dataset
%Find the Optimal Clusters for this dataset
eva = evalclusters(dataset1,'kmeans','silhouette','KList',[1:10])
%Apply Kmeans to the dataset with Optimal Clusters K
[idx,C,sumdist] = kmeans(dataset,K,'Display','final','Replicates',5);
%Plot the Clusters
hold on
legend('Cluster 1','Cluster 2','Cluster 3','Cluster 4','Cluster 5','Cluster Centroid')

Answers (1)

Image Analyst
Image Analyst on 14 May 2022
Maybe you can just find the distances of all points in your training set from their centroids. Then, for your second set, compute the distances of each point to each of the centroids. Whichever centriod the point is closest to is the cluster it belongs to. You could also use knnsearch for that.
Image Analyst
Image Analyst on 19 May 2022
I gave you option 2 already. Here it is again:
[rows, columns] = size(testData)
for k = 1 : rows
% Get coordinates of this one test data point.
tx = testData(k, 1);
ty = testData(k, 2);
% Get distance of that one point to all centroid coordinates.
distances = sqrt((tx - C(:, 1)) .^ 2 + (ty - C(:, 2));
% Find out which centroid is closest to this data point
% and assign the closest class to IndexOfClosestClass.
[minDistance, IndexOfClosestClass(k)] = min(distances);
Where C is what you got from doing kmeans() on the first set.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by