- 使用部分鸢尾花数据(MATLAB自带数据),并做可视化
clc
clear
load fisheriris % 载入matlab自带的鸢尾花数据集
s1 = meas(1:50,:);
[a,~] = sortrows(s1,-2);
S1 = a(1:25,1:2);
s2 = meas(51:100,:);
[a,~] = sortrows(s2,1);
S2 = a(1:25,1:2);
s3 = meas(101:150,:);
[a,b] = sortrows(s3,-1);
S3 = a(1:20,1:2);
meas = [S1;S2;S3];
species = species([1:25,51:75,101:120],:);
scatter(meas(:,1),meas(:,2),'.');
hold on
grid on
axis( [4 7.2 1.8 4.5] )
xlabel('Sepal length');
ylabel('Sepal width');
hold off
 2. kmeans聚类
[cidx2,cmeans2] = kmeans(meas,3,'dist','sqeuclidean');
[silh2,~] = silhouette(meas,cidx2,'sqeuclidean');
 3. 做聚类可视化
figure;
hold on
grid on
axis( [4 7.2 1.8 4.6] )
gscatter(meas(:,1),meas(:,2),cidx2,'rbg','...')
xlabel('Sepal length');
ylabel('Sepal width');
hold off
 4. Hierarchical Clustering
eucD = pdist(meas,'euclidean');
clustTreeEuc = linkage(eucD,'average');
cophenet(clustTreeEuc,eucD)
h_gca = gca;
h_gca.TickDir = 'out';
h_gca.TickLength = [.002 0];
h_gca.XTickLabel = [];
 本文参照:https://ww2.mathworks.cn/help/stats/cluster-analysis-example.html
|