hi
today we are going to see how to cluster
your data
when working with data it may happen to
have the necessity to group a set of
data given a certain criterion
whatever is the method the process of
defining groups of items is called
clustering
today we are going to see a very common
technique of clustering
k-means clustering
the idea behind key means is very simple
in step 1 the algorithm randomly selects
k points
as cluster centers where k
is the number of clusters you want to
find
in step 2 it assigns samples to the
nearest cluster center according to
euclidean distance
in step 3 the algorithm calculates the
new centroid of all objects in each
cluster and repeats step 2 and 3 until
the same points are assigned to each
cluster in consecutive rounds
but for a real case example
let's jump to matlab
we can start by loading our dataset.math
file which contains our dataset then by
using the scatter free function
we are able to quickly visualize our
data
how many clusters do you see
well for now let's suppose
that there are four clusters
to use the k-means algorithm you need to
know a priori the number of clusters in
your data
by adjusting
this slider we can set the value of k
in our case we will set k equal to four
we can finally call the matlab function
key means that implements the algorithm
the most important arguments are three
our data x
the value k
and the method for choosing the initial
cluster centered positions
in our case we will randomly sample
key points from our data set
this initialization is chosen with the
name value pair start sample
now let's call the matlab function
k-means
and see what happens
we can also visualize
the data
using the scatter free function
we have seen before
are you happy with the clustering
if not you can always change the value
of k
it may happen that you have no idea
about how many clusters there are
this is especially true with varied
dimensional data sets
fortunately there exists different
algorithms that may help you in finding
the optimal value of k
for example we can try with the kalinski
arabs criterion which assigns a score to
each possible value of k
the kalinski arabs score
is defined as ratio between the within
cluster dispersion and the between
cluster dispersion
the optimal number of clusters is the
one associated with the is value
in our code we first run key means with
different values of k
in our case with k that goes from 1 to
9.
then the eval cluster function
is used
to evaluate the clustering solutions
we can specify the kalinskiribus method
in this function to find the optimal
value of k
but of course there exists other methods
that matlab implements
now we can run the code
as you can see the optimal value of k
is with k equal to 5.
and here you can see the results
with k equal to five
k means is just one of the many
algorithms that can be used for
clustering
each one of them adopts a different
grouping strategy for example you may
find interesting methods like dbscan
which searches for clusters
based on the density of the data