-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainDyDaSL_Weight.R
141 lines (135 loc) · 5.92 KB
/
mainDyDaSL_Weight.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
args <- commandArgs(TRUE)
# args <- c("-s", "1", "-e", "1", "-l", "100", "-r", "0.1")
#' @description This function check the actual directory has a sub directory
#' called src if exists it's a new working directory
setWorkspace <- function() {
files <- c("classifiers.R", "crossValidation.R", "database.R", "flexconc.R",
"functions.R", "statistics.R", "utils.R", "write.R")
if ("src" %in% list.dirs(full.names = F)) {
setwd("src")
} else if (all(files %in% list.files())) {
print("All files exists!")
} else {
stop("The follow file(s) are missing!\n", files[!files %in% list.files()])
}
}
options(java.parameters = "-Xmx4g")
shuffleClassify <- function(size) {
typeClassify <- 1:length(baseClassifiers)
return(sample(typeClassify, size, T))
}
setWorkspace()
source('utils.R')
installNeedPacks()
token <- fromJSON('../token.txt')
pbSetup(token$key, defdev = 1)
scripts <- list.files(pattern='*.R', recursive=T)
for (scri in scripts) {
source(scri)
}
path <- "../results/detailed"
rm(scripts, scri)
databases <- list.files(path = "../datasets/")
myParam <- atribArgs(args, databases)
ratios <- myParam$ratios
lengthBatch <- myParam$lengthBatch
# lengthBatch <- c(100, 250, 750, 500, 1000, 2500, 5000)
databases <- databases[myParam$iniIndex:myParam$finIndex]
defines()
ratio <- 0.1
for (dataLength in lengthBatch) {
kValue <- floor(sqrt(dataLength))
for (dataset in databases) {
dataName <- strsplit(dataset, ".", T)[[1]][1]
script_name <- "_mainDyDaSL_Weight_"
fileName <- paste(ratio * 100, dataName, script_name, dataLength, ".txt", sep = "")
title <- paste("test", fileName, sep = "")
headerDetailedOutputEnsemble(title, path, dataName, "DyDaSL - Weight by Acc")
cat(dataName)
epoch <- 0
calculate <- TRUE
epoch <- epoch + 1
cat("\n\n\nRODADA: ", epoch, "\n\n\n\n")
set.seed(19)
ensemble <- list()
ensemble_weights <- c()
it <- 0
typeClassifier <- shuffleClassify(10)
train <- readData(dataset, path = "../datasets/")
all_classes <- sort(levels(train$data$class))
totalInstances <- nrow(train$data)
while (totalInstances > (train$state)) {
detect_drift <- FALSE
begin <- Sys.time()
it <- it + 1
batch <- getBatch(train, dataLength)
# batch$class <- droplevels(batch$class)
cat("Foram processadas: ", train$processed, "/", totalInstances, "\t")
rownames(batch) <- as.character(1:nrow(batch))
batchIds <- holdout(batch$class, ratio, seed = 1, mode="random")
batchLabeled <- batchIds$tr
rm(batchIds)
data <- newBase(batch, batchLabeled)
data$class <- droplevels(data$class)
if (((totalInstances - (train$state)) > 100) &&
(length(levels(data$class)) > 1)) {
classDist <- ddply(data[batchLabeled, ], ~class, summarise,
samplesClass = length(class))
if (it > 1) {
# ensemble <- knora(valid_base_classifier, data[batchLabeled],
# sort(levels(batch$class)))
ensemble_weights <- weightEnsemble(ensemble, data[batchLabeled, ],
all_classes)
ensemble_pred_weighted <- predictEnsembleConfidence(ensemble,
ensemble_weights,
data[batchLabeled, ],
all_classes)
cmLabeled <- table(ensemble_pred_weighted, data[batchLabeled, ]$class)
cmLabeled <- fixCM(cmLabeled, all_classes)
ensembleAcc <- getAcc(cmLabeled)
cat("Accuracy Ensemble:\t", ensembleAcc, "\n")
if (calculate) {
calculate <- FALSE
acceptabelAcc <- round(ensembleAcc, 2)
}
if (ensembleAcc < acceptabelAcc * 0.99) {
detect_drift <- TRUE
typeClassifier <- shuffleClassify(1)
learner <- baseClassifiers[[typeClassifier]]
initialAcc <- supAcc(learner, data[batchLabeled, ])
oracle <- flexConC(learner, funcType[typeClassifier], classDist,
initialAcc, "1", data, batchLabeled,
learner@func)
oracle_data <- cbind(batch[, -match(label, colnames(batch))],
class=predictClass(oracle, batch))
ensemble <- swapEnsemble(ensemble, oracle_data, oracle, all_classes)
calculate <- TRUE
}
} else {
for (i in typeClassifier) {
learner <- baseClassifiers[[i]]
initialAcc <- supAcc(learner, data[batchLabeled, ])
model <- flexConC(learner, funcType[i], classDist, initialAcc,
"1", data, batchLabeled, learner@func)
ensemble <- addingEnsemble(ensemble, model)
} # END FOR
} # END ELSE
} # END ELSE
end <- Sys.time()
ensemble_weights <- weightEnsemble(ensemble, batch, all_classes)
ensemble_pred_weighted <- predictEnsembleConfidence(ensemble,
ensemble_weights,
batch, all_classes)
cm_ensemble_weight <- table(ensemble_pred_weighted, batch$class)
cm_ensemble_weight <- fixCM(cm_ensemble_weight, all_classes)
detailedOutputEnsemble(title, path, length(ensemble_weights),
sum(diag(cm_ensemble_weight)),
sum(cm_ensemble_weight) - sum(diag(cm_ensemble_weight)),
getAcc(cm_ensemble_weight), fmeasure(cm_ensemble_weight),
kappa(cm_ensemble_weight), detect_drift, train$state,
difftime(end, begin, units = "mins"))
} # END WHILE
} # END FOR DATASETS
} # END FOR BATCHSIZE
msg <- paste("Batch Size = ", dataLength, "\nTime: ", Sys.time(), sep = "")
pbPost("note", "Experiment Finished!!", msg)