Matlab simulation of fuzzy clustering

1. Problem description:

 Fuzzy cluster analysis is a mathematical method that uses fuzzy mathematical language to describe and classify things according to certain requirements. [1]   Fuzzy cluster analysis generally refers to constructing a fuzzy matrix according to the attributes of the research object itself , and on this basis, to determine the clustering relationship according to a certain degree of membership , that is, to use fuzzy mathematics to determine the fuzzy relationship between samples Quantitative determination, so as to objectively and accurately perform clustering . Clustering is to divide the data set into multiple classes or clusters , so that the data difference between each class should be as large as possible, and the data difference between the classes should be as small as possible, that is, "minimize the similarity between classes and maximize Principle of similarity within class

2. Part of the program:

 

%Fuzzy c-means clustering FCM algorithm MATLAB code 

The MATLAB code of the two iterative forms of the %FCM algorithm: m file 1/7:
function [U,P,Dist,Cluster_Res,Obj_Fcn,iter]=fuzzycm(Data,C,plotflag,M,epsm)
% Fuzzy C mean aggregation Class FCM: Iterate from the random initialization of the division matrix
% [U,P,Dist,Cluster_Res,Obj_Fcn,iter] = fuzzycm(Data,C,plotflag,M,epsm)
% Input:
% Data: N×S matrix, poly The original data of the class, that is, a limited set of observation samples,
each row of% Data is the feature vector of an observation sample, S is
the dimension of the feature vector %, and N is the number of sample points
% C: number of clusters, 1<C<N
% plotflag: 2D/3D plot flag for clustering results, 0 means no plot, default value        
% M: weighted index, default value 2
% epsm: iterative stop threshold of FCM algorithm, default value 1.0e-6
% output:
% U: C×N type matrix, FCM partition matrix
% P: C×S type matrix, FCM cluster center, each row corresponds to a cluster prototype
% Dist: C×N type Matrix, the distance from each cluster center of FCM to each sample point, the distance from the
center i of the cluster center to the sample point j is Dist(i,j)
% Cluster_Res: clustering results, a total of C rows, each row corresponds to a category
% Obj_Fcn: objective function value
% iter: the number of iterations of the FCM algorithm
% See also: fuzzydist maxrowf fcmplot
if nargin<5
    epsm=1.0e-6; 
end
if nargin <4
    M=2;
end
if nargin<3
    plotflag=0;
end
[N,S]=size(Data);m=2/(M-1);iter=0;
Dist(C,N)=0; U(C,N)=0; P(C,S)=0;
% Initialize the partition matrix randomly
U0 = rand(C,N); 
U0=U0./(ones(C,1)*sum(U0)) ;
% FCM iterative algorithm
while true 
    % iterative counter
    iter=iter+1; 
    % calculate or update the cluster center P
    Um=U0.^M;
    P=Um*Data./(ones(S,1)*sum(Um '))';   
    % Update the partition matrix U
    for i=1:C
        for j=1:N
            Dist(i,j)=fuzzydist(P(i,:),Data(j,:));
        end
    end         
    U=1./(Dist.^m.*(ones(C,1 )*sum(Dist.^(-m))));          
    % Objective function value: within-class weighted squared error sum
    if nargout>4 | plotflag
        Obj_Fcn(iter)=sum(sum(Um.*Dist.^2)) ;
    end
    % FCM algorithm iteration stop condition
    if norm(U-U0,Inf)<epsm
        break
    end
    U0=U;   
end
% clustering result
if nargout> 3
    res = maxrowf(U);
    for c = 1:C
        v = find (res==c);
        Cluster_Res(c,1:length(v))=v;
    end
end
% plot
if plotflag
    fcmplot(Data,U,P,Obj_Fcn);
end
% m file 2/7:
function [U,P,Dist,Cluster_Res,Obj_Fcn,iter]=fuzzycm2(Data,P0,plotflag,M,epsm)
% Fuzzy C mean Clustering FCM: Iterate from the specified initial cluster center
% [U,P,Dist,Cluster_Res,Obj_Fcn,iter] = fuzzycm2(Data,P0,plotflag,M,epsm)
% Input: Data,plotflag,M,epsm: See fuzzycm.m
% P0: Initial cluster center
% Output: U,P,Dist,Cluster_Res,Obj_Fcn,iter: See fuzzycm.m    
% See also: fuzzycm
if nargin<5
    epsm=1.0e-6; 
end
if nargin< 4
    M=2;
end
if nargin<3
    plotflag=0;
end
[N,S] = size(Data); m = 2/(M-1); iter = 0;
C=size(P0,1);Dist (C,N)=0;U(C,N)=0;P(C,S)=0;
% FCM iterative algorithm
while true 
    % Iteration counter
    iter=iter+1; 
    % Calculate or update the partition matrix U
    for i=1:C
        for j=1:N
            Dist(i,j)=fuzzydist(P0(i,:),Data(j, :));
        end
    end         
    U=1./(Dist.^m.*(ones(C,1)*sum(Dist.^(-m))));      
    % update cluster center P
    Um=U.^ M;
    P=Um*Data./(ones(S,1)*sum(Um'))';   
    % objective function value: within-class weighted squared error sum
    if nargout>4 | plotflag
        Obj_Fcn(iter)=sum(sum (Um.*Dist.^2));
    end
    % FCM algorithm iteration stop condition
    if norm(P-P0,Inf)<epsm
        break
    end
    P0=P;
end
% clustering result
if nargout> 3
    res = maxrowf(U);
    for c = 1:C
        v = find(res==c);
        Cluster_Res(c,1:length(v))=v;
    end
end
% plot
if plotflag
    fcmplot(Data,U,P ,Obj_Fcn);
end
% m file 3/7:
function fcmplot(Data,U,P,Obj_Fcn)
% FCM result plotting function
% See also: fuzzycm maxrowf ellipse
[C,S] = size(P); res = maxrowf( U);
str ='po*x+d^v><.h'; 
% objective function plot
figure(1),plot(Obj_Fcn)
title('objective function value change curve','fontsize',8)
% 2D Plot
if S==2 
    figure(2),plot(P(:,1),P(:,2),'rs'),hold on
    for i=1:C
        v=Data(find(res==i ),:); 
        plot(v(:,1),v(:,2),str(rem(i,12)+1))      
        ellipse(max(v(:,1))-min(v(:,1)), ...
                max(v(:,2))-min(v(:,2)), ...
                [max(v(:,1))+min(v(:,1)), ...
                max(v(:,2))+min(v(:,2))]/2,'r:')    
    end
    grid on,title('2D 聚类结果图','fontsize',8),hold off
end
% 3D 绘图
if S>2 
    figure(2),plot3(P(:,1),P(:,2),P(:,3),'rs'),hold on
    for i=1:C
        v=Data(find(res==i),:);
        plot3(v(:,1),v(:,2),v(:,3),str(rem(i,12)+1))      
        ellipse(max(v(:,1))-min(v(:,1)), ...
                max(v(:,2))-min(v(:,2)), ...
                [max(v(:,1))+min(v(:,1)), ...
                max(v(:,2))+min(v(:,2)))/2, ...
                'r:',(max(v(:,3))+min(v(:,3) ))/2)   
    end
    grid on, title('3D clustering result graph','fontsize',8),hold off
end
%% m file 4/7:
function D=fuzzydist(A,B)
% fuzzy clustering Analysis: The distance between samples
% D = fuzzydist(A,B)
D=norm(AB);
%% m File 5/7:
function mr=maxrowf(U,c)
% Find the location of the c-th largest element in each column of matrix U OK, the default value of c is 1
% Call format: mr = maxrowf(U,c)
% See also: addr
if nargin<2
    c=1;
end
N=size(U,2); mr(1,N) =0;
for j=1:N
    aj=addr(U(:,j),'descend');
    mr(j)=aj(c);
end
%% m File 6/7:
function ellipse(a,b ,center,style,c_3d)
% Draw an ellipse
% Call: ellipse(a,b,center,style,c_3d)
% Input:
% a: The axis length of the ellipse (parallel to the x axis)
% b: The axis length of the ellipse (parallel to the y axis)
% center : The center of the ellipse [x0,y0], the default value is [0,0]
% style: The drawn line type and color, the default value is a solid line blue
% c_3d: The center of the ellipse is on the z axis in 3D space Coordinates, can be defaulted
if nargin<4
    style='b';
end
if nargin<3 | isempty(center)
    center=[0,0];
end
t=1:360;
x=a/2*cosd(t) +center(1);
y=b/2*sind(t)+center(2);
if nargin>4
    plot3(x,y,ones(1,360)*c_3d,style)
else
    plot(x,y,style)
end
%%m file 7/7:
function f = addr(a,strsort)
% returns the index of each component in the original vector after the vector is arranged in ascending or descending order
% 函数调用:f = addr(a,strsort)
% strsort: 'ascend' or 'descend'
%          default is 'ascend'
% -------- example --------
% addr([ 4 5 1 2 ]) returns ans:
%       [ 3   4   1   2 ]
if nargin==1
    strsort='ascend';
end
sa=sort(a); ca=a;
la=length(a);f(la)=0;
for i=1:la
    f(i)=find(ca==sa(i),1);
    ca(f(i))=NaN;
end
if strcmp(strsort,'descend')
    f=fliplr(f);
end

3. Simulation conclusion:

D19

Guess you like

Origin blog.csdn.net/ccsss22/article/details/115175643