Fuzzy c-mean clustering algorithm (principle +matlab code)

Source: Internet
Author: User




FCM Code

Function[center,u,obj_fun]=fcmcluster (data,n,options)% uses the fuzzy C-means to divide data into N-class% usage% 1 [center,u,obj_fcn]=fcmcluster (
Data,n,options);

% 2 [Center,u,obj_fcn]=fcmcluster (Data,n); % input% data n*m matrix, n samples, each sample has a dimension of M% n category% options 4*1 matrix% Options (1): Weighted index of Membership matrix U% Options (2): Max iterations% o Ptions (3): Minimum degree of membership change, iteration termination condition% options (4): Output information flag per iteration output% Center cluster center% U Membership matrix% obj_fun Target function value if Nargi
n~=2 && nargin~=3 Error (' Too many or Too few input arguments ');
End Data_n=size (data,1);

In_n=size (data,2);

% default parameter default_options=[2;100;1e-5;1];
  % parameter configuration% if only the first two parameters are selected, the default parameters are chosen, and if the number of parameters is less than 4, the other default parameters are selected if nargin==2 options=default_options;
           else if Length (options) <4 tmp=default_options;
           TMP (1:length (options)) =options;
       options=tmp;
       End Nan_index=find (isNaN (options) ==1);

       Options (Nan_index) =default_options (Nan_index); If options (1) <=1 error (' The exponent should be greater than1! ');
  End end% assigns the components in the options to four variables expo=options (1) respectively;
  Max_iter=options (2);
  Min_impro=options (3);

  Display=options (4);

  Obj_fun=zeros (max_iter,1);


  % initialization fuzzy distribution matrix U=INITFCM (N,data_n);
       % main program for I=1:max_iter [U,center,obj_fun (i)]=STEPFCM (Data,u,n,expo);
       If Display fprintf (' fcm:iteration count=%d,obj_fun=%f\n ', I,obj_fun (i));
           End% termination condition discriminant if i>1 if ABS (Obj_fun (i)-obj_fun (i-1)) <min_impro break;
   End End end Iter_n=i;
Obj_fun (Iter_n+1:max_iter) =[];
        % end percent of the sub function Fuzzy matrix initialization function u= INITFCM (n,data_n) U=rand (N,data_n);
        Col_sum=sum (U);
    U=u./col_sum (Ones (n,1),:);
            End-of-a-percent sub-function stepwise clustering functions [U_NEW,CENTER,OBJ_FUN]=STEPFCM (Data,u,n,expo) Mf=u.^expo;
            Center=mf*data./((Ones (Size (data,2), 1) *sum (MF '));
            DIST=DISTFCM (Center,data);
            Obj_fun=sum (sum ((dist.^2). *MF)); Tmp=dist.^ ( -2/(expo-1));
        U_new=tmp./(Ones (n,1) *sum (TMP));
                The end-of-count sub-function calculates the distance function OUT=DISTFCM (center,data) out=zeros (Size (center,1), size (data,1)); For K=1:size (center,1) out (k,:) =sqrt (Sum ((Data-ones (Size (data,1), 1) *center (k,:)). ^2) ', 1


                ));
 End END Algorithm Test

Data=rand (100,2);
[CENTER,U,OBJ_FCN] = Fcmcluster (data,2);
Plot (data (:, 1), Data (:, 2), ' O ');
Hold on;
Index1=find (U (1,:) ==max (u)),% find Data index divided into first class
Index2=find (U (2,:) ==max (u)),% find Data index divided into second class
Plot (data (index1,1), data (index1,2), ' g* ');
Hold on;
Plot (data (index2,1), data (index2,2), ' r* ');
Hold on;
Plot ([Center ([1 2],1)],[center ([1 2],2)], ' * ', ' color ', ' k ');

Experimental results show:
Original data distribution

Clustering results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.