Fuzzy logic in Scilab (sciFLT). Part 4 — Fuzzy c-means clustering

Previous parts:
Part 1
Part 2
Part 3

Let’s generate some quasi-random two-dimensional data.

function Xin =random_data_points(c,rad,npts)
    Xin=[];
    for i=1:length(c(:,1))
        Xin=[Xin;...
        [c(i,1)*ones(npts,1),c(i,2)*ones(npts,1)]+...
        rad*(rand(npts,2)-0.5*ones(npts,2))];
    end
    Xin=unique(Xin,'r');
endfunction

This function takes a two-dimensional array c, where each row contains (x,y) coordinates of some point.

Then it generates npts random points located not farther than rad (i.e. the Manhattan distance ≤ rad) from each point in c.

All the generated data are also organized as a two dimensional array.

Finally, the function random_data_points sorts the data in the ascending order of the elements of the first column.

For example, let’s take four points (2,2), (3,2), (2,3), (3,3), and generate 20 random points around each of those four, within the Manhattan distance 1.

Xin=random_data_points([2,2;3,2;2,3;3,3],1,20)

Thus we have 80 points in total.

clf(0),scf(0);
plot(Xin(:,1),Xin(:,2),'.k')

fcm0

Let’s do data clustering using fuzzy c-means.

[centers,U,ofun,ofunk]=fcmeans(Xin,4,2);

Here we took the number of clusters = 4, and the value of the fuzzifier constant = 2.

The cluster centres are:

disp(centers)

    1.8633479    1.9245908  
    2.8322514    1.9328012  
    1.9943521    2.95792    
    2.9315499    2.9691494  

Then we put each point to a corresponding cluster:

maxU = max(U,'c');
index1 = find(U(:,1) == maxU);
index2 = find(U(:,2) == maxU);
index3 = find(U(:,3) == maxU);
index4 = find(U(:,4) == maxU);

clf(1),scf(1);
plot(Xin(index1,1),Xin(index1,2),'.r')
plot(Xin(index2,1),Xin(index2,2),'.b')
plot(Xin(index3,1),Xin(index3,2),'.g')
plot(Xin(index4,1),Xin(index4,2),'.m')
plot(centers(1,1),centers(1,2),'xr')
plot(centers(2,1),centers(2,2),'xb')
plot(centers(3,1),centers(3,2),'xg')
plot(centers(4,1),centers(4,2),'xm')

fcm1

The centre of each cluster is marked with an ‘x’.

The membership functions of the clusters are:

clf(2),scf(2);
plot(Xin(:,1),U(:,1),'-r')
plot(Xin(index1,1),U(index1,1),'or')
plot(Xin(index2,1),U(index2,1),'ob')
plot(Xin(index3,1),U(index3,1),'og')
plot(Xin(index4,1),U(index4,1),'om')

fcm2

clf(3),scf(3);
plot(Xin(:,1),U(:,2),'-b')
plot(Xin(index1,1),U(index1,2),'or')
plot(Xin(index2,1),U(index2,2),'ob')
plot(Xin(index3,1),U(index3,2),'og')
plot(Xin(index4,1),U(index4,2),'om')

fcm3

clf(4),scf(4);
plot(Xin(:,1),U(:,3),'-g')
plot(Xin(index1,1),U(index1,3),'or')
plot(Xin(index2,1),U(index2,3),'ob')
plot(Xin(index3,1),U(index3,3),'og')
plot(Xin(index4,1),U(index4,3),'om')

fcm4

clf(5),scf(5);
plot(Xin(:,1),U(:,4),'-m')
plot(Xin(index1,1),U(index1,4),'or')
plot(Xin(index2,1),U(index2,4),'ob')
plot(Xin(index3,1),U(index3,4),'og')
plot(Xin(index4,1),U(index4,4),'om')

fcm5

You can check that all membership functions add up to one:

sum(U,'c')
Advertisements
Published in: on 30/10/2013 at 21:19  Leave a Comment  

The URI to TrackBack this entry is: https://burubaxair.wordpress.com/2013/10/30/fuzzy-logic-in-scilab-sciflt-part-4-fuzzy-c-means-clustering/trackback/

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: