【问题标题】:Internal Operations of K-MeansK-Means 的内部操作
【发布时间】:2012-03-09 16:06:24
【问题描述】:

使用 Matlabs K-means 我不确定聚类的细节。为了解释这一点,我将使用一个示例:

我的数据已经标准化,输出如下所示:

每一行代表一个标准化后的网络数据包。所以第 1 行代表来自计算机 A 的数据包。

现在我想知道当我在 Matlab 中运行我的 K-means 时,它是对每一列进行聚类还是通过行进行聚类?

即A列是否属于Cluster 1 Column B Cluster 2等。

询问的原因是我需要每个数据包(行)保持绑定,并且每个数据包都根据其内在质量进行聚类。然而,我担心这可能会严重削弱它的能力。但我希望有一种聚合方法可以解决这个谜题。

代码:

        %% generate sample data
K = 4;
numObservarations = 5000;
dimensions = 42;
%% cluster
opts = statset('MaxIter', 500, 'Display', 'iter');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, K, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',3);
%% plot data+clusters
figure, hold on
scatter3(data(:,1),data(:,2),data(:,3), 5, clustIDX, 'filled')
scatter3(clusters(:,1),clusters(:,2),clusters(:,3), 100, (1:K)', 'filled')
hold off, xlabel('x'), ylabel('y'), zlabel('z')
%% plot clusters quality
figure
[silh,h] = silhouette(data, clustIDX);
avrgScore = mean(silh);
%% Assign data to clusters
% calculate distance (squared) of all instances to each cluster centroid
D = zeros(numObservarations, K);     % init distances
for k=1:K
%d = sum((x-y).^2).^0.5
D(:,k) = sum( ((data - repmat(clusters(k,:),numObservarations,1)).^2), 2);
end
% find  for all instances the cluster closet to it
[minDists, clusterIndices] = min(D, [], 2);
% compare it with what you expect it to be
sum(clusterIndices == clustIDX)

结果:

这是基于 5000 行。不幸的是,聚类后无法重建数据限制了我对正在发生的事情的了解。 (见相关问题:MATLAB - Classification output

【问题讨论】:

    标签: matlab machine-learning cluster-analysis mathematical-optimization k-means


    【解决方案1】:

    Matlab中聚类和分类数据输入的标准格式是:

    1. 每个样本一行
    2. 每列中某个样本(行)的不同特征。

    【讨论】:

    • 除了以图形方式绘制数据来验证这一点之外,还有其他方法吗?
    猜你喜欢
    • 2015-05-29
    • 2018-09-15
    • 2020-08-27
    • 2013-07-03
    • 2020-03-03
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多