-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainDyDaSL_FT.R
130 lines (124 loc) · 5.2 KB
/
mainDyDaSL_FT.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
args <- commandArgs(TRUE)
# args <- c("-s", "9", "-e", "9", "-l", "5000", "-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 <- c(100, 250, 750, 1000, 2500)
lengthBatch <- myParam$lengthBatch
databases <- databases[myParam$iniIndex:myParam$finIndex]
defines()
# for (ratio in ratios) {
ratio <- 0.1
for (dataLength in lengthBatch) {
kValue <- floor(sqrt(dataLength))
for (dataset in databases) {
dataName <- strsplit(dataset, ".", T)[[1]][1]
script_name <- "_mainDyDaSL_NormalfixedThreshold_"
fileName <- paste(ratio * 100, dataName, script_name, dataLength, ".txt", sep = "")
title <- paste("test", fileName, sep = "")
headerDetailedOutputEnsemble(title, path, dataName, "DyDaSL - FT")
cat(dataName)
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)))
ensemblePred <- predictEnsemble(ensemble, ensemble_weights,
data[batchLabeled, ],
sort(levels(batch$class)))
cmLabeled <- table(ensemblePred, data[batchLabeled, ]$class)
cmLabeled <- fixCM(cmLabeled, all_classes)
ensembleAcc <- getAcc(cmLabeled)
cat("Accuracy Ensemble:\t", ensembleAcc, "\n")
if (ensembleAcc < 0.8) {
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)
}
} 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
ensemble_weights <- rep(1, length(ensemble))
} # END ELSE
} # END ELSE
end <- Sys.time()
ensemble_pred <- predictEnsemble(ensemble, ensemble_weights, batch,
all_classes)
cm_ensemble <- table(ensemble_pred, batch$class)
cm_ensemble <- fixCM(cm_ensemble, all_classes)
cat("\n\tCM TEST:\n")
# print(cm_ensemble)
# print(cm_ensemble_weight)
detailedOutputEnsemble(title, path, length(ensemble), sum(diag(cm_ensemble)),
sum(cm_ensemble) - sum(diag(cm_ensemble)),
getAcc(cm_ensemble), fmeasure(cm_ensemble),
kappa(cm_ensemble), detect_drift, train$state,
difftime(end, begin, units = "mins"))
} # END WHILE
} # END FOR DATASETS
} # END FOR BATCHSIZE
# } # END FOR RATIOS