图解机器学习第四部分第十四章——谱聚类

n = 500;
c = 2;
k = 10; % 10 均值聚类
t = randperm(n); %打乱 1~500 个随机数
a = linspace(0,2*pi,n/2)';
x = [a.*cos(a) a.*sin(a);(a+pi).*cos(a) (a+pi).*sin(a)];
% 拉普拉斯特征映射法
x=x+rand(n,2);
x=x-repmat(mean(x),[n,1]);
x2= sum(x.^2,2);
d=repmat(x2,1,n)+repmat(x2',n,1)-2*x*x';
[p,i]=sort(d);
W=sparse(d<=ones(n,1)*p(k+1,:));
W=(W+W'~=0);
D=diag(sum(W,2));
L=D-W;
[z,v]=eigs(L,D,c-1,'sm');


m=z(t(1:c),:);
s0(1:c,1)=inf;
z2=sum(z.^2,2);
for o=1:1000
    m2=sum(m.^2,2);
    [u,y]=min(repmat(m2,1,n)+repmat(z2',c,1)-2*m*z');
    for t=1:c
        m(t,:)=mean(z(y==t,:));
        s(t,1)=mean(d(y==t));
    end
    if norm(s-s0)<0.001,break,end
    s0=s;
end

figure(1);
clf;
hold on;
axis([-10 10 -10 10]);
plot(x(y==1,1),x(y==1,2),'bo');
plot(x(y==2,1),x(y==2,2),'rx');

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值