-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass7-kmeans.R
More file actions
59 lines (51 loc) · 1.63 KB
/
class7-kmeans.R
File metadata and controls
59 lines (51 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# install these packages
install.packages("ggfortify")
install.packages("factoextra")
# load the libraries
library(tidyverse)
library(stats)
library(ggfortify)
library(factoextra)
# dplyr and ggplot2 are part of tidyverse
# remove labels because
# we want to use it for unsupervised learning
view(iris)
# load the dataset
data <- iris[,1:4]
# scale the dataset
data_scale <- scale(data)
# specify the distance between data points
# data_scale_dist <- dist(data_scale)
# find how many clusters we need
# visualization to view the clusters we may have
# we use wss plot to view clusters
# the elbow is where we divide the clusters
fviz_nbclust(data_scale, kmeans, method="wss") +
labs(subtitle="Elbow method")
fviz_nbclust(data_scale, kmeans, method="silhouette") +
labs(subtitle="Silhouette method")
fviz_nbclust(data_scale, kmeans, method="gap_stat") +
labs(subtitle="Gap Stat method")
# KM = kmeans(data,2)
?kmeans
# syntax: kmeans(dataset, centers=k, nstart=n)
# random start point
model_km <- kmeans(data_scale,centers= 3, nstart=10)
# cluster plot
# boundary around the clusters
autoplot(model_km, data_scale, frame= TRUE)
# no boundary around the clusters
autoplot(model_km, data_scale)
# visualize it in a better way
km.clusters <- model_km$cluster
# Setosa_123
# Setosa_1
# Setosa_2
# rownames(data_scale) <- iris$Species
rownames(data_scale) <- paste(iris$Species,
1:dim(iris)[1],
sep="_")
fviz_cluster(list(data=data_scale,
cluster = km.clusters),
show.clust.cent = TRUE)
table(km.clusters, iris$Species)