K-means cluster plot [closed]

不打扰是莪最后的温柔 提交于 2019-12-23 02:04:39

问题


I have a data matrix of 510x6 and want to perform K-means cluster analysis on this. I am having problem in plotting all the different clusters in 2 dimensions. Is it not possible to plot 6 different clusters in 2 dimensions?


回答1:


Let's start by looking at some data which is 150x4 and try and split that into 6 different clusters. The fisheriris dataset has four columns (yours has 6), which correspond to sepal length, sepal width, petal length and petal width and can be loaded into MATLAB like this:

load fisheriris

We can then break the data down into six clusters with:

clusters = kmeans(meas, 6);

cluster1 = meas(clusters == 1, :);
cluster2 = meas(clusters == 2, :);
cluster3 = meas(clusters == 3, :);
cluster4 = meas(clusters == 4, :);
cluster5 = meas(clusters == 5, :);
cluster6 = meas(clusters == 6, :);

Given that we have four pieces of information for each data point, in order to visualise each cluster we need to choose what we want to look at. For example I might want to look at the clusters for sepal length against sepal width. These are the first two columns and I can see them with:

figure
axes

plot(cluster1(:, [1, 2]), '*'); hold all
plot(cluster2(:, [1, 2]), '*')
plot(cluster3(:, [1, 2]), '*')
plot(cluster4(:, [1, 2]), '*')
plot(cluster5(:, [1, 2]), '*')
plot(cluster6(:, [1, 2]), '*')

xlabel('Sepal Length')
ylabel('Sepal Width')

If I want to look at columns at once, we need to go up a dimension:

figure
axes
hold all

plot3(cluster1(:, 1), cluster1(:, 2), cluster1(:, 3),'*')
plot3(cluster2(:, 1), cluster2(:, 2), cluster2(:, 3),'*')
plot3(cluster3(:, 1), cluster3(:, 2), cluster3(:, 3),'*')
plot3(cluster4(:, 1), cluster4(:, 2), cluster4(:, 3),'*')
plot3(cluster5(:, 1), cluster5(:, 2), cluster5(:, 3),'*')
plot3(cluster6(:, 1), cluster6(:, 2), cluster6(:, 3),'*')

grid on
box on

Your data has six dimensions so it is even harder to get a sense of the clusters visually. You could have a go at doing something similar to the plotmatrix function:

plotmatrix(meas)



来源:https://stackoverflow.com/questions/18091728/k-means-cluster-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!