简要了解K-means聚类
给定一个数据点集合和需要的聚类数目k,k由用户指定,k均值算法根据某个距离函数反复把数据分入k个聚类中。
简单示例
%数据导入
rng default; % For reproducibility
X = [randn(100,2)*0.75+ones(100,2);
randn(100,2)*0.5-ones(100,2)];
%作轮廓图
figure;
plot(X(:,1),X(:,2),'.');
title 'Randomly Generated Data';
%从轮廓图中观察有2个类,设定k值为2
opts = statset('Display','final');
%idx索引,C质心位置
[idx,C] = kmeans(X,2,'Distance','cityblock',...
'Replicates',5,'Options',opts)
%绘制簇和簇质心
figure;
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(C(:,1),C(:,2),'kx',...
'MarkerSize',15,'LineWidth',3)
legend('Cluster 1','Cluster 2','Centroids',...
'Location','NW')
title 'Cluster Assignments and Centroids'
hold off