Skip to content

Commit 5b579d0

Browse files
authored
Update k mean.R
1 parent 8687be6 commit 5b579d0

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Clustering/K-mean clustering/k mean.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,42 @@ install.packages("animation")
3030
library(animation)
3131
windows() #pop a new window
3232
km<-kmeans.ani(data,7)
33+
34+
35+
36+
37+
38+
39+
#K-Means Clustering
40+
41+
install.packages("plyr")
42+
library(plyr)
43+
44+
x <- runif(50) #Generate 50 random numbers and store it in x
45+
46+
y <- runif(50) #Generate 50 random numbers and store it in y
47+
48+
data <- cbind(x,y) #Combines the data of x and y
49+
50+
plot(data)
51+
52+
#Get the reference value for k The experiment in elbow plt - 4 clusters;
53+
#Use the above k value as referenceand run the below to find optimum k value
54+
55+
wss <- c()
56+
for (i in 2:15) wss[i]<- sum(kmeans(data, centers = i)$withinss)
57+
plot(1:15,wss,type = "b", xlab = "No of Clusters", ylab = "Avg Distance")
58+
59+
#Cluster Algorithm Building
60+
61+
km <- kmeans(data,8) #Here we are using the kmeans algorithm with k value as 8
62+
km$centers #To find the centroid of the Clusters
63+
km$cluster #To find the datapoints Cluster number
64+
65+
#In order to see the execution of kmeans algorith, we use the animation package
66+
67+
install.packages("animation")
68+
library(animation)
69+
70+
windows()#to pop a new window
71+
km1 <- kmeans.ani(data,6)

0 commit comments

Comments
 (0)