Clustering data and then classifying a new set of data
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi I'm new to clustering and classifying data. I have currently generated training data that I can plot and cluster using k-means clustering, I have achieved this using the second example in this link; http://uk.mathworks.com/help/stats/kmeans.html. I would like to now use the labels created through my clusters to help classifying new data based on its plot values and give it the label of which cluster it falls under. Any direction or pointers would be helpful. I hope that makes sense and thank you in advance.
1 commentaire
Réponses (1)
Ayush Aniket
le 8 Mai 2025
After performing k-means clustering, you can use the resulting cluster centroids to classify new data points by assigning each new point to the nearest centroid. This is a common approach for classifying new data using unsupervised clustering results. Refer the example code below:
% Training data
Xtrain = randn(100,2); % Example data
k = 3;
[idx, C] = kmeans(Xtrain, k);
% New data
Xnew = [0 0; 1 1; -1 -1]; % Example new points
For a new data point (or set of points) Xnew, assign each to the nearest centroid by calculating the pair-wise distance between the data point and the cluster centroids using the pdlist2 function( calcutes euclidean distance by default), and the finding the minimum one:
% Classify new data based on nearest centroid
D = pdist2(Xnew, C);
[~, newLabels] = min(D, [], 2);
disp(newLabels); % Shows which cluster each new point belongs to
Refer the documentation link to read more about the function here: https://www.mathworks.com/help/stats/pdist2.html#d126e891557
1 commentaire
Image Analyst
le 9 Mai 2025
Very nice code. Thanks for answering it. I'm sure it will come in useful for others. 🙂
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!