0
我有一個1000x6數據集,並使用下面的kmeans腳本是好的,但是當我想輸出其中一個集羣時,它只能作爲一個列出現?在Kmeans中輸出IDX?
%% 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幾次,所以不太確定所有的語法! +1 – 2012-07-11 19:23:07