K均值聚类图

npr*_*rak 1 matlab cluster-analysis k-means

我有一个510x6的数据矩阵,并希望对此进行K-means聚类分析.我在绘制2维中的所有不同聚类时遇到问题.是不是可以在2个维度上绘制6个不同的聚类?

Hug*_*not 8

让我们首先看一些150x4的数据,并尝试将其分成6个不同的集群.fisheriris数据集有四列(你的有6个),对应于萼片长度,萼片宽度,花瓣长度和花瓣宽度,可以加载到MATLAB中,如下所示:

load fisheriris
Run Code Online (Sandbox Code Playgroud)

然后我们可以将数据分解为六个集群:

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, :);
Run Code Online (Sandbox Code Playgroud)

鉴于每个数据点有四条信息,为了可视化每个集群,我们需要选择我们想要查看的内容.例如,我可能想看看萼片长度与萼片宽度的簇.这是前两列,我可以看到它们:

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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果我想立刻查看列,我们需要提升一个维度:

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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您的数据有六个维度,因此更难以直观地了解集群.你可以做一些类似于plotmatrix函数的事情:

plotmatrix(meas)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述