如何在MATLAB中找出k-means聚类中每个聚类的质心。数据本质上是异构的。所以,我想编写一些MATLAB代码,可以绘制每个聚类的质心,并给出每个质心的坐标。 我使用以下代码进行聚类 -
function filter_samples(all_data, filter_values) {
if (filter_values.hasOwnProperty('age')) {
//check condition
}
if (filter_values.hasOwnProperty('gender')) {
//check condition
}
if (filter_values.hasOwnProperty('marks')) {
//check condition
}
}
答案 0 :(得分:0)
我稍微修改了你的代码:
rng default; % For reproducibility
X = [randn(100,2)*0.75+ones(100,2);
randn(100,2)*0.5-ones(100,2)];
opts=statset('Display','final');
[idx,C]=kmeans(X,4,'Distance','cityblock','Replicates',5,'Options',opts);
plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12);
hold on;
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12);
plot(X(idx==3,1),X(idx==3,2),'g.','MarkerSize',12);
plot(X(idx==4,1),X(idx==4,2),'y.','MarkerSize',12);
plot(C(:,1),C(:,2),'Kx','MarkerSize',15,'LineWidth',3);
legend('Cluster 1','Cluster 2','Cluster 3','Cluster 4','Centroids', 'Location','NW');
title('Cluster Assignments and centroids');
hold off;
for i=1:size(C, 1)
display(['Centroid ', num2str(i), ': X1 = ', num2str(C(i, 1)), '; X2 = ', num2str(C(i, 2))]);
end
这是情节:
以下是质心:
Centroid 1: X1 = 1.3661; X2 = 1.7232
Centroid 2: X1 = -1.015; X2 = -1.053
Centroid 3: X1 = 1.6565; X2 = 0.36376
Centroid 4: X1 = 0.35134; X2 = 0.85358