-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitt_migrate_starter2.R
260 lines (173 loc) · 6.52 KB
/
pitt_migrate_starter2.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
library(lattice)
library(robustbase)
source("Pitt_migrate_norm_3_1.R")
#a<-read.table("C:\\Drive E\\imbalance\\Data\\aus.csv",sep=",",header=F)
#a<-read.table("C:\\Drive E\\imbalance\\Data\\thyroidGland.csv",sep=",",header=F)
#a<-read.table("C:\\Drive E\\imbalance\\Data\\sattelite.csv",sep=",",header=F)
a<-read.table("page.csv",sep=",",header=F)
#a<-read.table("C:\\Drive E\\imbalance\\Data\\wave.csv",sep=",",header=F)
#a<-read.arff("C:\\Drive E\\Fall16\\BBO-RMCode\\adult1.arff")
#a<-read.arff("C:/Drive E/Fall16/BBO-RMCode/cfs/optical_IG.arff")
#a<-read.table("C:\\Drive E\\imbalance\\Data\\german.csv",sep=",",header=F)
#a<-read.table("C:\\Drive E\\Fall 2017\\Data\\cmc.csv",sep=",",header=T)
#a<-read.table("C:\\Drive E\\imbalance\\Data\\sonar.csv",sep=",",header=F)
#a<-read.arff("C:\\Drive E\\Fall 2017\\Data\\thoracic.arff")
#a<-read.table("C:\\Drive E\\Fall 2017\\Data\\spect.csv",sep=",",header=T)
#a<-read.table("C:\\Drive E\\Fall 2017\\Data\\park.csv",sep=",",header=T)
rows<-nrow(a)
cols<-ncol(a)
##############
classIndex=11#23#--park #23--spect#17--thoracic#61--sonar #15#--adult##41#--wave#11 #--page#37--satt
################
### determine attType
f<-sapply(a, is.double)
f1<-sapply(a, is.factor)
f2<- sapply(f, function(x){if(x==TRUE) x=0 else x=1})
f3<-sapply(f1,function(x){if(x==TRUE) x=1 else x=0})
attType1<-rep(0, cols)
for(i in 1:cols){
if(f3[[i]]==1)#factor
attType1[i]<-1
if(length(unique(a[,i])) <=20)#discrete var if <=20 unique values
attType1[i]<-1
}
###for adult dataset only ..7/18
#attType1[1]<-0
nominal<-which(attType1==1)
##discard classIndex
nominal<-setdiff(nominal,classIndex)
y<-1
while(y<=length(nominal)){
i<-nominal[y]
if(i==classIndex)
next
#### here nominal values are encoded 1, 2,..R will consider them as numeric
### make 1, 2 as "1", "2"
a[,i]<-as.factor(a[,i])
colVal<-levels(a[,i])
#d<-c(1:length(sets1[[3]]))
d<-c(1:length(colVal))
###added new for binary feature 9/20
if(length(colVal)==2){
replaceNomVals<-as.numeric(colVal)
# any_na<-any(is.na(replaceNomVals))
is_binary<-sapply(colVal, function(x) x %in% c(0,1))
res_logical_vector<-as.logical(is_binary)
if(all(res_logical_vector)==TRUE)
d<-replaceNomVals
#binary_levels<-c(0,1)
###if binary levels, d is replaceNomVals..it is required for ordering of levels 0, 1
# if(all(replaceNomVals) %in% binary_levels )
# d<-replaceNomVals
}
levels(a[,i]) <- c(d)
y<-y+1
}
lowerRange<-c()
upperRange<-c()
for(y in 1:cols){
i<-y
colVal<-levels(a[,i])
lowerRange[i]<-min(as.numeric((a[,i])))
upperRange[i]<-max(as.numeric((a[,i])))
}
###apply cfs
#############################################################################################
### all migration curves seed 111
set.seed(155)
a<-a[sample(nrow(a)),]
#attType1= c( 1, 0, 1, 1, 0, 1, 1,0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1);#ger
#sourceCpp("C:/Drive E/Fall16/BBO-RMCode/cppFiles/aus_num_2.cpp")
#sourceCpp("C:/Drive E/Fall16/BBO-RMCode/cppFiles/top_down.cpp")
#sourceCpp("C:/Drive E/Fall16/BBO-RMCode/cppFiles/thy.cpp")
sourceCpp("Pitt_mem_1.cpp")
#sourceCpp("Pitt_mem_1.cpp")
outer_fold <-3
folds <-generateCVRuns(labels = a[, classIndex],
ntimes = 1,
nfold = outer_fold,
stratified=TRUE)
tuneFolds<-vector("list",3)
testfold <-vector("list",2)
migrate_data<-NULL
sumAcc<-0
allClass<-unique(a[, classIndex])
all_cv_time<-rep(0,10)
all_test_acc<-rep(0,10)
all_tuned_a<-rep(0,10)
all_tuned_b<-rep(0,10)
#ptm_allcv<-proc.time() - ptm
for(l in 1:outer_fold){
ptm_cv_st<- proc.time() #ifelse(l==1, proc.time() - ptm, ptm_cv_end)
#proc.time() - ptm
cv.test<-folds$`Run 1`[[l]]
cv.train<-a[-cv.test,]
cv.test<-a[cv.test,]
currDataSet<-cv.train
###init tune fold
# foldtemp <-generateCVRuns(labels = cv.train[, classIndex],
# ntimes = 1,
# nfold = 10,
# stratified=TRUE)
###take some part of tr data for optimization
# opt_ind<-c()
#tr_splits<-c(1:10)
# tr_splits[-l]
#for(opt in 1:2){
# curr_ind<-tr_splits[opt]
# opt_ind<-c(opt_ind, folds$`Run 1`[[curr_ind]])
# }
migrate_data<-cv.train
migrate_data <- data.frame(migrate_data)
migrate_data <-data.matrix(migrate_data)
####change 10/1
#set.seed(117)
#cv.train<- cv.train[sample(nrow( cv.train)),]
print(paste0("yey ", l))
# attType1=c(1,1,1,1,1,1,1,1,1,1,1);
cDist<-table(cv.train[,classIndex])
c<-NULL
for(j in 1:length(cDist))
c<-c(c,cDist[[j]])
df<-data.frame(currDataSet)
df = data.matrix(df)
classLabels<-allClass#c(1,2,3)
defaultRule<-0
defaultVal<-0
for(i in 1:length(cDist)){
if(cDist[[i]]>defaultVal){
defaultVal<-cDist[[i]]
defaultRule<-classLabels[i]
}
}
print(paste0(classIndex))
dfr<-setDefaultClass(defaultRule,classIndex,length(classLabels));
x<-NULL
# BBO_GA<-bbo2(fn=calcFit,genomeLen =6, rMin = 5, rMax = 20, lower=lowerRange,#c(1,64,0.4,0.1,0.05,-.5), #lower index less than actual
# upper=upperRange,#c(3,145,26.3,12,58.4,58.3),
# attType=attType1)
genomeLen<-ncol(a)
# satellite --prev 16, 25 rMin, rMax
BBO_GA<-bbo2(fn=calcFit,genomeLen =37, rMin = 10, rMax = 20, lower=lowerRange,#c(1,64,0.4,0.1,0.05,-.5), #lower index less than actual
upper=upperRange,#c(3,145,26.3,12,58.4,58.3),
attType=attType1)
# BBO_GA<-bbo2(fn=calcFit,genomeLen =59, rMin = 25, rMax = 35, lower=lowerRange,#c(1,64,0.4,0.1,0.05,-.5), #lower index less than actual
# upper=upperRange,#c(3,145,26.3,12,58.4,58.3),
# attType=attType1)
x<-BBO_GA$minCost$bestMember
currDataSet<-cv.test
##changed 11/9
df<-data.frame(currDataSet)
df = data.matrix(df)
evalRes_1 <-calcFitTst(x,attType1,df)
testAcc<-evalRes_1[1]#calcFit(x,attType1)
ptm_cv_end<-proc.time() - ptm_cv_st
all_cv_time[l]<-ptm_cv_end
all_test_acc[l]<-testAcc
all_tuned_a[l]<- tuned_val_a
all_tuned_b[l]<- tuned_val_b
print(paste0("testAcc: ", l, " ", testAcc))
sumAcc<-sumAcc+testAcc
}
#aus : 144--86.08, 177--86.08 seed 110--86.96, 120--85.94, 130--86.52
ptmElapse<-proc.time() - ptm