设计网站建设书南昌大学论文,用asp做网站需要的软件,怎样给网站做seo优化,做外贸网站怎么设计目录
1.简介
2.算法原理
3.实例分析
3.1 读取数据
3.2 原理推导K均值过程
3.3 自带kmeans函数求解过程
完整代码 1.简介 聚类是一个将数据集中在某些方面相似的数据成员进行分类组织的过程#xff0c;聚类就是一种发现这种内在结构的技术#xff0c;聚类技术经常被称为…目录
1.简介
2.算法原理
3.实例分析
3.1 读取数据
3.2 原理推导K均值过程
3.3 自带kmeans函数求解过程
完整代码 1.简介 聚类是一个将数据集中在某些方面相似的数据成员进行分类组织的过程聚类就是一种发现这种内在结构的技术聚类技术经常被称为无监督学习。 K均值聚类是最著名的划分聚类算法由于简洁和效率使得他成为所有聚类算法中最广泛使用的。给定一个数据点集合和需要的聚类数目KK由用户指定K均值算法根据某个距离函数反复把数据分入K个聚类中。
2.算法原理 K-means算法是典型的基于距离的聚类算法采用距离作为相似性的评价指标即认为两个对象的距离越近其相似度就越大。该算法认为簇是由距离靠近的对象组成的因此把得到紧凑且独立的簇作为最终目标。
K-mean算法步骤如下
1随机选取K个样本为中⼼
2分别计算所有样本到随机选取的K个中⼼的距离
3样本离哪个中⼼近就被分到哪个中⼼
4计算各个中⼼样本的均值最简单的⽅法就是求样本每个维度的平均值作为新的中心
5重复234直到新的中⼼和原来的中⼼基本不变化的时候算法结束
3.实例分析 数据来源于统计年鉴
从数据中我们可以看到实际数据是被分为三类的。
3.1 读取数据
dataxlsread(D:\桌面\kmeans.xlsx)
返回 在这里我们看到xlsread读取数据时没有读取变量名但序号也被加进去了接下来我们需要将其剔除
datadata(:,2:7)
返回 3.2 原理推导K均值过程
%% 原理推导K均值
[m,n]size(data); %读取数据的行数与列数
cluster_num3; %自定义分类数
clusterdata(randperm(m,cluster_num),:);
epoch_max1000;%最大次数
therad_lim0.001;%中心变化阈值
epoch_num0;
while(epoch_numepoch_max)epoch_numepoch_num1;for i1:cluster_numdistance(data-repmat(cluster(i,:),m,1)).^2;distance1(:,i)sqrt(sum(distance));end[~,index_cluster]min(distance1);for j1:cluster_numcluster_new(j,:)mean(data(find(index_clusterj),:));endif (sqrt(sum((cluster_new-cluster).^2))therad_lim)clustercluster_new;elsebreak;end
end
%% 画出聚类效果
figure(2)
subplot(2,1,1)
aunique(index_cluster); %找出分类出的个数
Ccell(1,length(a));
for i1:length(a)C(1,i){find(index_clustera(i))};
end
for j1:cluster_numdata_getdata(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,filled,MarkerFaceAlpha,.6,MarkerEdgeAlpha,.9);hold on
end
plot(cluster(:,1),cluster(:,2),kd,LineWidth,2);
hold on
sc_tmean(silhouette(data,index_cluster));
title_str[原理推导K均值聚类, 聚类数为,num2str(cluster_num), SC轮廓系数:,num2str(sc_t)];
title(title_str)
返回 3.3 自带kmeans函数求解过程
%% MATLAB自带kmeans函数
subplot(2,1,2) %画子图在这里是一图上可画两张子图
cluster_num3; %自定义分类数
[index_km,center_km]kmeans(data,cluster_num) ;%MATLAB自带kmeans函数
aunique(index_km); %找出分类出的个数
Ccell(1,length(a));
for i1:length(a)C(1,i){find(index_kma(i))};
end
for j1:cluster_numdata_getdata(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,filled,MarkerFaceAlpha,.6,MarkerEdgeAlpha,.9);hold on
end
plot(center_km(:,1),center_km(:,2),kd,LineWidth,2);
hold on
sc_kmean(silhouette(data,index_km));
title_str1[MATLAB自带kmeans函数, 聚类数为,num2str(cluster_num), SC轮廓系数:,num2str(sc_k)];
title(title_str1)
返回结果如下 完整代码
clear;clc;
dataxlsread(D:\桌面\kmeans.xlsx)
datadata(:,2:7)
%% 原理推导K均值
[m,n]size(data); %读取数据的行数与列数
cluster_num3; %自定义分类数
clusterdata(randperm(m,cluster_num),:);
epoch_max1000;%最大次数
therad_lim0.001;%中心变化阈值
epoch_num0;
while(epoch_numepoch_max)epoch_numepoch_num1;for i1:cluster_numdistance(data-repmat(cluster(i,:),m,1)).^2;distance1(:,i)sqrt(sum(distance));end[~,index_cluster]min(distance1);for j1:cluster_numcluster_new(j,:)mean(data(find(index_clusterj),:));endif (sqrt(sum((cluster_new-cluster).^2))therad_lim)clustercluster_new;elsebreak;end
end
%% 画出聚类效果
figure
subplot(2,1,1) %画子图在这里是一图上可画两张子图
aunique(index_cluster); %找出分类出的个数
Ccell(1,length(a));
for i1:length(a)C(1,i){find(index_clustera(i))};
end
for j1:cluster_numdata_getdata(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,filled,MarkerFaceAlpha,.6,MarkerEdgeAlpha,.9);hold on
end
plot(cluster(:,1),cluster(:,2),kd,LineWidth,2);
hold on
sc_tmean(silhouette(data,index_cluster));
title_str[原理推导K均值聚类, 聚类数为,num2str(cluster_num), SC轮廓系数:,num2str(sc_t)];
title(title_str)%% MATLAB自带kmeans函数
subplot(2,1,2) %画子图在这里是一图上可画两张子图
cluster_num3; %自定义分类数
[index_km,center_km]kmeans(data,cluster_num) ;%MATLAB自带kmeans函数
aunique(index_km); %找出分类出的个数
Ccell(1,length(a));
for i1:length(a)C(1,i){find(index_kma(i))};
end
for j1:cluster_numdata_getdata(C{1,j},:);scatter(data_get(:,1),data_get(:,2),100,filled,MarkerFaceAlpha,.6,MarkerEdgeAlpha,.9);hold on
end
plot(center_km(:,1),center_km(:,2),kd,LineWidth,2);
hold on
sc_kmean(silhouette(data,index_km));
title_str1[MATLAB自带kmeans函数, 聚类数为,num2str(cluster_num), SC轮廓系数:,num2str(sc_k)];
title(title_str1)
返回 每次返回结果也不尽相同原理推导的和自带的函数的求解结果也相差不是很大但与原始数据的分类相比较还是有一定差距