请高手帮忙编写关于K—mediods算法的MATLAB程序用于处理Iris数据集的聚类处理,得到迭代次数图形和准确率
答案:1 悬赏:0 手机版
解决时间 2021-04-07 09:54
- 提问者网友:孤凫
- 2021-04-07 02:12
请高手帮忙编写关于K—mediods算法的MATLAB程序用于处理Iris数据集的聚类处理,得到迭代次数图形和准确率
最佳答案
- 五星知识达人网友:未来江山和你
- 2021-04-07 02:28
我把K-mediods的matlab代码贴出来,你好好学习一下
function label = kmedoids( data,k,start_data )
% kmedoids k中心点算法函数
% data 待聚类的数据集,每一行是一个样本数据点
% k 聚类个数
% start_data 聚类初始中心值,每一行为一个中心点,有cluster_n行
% class_idx 聚类结果,每个样本点标记的类别
% 初始化变量
n = length(data);
dist_temp1 = zeros(n,k);
dist_temp2 = zeros(n,k);
last = zeros(n,1);
a = 0;
b = 0;
if nargin==3
centroid = start_data;
else
centroid = data(randsample(n,k),:);
end
for a = 1:k
temp1 = ones(n,1)*centroid(a,:);
dist_temp1(:,a) = sum((data-temp1).^2,2);
end
[~,label] = min(dist_temp1,[],2);
while any(label~=last)
for a = 1:k
temp2 = ones(numel(data(label==a)),1);
temp3 = data(label==a);
for b = 1:n
temp4 = temp2*data(b,:);
temp5 = sum((temp3-temp4).^2,2);
dist_temp2(b,a) = sum(temp5,1);
end
end
[~,centry_indx] = min(dist_temp2,[],1);
last = label;
centroid = data(centry_indx,:);
for a = 1:k
temp1 = ones(n,1)*centroid(a,:);
dist_temp1(:,a) = sum((data-temp1).^2,2);
end
[~,label] = min(dist_temp1,[],2);
end
end
function label = kmedoids( data,k,start_data )
% kmedoids k中心点算法函数
% data 待聚类的数据集,每一行是一个样本数据点
% k 聚类个数
% start_data 聚类初始中心值,每一行为一个中心点,有cluster_n行
% class_idx 聚类结果,每个样本点标记的类别
% 初始化变量
n = length(data);
dist_temp1 = zeros(n,k);
dist_temp2 = zeros(n,k);
last = zeros(n,1);
a = 0;
b = 0;
if nargin==3
centroid = start_data;
else
centroid = data(randsample(n,k),:);
end
for a = 1:k
temp1 = ones(n,1)*centroid(a,:);
dist_temp1(:,a) = sum((data-temp1).^2,2);
end
[~,label] = min(dist_temp1,[],2);
while any(label~=last)
for a = 1:k
temp2 = ones(numel(data(label==a)),1);
temp3 = data(label==a);
for b = 1:n
temp4 = temp2*data(b,:);
temp5 = sum((temp3-temp4).^2,2);
dist_temp2(b,a) = sum(temp5,1);
end
end
[~,centry_indx] = min(dist_temp2,[],1);
last = label;
centroid = data(centry_indx,:);
for a = 1:k
temp1 = ones(n,1)*centroid(a,:);
dist_temp1(:,a) = sum((data-temp1).^2,2);
end
[~,label] = min(dist_temp1,[],2);
end
end
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯