-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuningMax.R
179 lines (157 loc) · 7.55 KB
/
TuningMax.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#Basic stuff
{##Absolutely necessary to run initialization BEFORE this file
##Import data
data <- read.csv("LIDC dataset with full annotations.csv",header=TRUE)
img_fs <- data[,c(5:18, 43:69)]
img_fs <- data.frame(img_fs, Avg.Gabor(data))
#Df for results
col <- c("Mode 1", "Mode 2", "Mode 3", "Max Mode", "Set",
"I1 Label", "I1 Pred", "I1 Label Added", "I2 Label",
"I2 Pred","I2 Label Added", "I3 Label", "I3 Pred",
"I3 Label Added", "I4 Label", "I4 Pred")
results <- data.frame(data.frame(matrix(vector(), 810, 16, dimnames=list(c(), col))))
## Label tracker
label.tracker <- rep(1,nrow(data))
labelsum <- list()
#Null objects
train = NULL
test = NULL
valid = NULL
tables <- vector(mode="list",length=20)}
#Tuned controls per iteration
ics = rbind(rpart.control(minsplit = 300, minbucket= round(300/6), cp = 0.01))
for (t in 1:20){
set.seed(t)
##Process labels
#currently iterative labeling for both trail and test
labels <- data[,70:73]
#shuffles labels
labels <- t(apply(labels,1,sample))
#takes the mode for each iteration
labels <- cbind(labels[,1],apply(labels[,1:2],1,mode),
apply(labels[,1:3],1,mode),apply(labels,1,mode))
labels <- apply(labels,c(1,2),rescale)
results[1:4] <- labels
#Separate training, testing and valid
index <- bal_strat(labels)
#Get image features
train$img <- as.matrix(img_fs[index$train,])
test$img <- as.matrix(img_fs[index$test,])
valid$img <- as.matrix(img_fs[index$valid,])
table <- data.frame(data.frame(matrix(vector(), 50, 5,
dimnames=list(c(), c("np","nc","trM",
"teM", "diff")))))
#Controls
{ms = 20
table [1:3,1] = ms
c1 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c2 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c3 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 30
table [4:6,1] = ms
c4 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c5 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c6 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 40
table [7:10,1] = ms
c7 <- rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01)
c8 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c9 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c10 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 50
table [11:14,1] = ms
c11 <- rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01)
c12 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c13 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c14 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 60
table [15:18,1] = ms
c15 <- rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01)
c16 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c17 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c18 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 70
table [19:22,1] = ms
c19 <- rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01)
c20 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c21 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c22 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 100
table [23:26,1] = ms
c23 <- rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01)
c24 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c25 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c26 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 150
table [27:30,1] = ms
c27 <- rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01)
c28 <- rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01)
c29 <- rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01)
c30 <- rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01)
ms = 200
table [31:34,1] = ms
c200 = rbind (rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01))
ms = 250
table [35:38,1] = ms
c250 = rbind (rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01))
ms = 300
table [39:42,1] = ms
c300 = rbind (rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01))
ms = 350
table [43:46,1] = ms
c350 = rbind (rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01))
ms = 400
table [47:50,1] = ms
c400 = rbind (rpart.control(minsplit = ms, minbucket= round(ms/8), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/6), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/4), cp = 0.01),
rpart.control(minsplit = ms, minbucket= round(ms/2), cp = 0.01))
controls = rbind(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14,
c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27,
c28, c29, c30, c200, c250, c300, c350, c400)
table[,2] = c(rep(c("6", "4", "2"), 2), rep(c("8","6", "4", "2"), 11))}
#Get label
{
#Trials for parameter tuning
r = 1
#Make dataframes work for decision trees
train$data <- data.frame(cbind(results[index$train, "Max.Mode"], train$img))
colnames(train$data)[1] <- "label"
test$data <- data.frame(cbind(results[index$test, "Max.Mode"], test$img))
colnames(test$data)[1] <- "label"
valid$data <- data.frame(cbind(results[index$valid, "Max.Mode"], valid$img))
colnames(valid$data)[1] <- "label"}
for (i in 1:50){
#THIS IS WHERE CLASSIFICATION ACTUALLY HAPPENS
model <- rpart(formula, method = "class", data = train$data, control = controls[i,])
results["Max.Pred"] <- as.integer(predict(model, img_fs, type="class"))
miss.trM <- which(results[index$train,"Max.Pred"]!=
results[index$train , "Max.Mode"])
miss.teM <- which(results[index$test ,"Max.Pred"]!=
results[index$test , "Max.Mode"])
avg <- data.frame(data.frame(matrix(vector(), 50, 5,
dimnames=list(c(), c("tr","trM",
"teI", "teM", "diff")))))
table[i, "trM"] = 1-length(miss.trM)/length(index$train)
table[i, "teM"] = 1-length(miss.teM)/length(index$test)
}
table["diff"] = table["trM"]-table["teM"]
tables[[t]] <- table
}
tables2 = llply(tables, function(df) df[,sapply(df, is.numeric)]) # strip out non-numeric cols
avg = Reduce("+", tables2)/length(tables2)
View(avg)
View(tables)
View(results)