Non - Hierarchical Clustering

K- Means Clustering

#install.packages("plyr")
library(plyr)
## Warning: package 'plyr' was built under R version 3.4.4
x <-  runif(50) # generating 50 random numbers
x
##  [1] 0.893091462 0.322538036 0.086988587 0.037545785 0.304024326
##  [6] 0.482701827 0.524158866 0.729887515 0.879697652 0.910081380
## [11] 0.921183663 0.567386410 0.327493357 0.482675093 0.058051599
## [16] 0.169094161 0.174302929 0.006406737 0.390709955 0.665487169
## [21] 0.662815883 0.780086010 0.974055614 0.052998021 0.576951337
## [26] 0.974210666 0.200947567 0.739856845 0.306733584 0.809986353
## [31] 0.742593674 0.315033908 0.987399167 0.883473156 0.166661585
## [36] 0.705020486 0.186476124 0.035278007 0.458591681 0.508812002
## [41] 0.978520857 0.557334581 0.738228695 0.556032936 0.050588112
## [46] 0.857959754 0.491392905 0.302461277 0.328071809 0.437835071
y <-  runif(50) # generating 50 random numbers 
y
##  [1] 0.863469115 0.841564692 0.731385451 0.089519612 0.116178028
##  [6] 0.440530585 0.084195378 0.460365857 0.222016541 0.617902891
## [11] 0.322959726 0.828901739 0.805135876 0.310439105 0.248607574
## [16] 0.321832794 0.228444586 0.433728480 0.551647361 0.073849248
## [21] 0.915566019 0.172902728 0.749746324 0.794059173 0.398052036
## [26] 0.773544155 0.452331316 0.429586574 0.569719925 0.563078229
## [31] 0.795769983 0.254999965 0.009952825 0.228327641 0.797879223
## [36] 0.483001342 0.291852642 0.980463674 0.333948758 0.582605137
## [41] 0.991764807 0.058939203 0.256333534 0.345179387 0.538535840
## [46] 0.236810025 0.673539011 0.944334867 0.567854694 0.735406302
data <- cbind(x,y) 
data
##                 x           y
##  [1,] 0.893091462 0.863469115
##  [2,] 0.322538036 0.841564692
##  [3,] 0.086988587 0.731385451
##  [4,] 0.037545785 0.089519612
##  [5,] 0.304024326 0.116178028
##  [6,] 0.482701827 0.440530585
##  [7,] 0.524158866 0.084195378
##  [8,] 0.729887515 0.460365857
##  [9,] 0.879697652 0.222016541
## [10,] 0.910081380 0.617902891
## [11,] 0.921183663 0.322959726
## [12,] 0.567386410 0.828901739
## [13,] 0.327493357 0.805135876
## [14,] 0.482675093 0.310439105
## [15,] 0.058051599 0.248607574
## [16,] 0.169094161 0.321832794
## [17,] 0.174302929 0.228444586
## [18,] 0.006406737 0.433728480
## [19,] 0.390709955 0.551647361
## [20,] 0.665487169 0.073849248
## [21,] 0.662815883 0.915566019
## [22,] 0.780086010 0.172902728
## [23,] 0.974055614 0.749746324
## [24,] 0.052998021 0.794059173
## [25,] 0.576951337 0.398052036
## [26,] 0.974210666 0.773544155
## [27,] 0.200947567 0.452331316
## [28,] 0.739856845 0.429586574
## [29,] 0.306733584 0.569719925
## [30,] 0.809986353 0.563078229
## [31,] 0.742593674 0.795769983
## [32,] 0.315033908 0.254999965
## [33,] 0.987399167 0.009952825
## [34,] 0.883473156 0.228327641
## [35,] 0.166661585 0.797879223
## [36,] 0.705020486 0.483001342
## [37,] 0.186476124 0.291852642
## [38,] 0.035278007 0.980463674
## [39,] 0.458591681 0.333948758
## [40,] 0.508812002 0.582605137
## [41,] 0.978520857 0.991764807
## [42,] 0.557334581 0.058939203
## [43,] 0.738228695 0.256333534
## [44,] 0.556032936 0.345179387
## [45,] 0.050588112 0.538535840
## [46,] 0.857959754 0.236810025
## [47,] 0.491392905 0.673539011
## [48,] 0.302461277 0.944334867
## [49,] 0.328071809 0.567854694
## [50,] 0.437835071 0.735406302

Kmeans

#windows()
plot(data)

plot(data, type="n")
text(data, rownames(data))

# Implemantation

km <- kmeans(data,4) #kmeans clustering
str(km)
## List of 9
##  $ cluster     : int [1:50] 1 4 4 3 3 2 2 2 2 1 ...
##  $ centers     : num [1:4, 1:2] 0.868 0.696 0.15 0.309 0.784 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:4] "1" "2" "3" "4"
##   .. ..$ : chr [1:2] "x" "y"
##  $ totss       : num 8.34
##  $ withinss    : num [1:4] 0.243 0.84 0.294 0.626
##  $ tot.withinss: num 2
##  $ betweenss   : num 6.33
##  $ size        : int [1:4] 8 18 10 14
##  $ iter        : int 2
##  $ ifault      : int 0
##  - attr(*, "class")= chr "kmeans"
#install.packages("animation")
library(animation)
## Warning: package 'animation' was built under R version 3.4.4
windows()
km1 <- kmeans.ani(data, 4)

km$centers
##           x         y
## 1 0.8681695 0.7838552
## 2 0.6959292 0.2704106
## 3 0.1502471 0.2976031
## 4 0.3089543 0.7431784