-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpendigits_code.Rmd
More file actions
163 lines (122 loc) · 4.69 KB
/
pendigits_code.Rmd
File metadata and controls
163 lines (122 loc) · 4.69 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: "Data Mining Final"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## GitHub Documents
This is an R Markdown format used for publishing markdown documents to GitHub. When you click the **Knit** button all R code chunks are run and a markdown file (.md) suitable for publishing to GitHub is generated.
## Including Code
You can include R code in the document as follows:
```{r cars}
data.dir = "C://Users//Arti Patel//Desktop//data_mining//final"
digits = read.csv(file.path(data.dir, "dataset_32_pendigits.csv"))
set.seed(524)
x = digits[,1:16]
library(dplyr)
library(dendextend)
digits_p1 <- filter(digits, class < 5)
x1 = digits_p1[,1:16]
digits_p2 <- filter(digits, class > 4)
x2 = digits_p2[,1:16]
# pca.comp = prcomp(x, scale=TRUE)
# summary(pca.comp)
# pc.9 = pca.comp$x[, 1:9] # first 9 PC = 95% variability
#initialize df for different kinds of predictions
predicts = data.frame(digits$class)
#kmeans--------------------------------
#using pca 9 dim
km_pc.9 = kmeans(pc.9, centers=10, nstart=50)
predicts$pc.9.predict = km_pc.9$cluster
table(predicts$digits.class,predicts$pc.9.predict)
#no pca
km_all = kmeans(x, centers=10, nstart=50)
predicts$allpredict = km_all$cluster
table(predicts$digits.class,predicts$allpredict)
#--------------------------------------
#hierarchical clustering
digit.dist = dist(scale(x),method="maximum")
hc = hclust(digit.dist, method="complete") # new hclust
#complete linkage dendrogram for all 10 digits
plot(hc, las=1,cex=.6)
#height vs k plot to determine k
plot(length(hc$height):1, hc$height, type='o', xlab="K", ylab="height", las=1,
xlim=c(1, 20),ylim=c(3, 7))
points(10, hc$height[n-9], col="red", pch=19) # K=10
# dend <- as.dendrogram(hc)
# dend <- color_labels(dend, k = 10)
# plot(dend)
#breaking into 2 sets: digits 0-4
p1.dist = dist(scale(x1))
hc1 = hclust(p1.dist, method="complete") # new hclust
#n = length(hc$height) # get number of merges
#plot(hc1, las=1,cex=.6)
dend <- as.dendrogram(hc1)
dend <- rotate(dend, 1:150)
dend <- color_branches(dend, k=5)
# labels_colors(dend) <-
# rainbow_hcl(5)[sort_levels_values(as.numeric(digist_p1[,16])[order.dendrogram(dend)]
# )]
# dend <- hang.dendrogram(dend,hang_height=0.1)
# dend <- set(dend, "labels_cex", 0.5)
# plot(dend,
# main = "Clustering digits 0-4",
# horiz = TRUE, nodePar = list(cex = .007))
dend <- color_labels(dend, k = 5,labels=c(0,1,2,3,4),col = c(3, 1, 1, 4, 1))
plot(dend)
#to compare linkage type from ward.D to complete:
hclust_methods <- c("ward.D", "single", "complete", "average", "mcquitty",
"median", "centroid", "ward.D2")
digits_dendlist <- dendlist()
for(i in seq_along(hclust_methods)) {
hc_digits <- hclust(p1.dist, method = hclust_methods[i])
digits_dendlist <- dendlist(digits_dendlist, as.dendrogram(hc_digits))
}
names(digits_dendlist) <- hclust_methods
digits_dendlist %>% dendlist(which = c(1,3)) %>% ladderize %>%
set("branches_k_color", k=5) %>%
# untangle(method = "step1side", k_seq = 3:20) %>%
tanglegram(faster = TRUE)
plot(length(hc1$height):1, hc1$height, type='o', xlab="K", ylab="height", las=1,
xlim=c(1, 10), ylim=c(7, 12))
points(5, hc$height[n-4], col="red", pch=19) # K=5
p = cutree(hc, k=10)
predicts$hc.maxd.compl = p
table(predicts$digits.class,predicts$hc.maxd.compl)
```
```{r}
set.seed(524)
eq = read.csv(file.path(data.dir, "earthquake_4.5_10.csv"))
spatial <- eq[,2:3]
temporal <- eq[,1:2]
library(ggplot2)
ggplot(spatial) + geom_point(aes(latitude, longitude))
library(mclust)
mc = Mclust(spatial,
G = 6, # set number of non-noise components
initialization=list(noise=TRUE))
plot(mc, what="classification",asp=1, las=1, xlim=c(10, 50))
grid()
summary(mc, parameters = TRUE)
mc_d <- densityMclust(spatial)
plot(mc_d, what='density',data=spatial, type="persp")
#eval.pts = expand.grid(latitude = seq(0, 60, by=.5), longitude = seq(-140, -50, by=0.5))
#mod_pred <- predict(mc_d,eval.pts,what="dens")
#plot(mod, what="density",data=X, type="persp")
# library(plot3D)
# library(rgl)
# library(plotly)
# eval.pts$f <- mod_pred
# #plot3D(eval.pts$latitude,eval.pts$longitude,eval.pts$f)
# p <- plot_ly(eval.pts,x = eval.pts$latitude, y=eval.pts$longitude, z = eval.pts$f) %>% add_surface()
# chart_link = api_create(p, filename="surface-1")
# chart_link
# p
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.