Anda di halaman 1dari 16

# Cluster Analysis

K-Means Clustering
By
Subhasis Dasgupta
Asst Professor

Concept of Clustering
If there are N data points available and they are to be divided into K
number of groups, the best way to do this is to put similar data points
within a group and dissimilar points into other groups
There are many algorithms available to achieve this goal but partitioning
algorithms are the most preferred ones
In partitioning algorithms, the entire dataset is partitioned among K
groups to make sure that objects within a cluster are similar to each
other based on some objective function
In this section, we will focus on the very basic partitioning algorithm, i.e.
K-Means clustering

K-Means Clustering
The k-means algorithm is an algorithm to cluster n objects based on
attributes into k partitions, where
k<n
It assumes that the object attributes form a vector space
It is an algorithm for partitioning (or clustering) N data points into K disjoint
subsets Sj so as to minimize the within cluster sum-of-squares value.
Mathematically, within sum of square is calculated as:

where xn is a vector representing the the nth data point and uj is the
geometric centroid of the data points in Sj

## Simply speaking k-means clustering is an algorithm to group the objects

based on attributes/features into K number of group

## K-Means Clustering Algorithm

Step 1: Begin with a decision on the value of k = number of clusters
Step 2: Put any initial partition that classifies the data into k clusters. You
may assign the training samples randomly,or systematically as the
following
Take the first k training sample as singleelement clusters
Assign each of the remaining (N-k) training sample to the cluster
with the nearest centroid. After each assignment, recompute the
centroid of the gaining cluster

## K-Means Clustering Algorithm

Step 3: Take each sample in sequence and compute its distance from the
centroid of each of the clusters. If a sample is not currently in the cluster
with the closest centroid, switch this sample to that cluster and update the
centroid of the cluster gaining the new sample and the cluster losing the
sample
Step 4 . Repeat step 3 until convergence is achieved, that is until a pass
through the training sample causes no new assignments

## K-Means Clustering Example (K=2)

Let us consider the tiny dataset given below
Individua
l

Variable
1

Variable
2

1.0

1.0

1.5

2.0

3.0

4.0

5.0

7.0

3.5

5.0

4.5

5.0

3.5

4.5

centroids

Individua
l

Variable
1

Variable
2

1.0

1.0

1.5

2.0

3.0

4.0

5.0

7.0

3.5

5.0

4.5

3.5

Starting
4.5

{1,2,3} forms
Cluster 1 and
{4,5,6,7} forms
Cluster 2
Lower Values
form cluster

Individua
l

Centroid
1 Dist

Centroid
2 Dist

0.0

7.21

2 (1.5,2.0)

1.12

6.10

3.61

3.61

7.21

0.0

4.72

2.5

5.31

2.06

4.30

2.93

## K-Means Clustering Example (K=2)

Individual 3 will
move from Cluster 1
to Cluster 2
Thus {1,2} will form
Cluster 1 and
{3,4,5,6,7} will form
Cluster 2

Individua
l

Centroid
1 Dist

Centroid
2 Dist

1.57

7.21

2 (1.5,2.0)

0.47

6.10

2.04

1.78

5.64

1.84

3.15

0.73

## K-Means Clustering Example (K=2)

Since there is no
change in cluster
membership, no
change in cluster
centroid and hence
the algorithm stops
Final clusters are
{1,2} and {3,4,5,6,7}

Individua
l

Centroid
1 Dist

Centroid
2 Dist

0.56

7.21

2 (1.5,2.0)

0.56

6.10

3.05

1.42

6.66

2.20

4.16

0.42

## K-Means clustering with R

Let us create some data
exam pleD ata= m atrix(c(rnorm (50,20,2),rnorm (20,8,3),rnorm (50,
10,2),rnorm (20,25,3)),nrow = 70)
The data created in this way has 70 rows and are already segmented in
two parts
The scatter plot shows
the data distribution

## Get the raw data from here: https://www.dropbox.com/s/y3qxg3nv7yqr9oq/exampleData.csv?dl=0

Make sure to remove the 1st column

## K-Means clustering with R

To run kmeans clustering in R km ean s() function is used
R codes are given below:
cluster= km eans(exam pleD ata,centers= 2)
plot(exam pleD ata,pch= 20,xlab= "1st variable",ylab= "2nd
variable",col= cluster\$cluster)
points(cluster\$centers,pch= "+ ",col= c("red","blue"),cex= 2)

Cluster centers

## K-Means clustering with R

Until and unless the clusters are markedly distinct from each other,
kmeans clustering will produce different results for each run
If number of clusters are 4 (instead of 2), in two separate runs two
separate outcomes have come up (as shown below)

## K-Means clustering with R

K-means algorithm gives different results in each run because it is a
greedy algorithm and has very high probability to get stuck into local
minima since initialization starts at random
To minimize the chances of getting stuck in local minima, initialization
can be started at multiple points ( multiple random start) and the best
model is returned out of those many random starts
fi
t= km eans(exam pleD ata,centers= 4,nstart= 30)
plot(exam pleD ata,pch= 19,col= fi
t\$cluster,xlab= 1st Variable, ylab= 2nd Variable)

## K-Means clustering with R

Deciding the optimum number of clusters is a concern in K-means
clustering as the researcher has to specify the number of clusters to
extract apriori
R sends out a few more information as output of k-means clustering and
total within sum of square value is one of them which can be used to
find out optimum number of clusters
w ss= lapply(1:15,function(x)km eans(exdata,centers= x,nstart= 30)\$tot.w ithinss)
plot(1:15,w ss,type= "l",xlab= "# of Clusters",ylab= "TotalW ithin SS")
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://id.scribd.com/presentation/328571707/Cluster-Analysis-K-means", "fetch_time": 1571228885000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2019-43/segments/1570986668569.22/warc/CC-MAIN-20191016113040-20191016140540-00058.warc.gz", "warc_record_offset": 544059277, "warc_record_length": 73587, "token_count": 1649, "char_count": 5495, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2019-43", "snapshot_type": "latest", "language": "en", "language_score": 0.9124172925949097, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00040-of-00064.parquet"}