首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >将具有多个子聚类均值的数据点分组到指定的K个聚类中的KMM算法。

将具有多个子聚类均值的数据点分组到指定的K个聚类中的KMM算法。

作者头像
裴来凡
发布2022-05-28 15:23:25
发布2022-05-28 15:23:25
3.5K00
代码可运行
举报
运行总次数:0
代码可运行

KMM.m

代码语言:javascript
代码运行次数:0
运行
复制

function [laKMM, laMM, BiGraph, A, OBJ, Ah, laKMMh] = KMM_mmconv(X, c, m, k)
% [laKMM, laMM, BiGraph, Anc, ~, ~, ~]= KMM(X', c, m,k) : K-Multiple-Means
% Input:
%       - X: the data matrix of size nFea x nSmp, where each column is a sample
%               point
%       - c: the number of clusters
%       - m: the number of multiple means(MM)
%       - k: the number of neighbor points
% Output:
%       - laKMM: the cluster assignment for each point
%       - laMM: the sub-cluster assignment for each point
%       - BiGraph: the matrix of size nSmp x nMM
%       - A: the multiple means matrix of size nFea x nMM
%       - Ah: the history of multiple means
%       - laKMMh: the history of cluster assignment for each point
% Requre:
%       CSBG.m
%     meanInd.m
%     ConstructA_NP.m
%     EProjSimplex_new.m
%     svd2uv.m
%     struG2la.m
%       eig1.m
% Usage:
%       % X: d*n
%       [laKMM, laMM, AnchorGraph, Anchors, ~, ~, ~]= KMM(X', c, m,k) ;
% Reference:
%
%  Feiping Nie, Cheng-Long Wang, Xuelong Li, "K-Multiple-Means: A Multiple-Means 
%   Clustering Method with Specified K Clusters," In The 25th ACM SIGKDD Conference
%   on Knowledge Discovery and Data Mining (KDD 鈥�19), August 4鈥�8, 2019, Anchorage, AK, USA.
%
%   version 1.0 --May./2019 
%
%   Written by Cheng-Long Wang (ch.l.w.reason AT gmail.com)
if nargin < 4
    if m<6
        k=c-1;
    else
        k=5;
    end      
end
Ah=[];
laKMMh=[];
Iter=15;
OBJ=[];
n=size(X,2);
method=1; % method for initial seeds, 1:kmeans; 0:random 
opt_conv=1; % option for convergence, 1:sub prototypes; 0:partiton of subclusters
% StartIndZ: before MM update
if method ==0
    StartIndZ=randsrc(n,1,1:m);
else
    StartIndZ=kmeans(X',m);
end
BiGraph = ones(n,m);
A = meanInd(X, StartIndZ,m,BiGraph);
[laKMM, laMM, BiGraph, isCov, obj, ~] = CSBG(X, c, A, k);
% fprintf('time:%d,obj:%d\n',ti,obj)
iter=1;
while(iter<Iter)
    iter = iter +1;
    if isCov
        laKMMh=[laKMMh laKMM];
        Ah=[Ah A];
        OBJ=[OBJ obj];
        if opt_conv==1
            StartIndZ=laMM; 
            A_old = A;
            A = meanInd(X, StartIndZ, m, BiGraph);
            Dis = sqdist(A_old,A); % O(ndm)
            distXt = Dis;
            di = min(distXt, [], 2);  
            if norm(di)<1e-4
                fprintf('means converge\n')
                return;
            end
        else            
            if (all(StartIndZ==laMM))
                fprintf('partition converge\n')
                return;
            else
                StartIndZ=laMM; 
                A = meanInd(X, StartIndZ,m,BiGraph);
            end                   
        end
        [laKMM, laMM, BiGraph, isCov, obj, ~] = CSBG(X, c, A, k);   
    else
        if method ==0
            StartIndZ=randsrc(n,1,1:m);
        else
            StartIndZ=kmeans(X',m);
        end
        BiGraph = ones(n,m);
        A = meanInd(X, StartIndZ,m,BiGraph);
        [laKMM, laMM, BiGraph, isCov, obj, ~] = CSBG(X, c, A, k);
    end
fprintf('loop:%d\n',iter)
end

test_KMM_toy.m

代码语言:javascript
代码运行次数:0
运行
复制
% clc;
close all;

folder_now = pwd;  addpath([folder_now, '\funs']);

n=1000; [X, y] = face_gen(n, 0.1);c=4;   
m=floor(sqrt(n*c));k=5; fig = 1;       

tic
[laKMM,~,~,A,~,Ah,laKMMh ]= KMM(X', c, m,k) ;
toc;
result_KMM = ClusteringMeasure(y, laKMM);
if ~fig
    figure('name','KMM')
    rl = randperm(c);
    cm = colormap(jet(c+2));
    for i=1:c
        plot(X(laKMM==rl(i),1),X(laKMM==rl(i),2),'*', 'color', cm(i,:),'MarkerSize',4); hold on;
    end
    plot(A(1,:),A(2,:),'o','MarkerFaceColor', cm(c+2,:),'MarkerEdgeColor',0.3*cm(c+2,:),'MarkerSize',5); hold on;
end
% 
rl = randperm(c);
for j=1:(size(Ah,2)/m)
    A2=Ah(:,(m*(j-1)+1):(m*j));
        if fig
        figure('name','KMM')
        cm = colormap(jet(c+2));
        la=laKMMh(:,j);
        for i=1:c
            plot(X(la==rl(i),1),X(la==rl(i),2),'*', 'color', cm(i,:),'MarkerSize',4); hold on;
        end
        plot(A2(1,:),A2(2,:),'o','MarkerFaceColor', 'r','MarkerEdgeColor',0.3*cm(c+2,:),'MarkerSize',5); hold on;
        end
end
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 图像处理与模式识别研究所 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档