I want to know how i can apply k-means clustering on my scatter plotted dataset.

I want to know that how i can apply k means clustering on the above diagram i don't know the code please help me.

 Accepted Answer

You probably want to use kmeans.
Since you didn't give us any data to workj with, nor did you give us any idea of the number of groups, I've deciced to make some up.
Age = randn(40,1)*5+45;
C = 2*Age+100+20*randn(40,1);
plot(Age, C,'s')
This roughly replicates your data. Now I'll divide it up into 3 groups, and plot them with different colours.
idx = kmeans([Age,C],3); % select 3 groups
cmap = parula(3);
clf; hold on
for i=1:3
k = find(idx==i);
plot(Age(k), C(k), 's', 'MarkerFaceColor',cmap(i,:));
end
hold off
Feel free to play with the # of expected groups.
tmp.png

6 Comments

Sorry for your inconvenience sir,and thank you so much for your reply it's really helpful for me.
I have attached my dataset.
Sir it will be really helpful for me if you can do required modifications in code according to my dataset.
Try the following with your dataset. Note that you are missing one data point at the end. I've added a convex hull around the groups for easy viewing. In this case I'm using 5 groups.
[Dat] = xlsread('dataset.xlsx','sheet1','A2:B51');
Age = Dat(:,1); C = Dat(:,2);
X = zscore([Age, C]); % probably best to normalise
n = 5; % # of groups requested
idx = kmeans(X,n); % select 3 groups
cmap = parula(n);
clf; hold on
for i=1:n
k = find(idx==i);
plot(Age(k), C(k), 's', 'MarkerFaceColor',cmap(i,:));
dt = DelaunayTri(Age(k), C(k))
k = convexHull(dt);
plot(dt.X(k,1),dt.X(k,2), '--');
end
hold off
tmp.png
@David Wilson Sir i want to calcuate the accuracy of the above diagram is it possible or not.
Not easily to my knowledge. It will be application dependent.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!