【问题标题】:Output of IDX in Kmeans?Kmeans中IDX的输出?
【发布时间】:2012-07-12 03:18:44
【问题描述】:

我有一个 1000x6 的数据集,使用下面的 kmeans 脚本很好,但是当我想输出其中一个集群时,它只输出为一列?

%% cluster
opts = statset('MaxIter', 100, 'Display', 'iter');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, K, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',6);

%% 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)

% Output cluster data to K datasets
K1 = data(clustIDX==1)
K2 = data(clustIDX==2)... etc

K1 = data(clustIDX==1)不应该输出完整的行信息吗?不只是一列,而是六列,就像原始数据集一样?或者这只是输出距离?

【问题讨论】:

    标签: matlab cluster-analysis k-means


    【解决方案1】:

    替换

    K1 = data(clustIDX==1)
    K2 = data(clustIDX==2)
    

    K1 = data(clustIDX==1,:)
    K2 = data(clustIDX==2,:)
    

    第一个只检索对应行的第一列。第二个应该可以解决它,我试过了,它可以工作。

    【讨论】:

    • 嗨,谢谢 quenis,这很有效。只使用过几次matlab,所以对所有语法都不太确定! +1
    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 2017-08-30
    • 2021-08-18
    • 2014-05-11
    • 2012-06-30
    • 2013-10-22
    • 1970-01-01
    • 2013-08-17
    相关资源
    最近更新 更多