当前位置:   article > 正文

【入门向】k-means聚类函数详解(基于鸢尾花数据集)【MATLAB】_kmeans函数

kmeans函数

k-means聚类函数

先放例程

官方例程点这☜ Train a k-Means Clustering Algorithm.


load fisheriris
X = meas(:,3:4);

figure;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';

rng(1); % For reproducibility
[idx,C] = kmeans(X,3);

    % Assigns each node in the grid to the closest centroid
x1 = min(X(:,1)):0.01:max(X(:,1));
x2 = min(X(:,2)):0.01:max(X(:,2));
[x1G,x2G] = meshgrid(x1,x2);
XGrid = [x1G(:),x2G(:)]; % Defines a fine grid on the plot

idx2Region = kmeans(XGrid,3,'MaxIter',1,'Start',C);
    % Assigns each node in the grid to the closest centroid
    
figure;
gscatter(XGrid(:,1),XGrid(:,2),idx2Region,...
    [0,0.75,0.75;0.75,0,0.75;0.75,0.75,0],'..');
hold on;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';
legend('Region 1','Region 2','Region 3','Data','Location','SouthEast');
hold off;    

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

分段解析

  • 注:“%”段为注释,MATLAB代码好像没办法在markdown编辑器中高亮(小声,也可能是我自己没研究明白QuQ)。

PART1——载入数据集

load fisheriris
X = meas(:,3:4);
  • 1
  • 2

加载样本数据并取数据数组的第3,4列存到变量X中。
这一步的目的主要是为了获取样本数据集,还有很多其他类型的数据集可以使用,可以参考这篇博客《一些用于聚类和分类问题的数据集》
fisheriris——鸢尾花数据集(意为fisher算法的iris数据集),是一类多重变量分析的数据集,样本数量150个,每类50个。通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa(山鸢尾),Versicolour(杂色鸢尾),Virginica(维吉尼亚鸢尾))三个种类中的哪一类。
鸢尾花完整数据集放在文末。

PART2——画出数据集分布图

figure;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';
  • 1
  • 2
  • 3
  • 4
  • 5

figure——创建图窗窗口
X(:,1) 表示 X 数组的第一列,X(:,2) 同理。
运行 plot 函数产生下图,其中 X 的第一列为横坐标取值,第二列为纵坐标取值。
figure1

PART3——kmeans对数据集聚类

rng(1); % For reproducibility
[idx,C] = kmeans(X,3);
  • 1
  • 2

rng——控制随机数生成器
常用语法有:

rng(seed)
rng(seed,generator)
s = rng

指定 MATLAB® 随机数生成器的种子。例如,rng(1) 使用种子 1 初始化梅森旋转生成器。相关例程请参考《rng控制随机数生成器》
kmeans——k均值聚类
常用语法有:

idx = kmeans(X,k)
idx = kmeans(X,k,Name,Value)
[idx,C] = kmeans()
[idx,C,sumd] = kmeans(
)
[idx,C,sumd,D] = kmeans(___)

此处idx是长度为N×1的标号数组,C是聚类中心坐标值组成的数组,因此处聚类组数k=3,所处空间为二维平面,因此C的大小为3×2

PART4——确定坐标栅格

    % Assigns each node in the grid to the closest centroid
x1 = min(X(:,1)):0.01:max(X(:,1));
x2 = min(X(:,2)):0.01:max(X(:,2));
[x1G,x2G] = meshgrid(x1,x2);
XGrid = [x1G(:),x2G(:)];  % Defines a fine grid on the plot
  • 1
  • 2
  • 3
  • 4
  • 5

x1,x2用于决定坐标范围,取0.01为最小间距。
使用“数据游标”工具,可以更加直观地看到数据横纵轴上下限。横坐标下限
纵坐标下限
横坐标上限
纵坐标上限
x ∈ [ 1 , 6.9 ] y ∈ [ 0.1 , 2.5 ] x \in [1,6.9] \\ y \in [0.1, 2.5] x[1,6.9]y[0.1,2.5]
以0.01为间距划分,可以计算出x1和x2的长度:
x 1 = ( 6.9 − 1 ) / 0.01 + 1 = 591 x 2 = ( 2.5 − 0.1 ) / 0.01 + 1 = 241 x1=(6.9-1)/0.01+1=591\\x2=(2.5-0.1)/0.01+1=241 x1=(6.91)/0.01+1=591x2=(2.50.1)/0.01+1=241
在工作区中可以看到变量长度与计算结果相同:
请添加图片描述
mshgrid建立二维网格

PART5——kmeans对网格点聚类

idx2Region = kmeans(XGrid,3,'MaxIter',1,'Start',C);
    % Assigns each node in the grid to the closest centroid
  • 1
  • 2

MaxIter是指kmeans算法最大迭代次数,此处最大迭代次数为1。

PART6——作图

figure;
gscatter(XGrid(:,1),XGrid(:,2),idx2Region,...
    [0,0.75,0.75;0.75,0,0.75;0.75,0.75,0],'..');
hold on;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';
legend('Region 1','Region 2','Region 3','Data','Location','SouthEast');
hold off;    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

gscatter——散点图绘制工具
常用语法有:

gscatter(x,y,g)
gscatter(x,y,g,clr,sym,siz)
gscatter(x,y,g,clr,sym,siz,doleg)
gscatter(x,y,g,clr,sym,siz,doleg,xnam,ynam)
gscatter(ax,)
h = gscatter(
)

值得注意的是:咋此处gscatter并不是用来绘制“散点图”,而是利用带有颜色的密集散点形成带颜色的区域。因为region取值足够大,所以散点足够密集(就成一片了0m0)。
idx2Region

[0,0.75,0.75;0.75,0,0.75;0.75,0.75,0]是三个RGB颜色取值,对应颜色分别为:
0,0.75,0.75
0.75,0,0.75
0.75,0.75,0

gscatter(XGrid(:,1),XGrid(:,2),idx2Region,...
    [0,0.75,0.75;0.75,0,0.75;0.75,0.75,0],'..');
  • 1
  • 2

这个’…‘指的(应该)是散点的形状,替换成’.'输出的图片一样。(我怀疑这里是写例程的老师手抖了,多大了一个.【狗头保命】)

legend——在坐标区上添加图例
常用语法有:

legend
legend(label1,…,labelN)
legend(labels)
legend(subset,)
legend(target,
)

figure2

鸢尾花数据集

萼片长(sepal length)萼片宽(sepal width)花瓣长(petal length)花瓣宽(petal width)
5.103.501.400.20
4.903.001.400.20
4.703.201.300.20
4.603.101.500.20
5.003.601.400.20
5.403.901.700.40
4.603.401.400.30
5.003.401.500.20
4.402.901.400.20
4.903.101.500.10
5.403.701.500.20
4.803.401.600.20
4.803.001.400.10
4.303.001.100.10
5.804.001.200.20
5.704.401.500.40
5.403.901.300.40
5.103.501.400.30
5.703.801.700.30
5.103.801.500.30
5.403.401.700.20
5.103.701.500.40
4.603.601.000.20
5.103.301.700.50
4.803.401.900.20
5.003.001.600.20
5.003.401.600.40
5.203.501.500.20
5.203.401.400.20
4.703.201.600.20
4.803.101.600.20
5.403.401.500.40
5.204.101.500.10
5.504.201.400.20
4.903.101.500.20
5.003.201.200.20
5.503.501.300.20
4.903.601.400.10
4.403.001.300.20
5.103.401.500.20
5.003.501.300.30
4.502.301.300.30
4.403.201.300.20
5.003.501.600.60
5.103.801.900.40
4.803.001.400.30
5.103.801.600.20
4.603.201.400.20
5.303.701.500.20
5.003.301.400.20
7.003.204.701.40
6.403.204.501.50
6.903.104.901.50
5.502.304.001.30
6.502.804.601.50
5.702.804.501.30
6.303.304.701.60
4.902.403.301.00
6.602.904.601.30
5.202.703.901.40
5.002.003.501.00
5.903.004.201.50
6.002.204.001.00
6.102.904.701.40
5.602.903.601.30
6.703.104.401.40
5.603.004.501.50
5.802.704.101.00
6.202.204.501.50
5.602.503.901.10
5.903.204.801.80
6.102.80 4.001.30
6.302.504.901.50
6.102.804.701.20
6.402.904.301.30
6.603.004.401.40
6.802.804.801.40
6.703.005.001.70
6.002.904.501.50
5.702.603.501.00
5.502.403.801.10
5.502.403.701.00
5.802.703.901.20
6.002.705.101.60
5.403.004.501.50
6.003.404.501.60
6.703.104.701.50
6.302.304.401.30
5.603.004.101.30
5.502.504.001.30
5.502.604.401.20
6.103.004.601.40
5.802.604.001.20
5.002.303.301.00
5.602.704.201.30
5.703.004.201.20
5.702.904.201.30
6.202.904.301.30
5.102.503.001.10
5.702.804.101.30
6.303.306.002.50
5.802.705.101.90
7.103.005.902.10
6.302.905.601.80
6.503.005.802.20
7.603.006.602.10
4.902.504.501.70
7.302.906.301.80
6.702.505.801.80
7.203.606.102.50
6.503.205.102.00
6.402.705.301.90
6.803.005.502.10
5.702.505.002.00
5.802.805.102.40
6.403.205.302.30
6.503.005.501.80
7.703.806.702.20
7.702.606.902.30
6.002.205.001.50
6.903.205.702.30
5.602.804.902.00
7.702.806.702.00
6.302.704.901.80
6.703.305.702.10
7.203.206.001.80
6.202.804.801.80
6.103.004.901.80
6.402.805.602.10
7.203.005.801.60
7.402.806.101.90
7.903.806.402.00
6.402.805.602.20
6.302.805.101.50
6.102.605.601.40
7.703.006.102.30
6.303.405.602.40
6.403.105.501.80
6.003.004.801.80
6.903.105.402.10
6.703.105.602.40
6.903.105.102.30
5.802.705.101.90
6.803.205.902.30
6.703.305.702.50
6.703.005.202.30
6.302.505.001.90
6.503.005.202.00
6.203.405.402.30
5.903.005.101.80
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/129071
推荐阅读
相关标签
  

闽ICP备14008679号