-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch-clustering.R
executable file
·142 lines (107 loc) · 4.51 KB
/
ch-clustering.R
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
### R code from vignette source 'ch-clustering.rnw'
###################################################
### code chunk number 1: ch-clustering.rnw:6-9
###################################################
# free memory
rm(list = ls())
gc()
###################################################
### code chunk number 2: ch-clustering.rnw:18-19
###################################################
set.seed(8953)
###################################################
### code chunk number 3: ch-clustering.rnw:22-25
###################################################
iris2 <- iris
iris2$Species <- NULL
(kmeans.result <- kmeans(iris2, 3))
###################################################
### code chunk number 4: ch-clustering.rnw:30-31
###################################################
table(iris$Species, kmeans.result$cluster)
###################################################
### code chunk number 5: ch-clustering.rnw:38-42
###################################################
plot(iris2[c("Sepal.Length", "Sepal.Width")], col = kmeans.result$cluster)
# plot cluster centers
points(kmeans.result$centers[,c("Sepal.Length", "Sepal.Width")], col = 1:3,
pch = 8, cex=2)
###################################################
### code chunk number 6: ch-clustering.rnw:62-68
###################################################
library(fpc)
pamk.result <- pamk(iris2)
# number of clusters
pamk.result$nc
# check clustering against actual species
table(pamk.result$pamobject$clustering, iris$Species)
###################################################
### code chunk number 7: ch-clustering.rnw:80-83
###################################################
layout(matrix(c(1,2),1,2)) # 2 graphs per page
plot(pamk.result$pamobject)
layout(matrix(1)) # change back to one graph per page
###################################################
### code chunk number 8: ch-clustering.rnw:95-97
###################################################
pam.result <- pam(iris2, 3)
table(pam.result$clustering, iris$Species)
###################################################
### code chunk number 9: ch-clustering.rnw:108-111
###################################################
layout(matrix(c(1,2),1,2)) # 2 graphs per page
plot(pam.result)
layout(matrix(1)) # change back to one graph per page
###################################################
### code chunk number 10: ch-clustering.rnw:131-132
###################################################
set.seed(2835)
###################################################
### code chunk number 11: ch-clustering.rnw:134-138
###################################################
idx <- sample(1:dim(iris)[1], 40)
irisSample <- iris[idx,]
irisSample$Species <- NULL
hc <- hclust(dist(irisSample), method="ave")
###################################################
### code chunk number 12: ch-clustering.rnw:144-148
###################################################
plot(hc, hang = -1, labels=iris$Species[idx])
# cut tree into 3 clusters
rect.hclust(hc, k=3)
groups <- cutree(hc, k=3)
###################################################
### code chunk number 13: ch-clustering.rnw:171-176
###################################################
library(fpc)
iris2 <- iris[-5] # remove class tags
ds <- dbscan(iris2, eps=0.42, MinPts=5)
# compare clusters with original class labels
table(ds$cluster, iris$Species)
###################################################
### code chunk number 14: ch-clustering.rnw:184-185
###################################################
plot(ds, iris2)
###################################################
### code chunk number 15: ch-clustering.rnw:196-197
###################################################
plot(ds, iris2[c(1,4)])
###################################################
### code chunk number 16: ch-clustering.rnw:206-207
###################################################
plotcluster(iris2, ds$cluster)
###################################################
### code chunk number 17: ch-clustering.rnw:217-229
###################################################
# create a new dataset for labeling
set.seed(435)
idx <- sample(1:nrow(iris), 10)
newData <- iris[idx,-5]
newData <- newData + matrix(runif(10*4, min=0, max=0.2), nrow=10, ncol=4)
# label new data
myPred <- predict(ds, iris2, newData)
# plot result
plot(iris2[c(1,4)], col=1+ds$cluster)
points(newData[c(1,4)], pch="*", col=1+myPred, cex=3)
# check cluster labels
table(myPred, iris$Species[idx])