-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMining_Chapter8_part2.r
More file actions
102 lines (71 loc) · 1.9 KB
/
DataMining_Chapter8_part2.r
File metadata and controls
102 lines (71 loc) · 1.9 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
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
Gaussian EM Clustering
```{r,fig=T}
library(mixtools)
mydata.gauss=mvnormalmixEM(mydata,k=2)
#Posterior probabilities and clusters
#mydata.gauss$posterior
mydata.cluster=apply(mydata.gauss$posterior,
1,
which.max)
plot(mydata,col=mydata.cluster)
#Prior probabilities (also called mixing probabilities)
mydata.gauss$lambda
#Means for each cluster
mydata.gauss$mu
points(rbind(mydata.gauss$mu[[1]],
mydata.gauss$mu[[2]]),
pch=24,bg='blue',col="blue")
#Covariance matrices for each cluster
mydata.gauss$sigma
Sigma1
Sigma2
```
## Gaussian clustering for iris data
Standardizing iris
```{r, fig=T}
x=iris[,1:4]
xbar=apply(x,2,mean)
xbarMatrix=cbind(rep(1,150))%*%xbar
s=apply(x,2,sd)
sMatrix=cbind(rep(1,150))%*%s
z=(x-xbarMatrix)/sMatrix
apply(z,2,mean)
apply(z,2,sd)
plot(z[,3:4],col=iris$Species)
#Silhouette Coefficient
library(cluster)
library(fields)
mysil=function(x,dmatrix){
return(mean(silhouette(x=x,dmatrix=dmatrix)[,3]))
}
plot.sil=function(data,max.K,dmatrix){
sil.vect=1:max.K
for(K in 2:max.K){
data.gauss=mvnormalmixEM(data,k=K)
cluster=apply(data.gauss$posterior,
1,
which.max)
sil.vect[K]=mysil(cluster,dmatrix)
}
sil.vect=sil.vect[2:max.K]
plot(2:max.K,sil.vect,xlab="K",ylab="Silhouette Coefficient")
return(max(sil.vect))
}
dmatrix=rdist(z)
plot.sil(z,5,dmatrix)
#Gaussian clustering with k=2
iris.gauss=mvnormalmixEM(z,k=2)
#Posterior probabilities and clusters
iris.gauss$posterior
iris.cluster=apply(iris.gauss$posterior,
1,
which.max)
plot(z[,3:4],col=iris.cluster)
plot(z,col=iris.cluster)
#Prior probabilities
iris.gauss$lambda
#Means for each cluster
iris.gauss$mu
#Covariance matrices for each cluster
iris.gauss$sigma
```