Sau đây là một ví dụ hoàn chỉnh về phân nhóm:
%% generate sample data
K = 3;
numObservarations = 100;
dimensions = 3;
data = rand([numObservarations dimensions]);
%% 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), 50, clustIDX, 'filled')
scatter3(clusters(:,1),clusters(:,2),clusters(:,3), 200, (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)
bạn mong đợi sản lượng nào? – SilentGhost
Giả sử các cụm của tôi là p1..p19. và tôi có điểm dữ liệu thử nghiệm mà tôi chọn ngẫu nhiên từ dữ liệu thử nghiệm mà tôi sử dụng trong khi phân cụm. Tôi muốn thấy sth như thế; "dữ liệu thử nghiệm của tôi thuộc về p5" – tguclu
Tôi tìm thấy một cách nhưng không chắc chắn đó là chính xác. ctrs giữ các trung tâm của mỗi cụm. Nếu tôi tính toán các yếu tố btw khoảng cách euclidian của ctrs và dữ liệu thử nghiệm của tôi và nhận được chỉ số tối thiểu thì nó sẽ cho tôi chỉ số cụm mà dữ liệu thử nghiệm của tôi thuộc về. bất kỳ ý tưởng nào? – tguclu