[Home]


LFDC

A Demo Matlab code for Locally finite distance clustering with discriminative information for Clustering problem. (You could Right-Click [Code and Data], and Save, then you can download the whole matlab code and artificial dataset.)


Reference

Yi-Fan Qi, Yuan-Hai Shao, Chun-Na Li*, Yan-Ru Guo. Locally finite distance clustering with discriminative information. (Submitted)


Main Function

No additional functions are required.

function [pY] = LFDC(X, YIni, dim) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % LFDC: Locally finite distance clustering with discriminative information for Clustering problem % % pY = LFDC(X, YIni, dim) % % Input: % X - Data matrix. % Each row vector of fea is a data point. % % YIni - Random initial label for dataset X. % Category labels are 1,2,3... , said. % % dim - The dimension that needs to be reduced. % that can be set: % The lowest one dimension. % The highest is the number of features of data X. % % Output: % pY - Prediction label for data X. % % % Examples: % % x1 = -2: 0.05: 2; % y1 = 0*ones(1, length(x1)); % y = -5: 0.05: 5; % x2 = -5*ones(1, length(y)); % x3 = 5*ones(1, length(y)); % data1 = [x1', y1']; % data2 = [x2', y']; % data3 = [x3', y']; % sj1 = randn(length(y1), 2); % sj2 = randn(length(y), 2); % sj3 = randn(length(y), 2); % data1_end = data1+0.2*sj1; % data2_end = data2+0.2*sj2; % data3_end = data3+0.2*sj3; % X = [data1_end;data2_end;data3_end]; % Y = [ones(length(data1_end), 1); 2*ones(length(data2_end), 1); 3*ones(length(data3_end), 1)]; % k = 3; dim = 1; % YIni = Initialization(X, k, 1); % [pY] = LFDC(X, YIni, dim) % % Reference: % Yi-Fan Qi, Yuan-Hai Shao, Chun-Na Li*, Yan-Ru Guo. Locally finite distance clustering with discriminative information. Submitted 2021 % % September 1, 2021 % % Written by Yi-Fan Qi (yifanqicoco@163.com) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Initailization %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tic; Maxite = 4; X = X'; flag = 0; outite = 0; classLabel = unique(YIni); nClass = max(YIni); % nClass = length(classLabel); [nFea,nSmp] = size(X); pY = YIni; u = 1; MaxIter = 30; epsilon1 = 1; epsilon2 = 1; delta = 10^-6; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % To predict %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% while flag == 0 && outite < Maxite outite = outite+1; tY = pY; %% m_Cls = zeros(nClass,1); idxCls = cell(nClass,1); M = mean(X,2); MCls = zeros(nFea,nClass); HB = zeros(nFea,nClass); HW = zeros(nFea,nSmp); WallCls = cell(length(nClass)); for k = 1:nClass disc = ismember(k,unique(tY)); if disc ~= 1 continue end W = eye(nFea,nFea); idxCls{k} = find(tY==classLabel(k)); m_Cls(k) = length(idxCls{k}); MCls(:,k) = mean(X(:,idxCls{k}),2); HB(:,k) = sqrt(m_Cls(k))*(MCls(:,k) - M); % HB(:,k) = m_Cls(k)*(MCls(:,k) - M); HW(:,idxCls{k}) = X(:,idxCls{k}) - MCls(:,k); for i = 1:MaxIter S1k = zeros(nFea,nFea); S2k = zeros(nFea,nFea); A = []; B = []; for l = 1:m_Cls(k) if norm(W'*HW(:,l),2) <= epsilon1 Ind1(l) = 1; else Ind1(l) = 0; end A = [A,norm(W'*HW(:,l),2)]; Fk(l) = Ind1(l)/(norm(W'*HW(:,l),2) + delta); end S1k = HW(:,idxCls{k})*diag(Fk(l))*HW(:,idxCls{k})'; if norm(sqrt(m_Cls(k))*W'*HB(:,k),2) <= epsilon2 % if norm(m_Cls(k)*W'*HB(:,k),2) <= epsilon2 Ind2(k) = 1; else Ind2(k) = 0; end B = [B,norm(sqrt(m_Cls(k))*W'*HB(:,k),2)]; Gk = Ind2(k)/(norm(sqrt(m_Cls(k))*W'*HB(:,k),2) + delta); % B = [B,norm(m_Cls(k)*W'*HB(:,k),2)]; % Gk = Ind2(k)/(norm(m_Cls(k)*W'*HB(:,k),2) + delta); S2k = HB(:,k)*diag(Gk)*HB(:,k)'; Sk = S1k - u * S2k; [W_best,D] = eig(Sk); % W_best = orth(W_best); D = diag(D); % eigIdx1 = find(D < 1e-5); % eigIdx = [eigIdx1]; % D(eigIdx) = []; % W_best(:,eigIdx) = []; [~, index] = sort(D); W_best = W_best(:,index); D = D(index); % W_best = W_best(:,dim); W_best = W_best(:,1:min(dim,length(index))); if abs(norm(W) - norm(W_best)) == 0 W = W_best; break; end W = W_best; A = sort(A); B = sort(B); epsilon1 = A(:,round(length(A)*0.9)); epsilon2 = B(:,round(length(B)*0.9)); % epsilon1 = 1; % epsilon2 = 1; i = i + 1; end WallCls{k} = W; % preparation for predicting for l = 1:nSmp Dist_k(k,l) = norm(WallCls{k}'*(X(:,l)-MCls(:,k)),2)^2; end end [~,pY] = min(Dist_k); pY = pY'; if getAC(pY,tY) > 0.9999 flag = 1; end t = toc; end end
Contacts


Any question or advice please email to na1013na@163.com or yifanqicoco@163.com