-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflexconc.R
293 lines (283 loc) · 11 KB
/
flexconc.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#' @description This function is a basic setup to other functions.
#'
#' @param data1It A data frame with all samples seted first iteration.
#' @param dataXIt A data frame with remaining samples.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param memo The history of the choices since the first iteration.
#' @param comp The insertion rule to be used in the comparation.
#'
#' @return The selected samples using a matching insertion rule.
#'
basicCheck <- function(data1It, dataXIt, confValue, memo, comp) {
samplesData <- c()
pos <- 0
xId <- c()
yCl <- c()
zConfPred <- c()
lvls <- match(dataXIt$id, data1It$id)
for (index in 1:length(lvls)) {
if (letCheck(data1It, dataXIt, confValue, lvls[index], index, comp)) {
pos <- pos + 1
xId[pos] <- as.numeric(dataXIt$id[index])
if ((comp == "1") || (comp == "2")) {
yCl[pos] <- as.character(dataXIt$cl[index])
} else {
yCl[pos] <- as.character(searchClass(xId[pos], memo))
}
zConfPred[pos] <- dataXIt[index, 2]
}
}
samplesData <- data.frame(cl = as.factor(yCl), prob = zConfPred, id = xId)
return(samplesData)
}
#' @description The rule is: Both samples have the same class and both
#' confidences are higher than threshold `confValue`.
#'
#' @param data1It A data frame with all samples seted first iteration.
#' @param dataXIt A data frame with remaining samples.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param index1It The sample from data1It that will be compared.
#' @param index The sample from dataXIt that will be compared.
#'
#' @return Logical return if rule is satisfied.
#'
classCheck <- function(data1It, dataXIt, confValue, index1It, index) {
if ((as.character(data1It[index1It, 1]) == as.character(dataXIt[index, 1]))) {
if ((data1It[index1It, 2] >= confValue)
&& (dataXIt[index, 2] >= confValue)) {
return(TRUE)
}
}
return(FALSE)
}
#' @description The rule is: Both samples have the same class and just one
#' confidences are higher than threshold `confValue`.
#'
#' @param data1It A data frame with all samples seted first iteration.
#' @param dataXIt A data frame with remaining samples.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param index1It The sample from data1It that will be compared.
#' @param index The sample from dataXIt that will be compared.
#'
#' @return Logical return if rule is satisfied.
#'
confCheck <- function(data1It, dataXIt, confValue, index1It, index) {
if ((as.character(data1It[index1It, 1]) == as.character(dataXIt[index, 1]))) {
if ((data1It[index1It, 2] >= confValue)
|| (dataXIt[index, 2] >= confValue)) {
return(TRUE)
}
}
return(FALSE)
}
#' @description The rule is: Both samples do not have the same class, but both
#' confidences are higher than threshold `confValue`.
#'
#' @param data1It A data frame with all samples seted first iteration.
#' @param dataXIt A data frame with remaining samples.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param index1It The sample from data1It that will be compared.
#' @param index The sample from dataXIt that will be compared.
#'
#' @return Logical return if rule is satisfied.
#'
diffClassCheck <- function(data1It, dataXIt, confValue, index1It, index) {
if ((as.character(data1It[index1It, 1]) != as.character(dataXIt[index, 1]))) {
if ((data1It[index1It, 2] >= confValue)
&& (dataXIt[index, 2] >= confValue)) {
return(TRUE)
}
}
return(FALSE)
}
#' @description The rule is: Both samples do not have the same class and both
#' confidences are lower than threshold `confValue`.
#'
#' @param data1It A data frame with all samples seted first iteration.
#' @param dataXIt A data frame with remaining samples.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param index1It The sample from data1It that will be compared.
#' @param index The sample from dataXIt that will be compared.
#'
#' @return Logical return if rule is satisfied.
#'
diffConfCheck <- function(data1It, dataXIt, confValue, index1It, index) {
if ((as.character(data1It[index1It, 1]) != as.character(dataXIt[index, 1]))) {
if ((data1It[index1It, 2] >= confValue)
|| (dataXIt[index, 2] >= confValue)) {
return(TRUE)
}
}
return(FALSE)
}
#' @description Generate a memorization matrix to store the label using some
#' condition (i.e. vote or sum of confidences).
#'
#' @param rawData The dataset of the unlabel and label samples.
#' @param nClass The total of the classes in the dataset.
#'
#' @return A matrix (number of samples x number of distinct classes).
#'
generateMemory <- function(rawData, nClass, all_levels) {
memo <- matrix(rep(0, nrow(rawData) * nClass), nrow(rawData), nClass, FALSE,
list(rownames(rawData), sort(all_levels)))
rm(rawData)
return(memo)
}
#' @description The Flexive Confidence with Classifier (FlexCon-C) algorithm. It
#' is a semi-supervised algorithm based on self-training. It uses a classifier
#' to change the threshold and flexibilize the number of samples which be
#' classified in the iteration.
#'
#' @assume The target of the data is called `class`.
#'
#' @param learner A classifier model to be trained each iteration.
#' @param predFunc The function that classifier predict the class of a sample
#' and the confidence rate of the prediction.
#' @param classDist The matrix with the amount of the samples per class.
#' @param initialAcc The accuracy of the initial labeled samples.
#' @param method The choosed algorithm (FlexCon-C1(s), FlexCon-C1(v) or
#' FlexCon-C2)
#' @param data The data set with labeled and unlabeled samples.
#' @param sup Ids of the labeled samples.
#' @param classiName The numeber of the classifier to train a sup model.
#' @param cr The changeRate param to flexibility this algorithm.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param maxIts The max number of iterations.
#'
#' @return A trained `model` to classify samples.
#'
flexConC <- function(learner, predFunc, classDist, initialAcc, method, data,
sup, classiName, cr = 5, confValue = 0.95, maxIts = 100) {
defaultSup <- sup
it <- 0
if (min(classDist$samplesClass) < 10) {
minClass <- min(classDist$samplesClass)
} else {
minClass <- floor(min(classDist$samplesClass) * 0.1)
}
nClass <- nrow(classDist)
trainSetIds <- c()
oldTrainSetIds <- c()
# FlexCon-C1 only
if ((method == "1") || (method == "2")) {
memo <- generateMemory(data, nClass, levels(data$class[sup]))
}
addRotSuperv <- FALSE
while ((it < maxIts) && (length(sup) < nrow(data))) {
newSamples <- c()
it <- it + 1
model <- generateModel(learner, form, data[sup, ])
probPreds <- generateProbPreds(model, data[-sup, ], predFunc)
if (it > 1) {
if (method != "3") {
memo <- updateMemory(probPreds, memo, method)
newSamples <- flexConC1(probPreds1It, probPreds, confValue, memo)
} else {
modelSup <- generateModel(learner, form, data[sup, ])
probPredsSup <- generateProbPreds(modelSup, data[-sup, ], predFunc)
newSamples <- flexConC2(probPreds, probPredsSup, confValue)
}
} else {
probPreds1It <- probPreds
newSamples <- probPreds[which(probPreds$pred >= confValue), ]
}
if ((length(newSamples) > 0) && (nrow(newSamples) > 0)) {
trainSetIds <- match(newSamples$id, rownames(data))
if (addRotSuperv) {
addRotSuperv <- FALSE
}
data[trainSetIds, label] <- newSamples$cl
classify <- validClassification(data, trainSetIds, oldTrainSetIds, nClass,
minClass)
sup <- union(sup, trainSetIds)
if (classify) {
oldTrainSetIds <- c()
localAcc <- calcLocalAcc(learner, data[defaultSup, ],
data[setdiff(sup, defaultSup), ])
confValue <- newConfidence(localAcc, initialAcc, confValue, cr)
} else {
oldTrainSetIds <- union(oldTrainSetIds, trainSetIds)
}
} else {
confValue <- max(probPreds[, 2])
}
}
return(model)
}
#' @description The FlexCon-C1 method. This method uses a matrix to store all
#' the labels of the samples.
#'
#' @param probPreds1It A data frame with all samples seted first iteration.
#' @param probPreds A data frame with remaining samples.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param memo The history of the choices since the first iteration.
#'
#' @return The ids of the selected samples by insetion rules.
#'
flexConC1 <- function(probPreds1It, probPreds, confValue, memo) {
labeled <- basicCheck(probPreds1It, probPreds, confValue, memo, "1")
lenLabeled <- length(labeled$id)
if (lenLabeled == 0) {
labeled <- basicCheck(probPreds1It, probPreds, confValue, memo, "2")
lenLabeled <- length(labeled$id)
if (lenLabeled == 0) {
labeled <- basicCheck(probPreds1It, probPreds, confValue, memo, "3")
lenLabeled <- length(labeled$id)
if (lenLabeled == 0) {
labeled <- basicCheck(probPreds1It, probPreds, confValue, memo, "4")
}
}
}
return(labeled)
}
# FlexCon-C2 funtion
flexConC2 <- function(probPreds, probPredsSuperv, confValue) {
probPreds <- convertProbPreds(probPreds)
probPredsSuperv <- convertProbPreds(probPredsSuperv)
probPredsCon <- (probPreds[, 2] >= confValue)
probPredsSupervCon <- (probPredsSuperv[, 2] >= confValue)
probPredsCl <- probPreds[, 1]
probPredsSupervCl <- probPredsSuperv[, 1]
newSamples <- which((probPredsCon & probPredsSupervCon)
& (probPredsCl == probPredsSupervCl))
if (length(newSamples) == 0) {
newSamples <- which((probPredsCon | probPredsSupervCon)
& (probPredsCl == probPredsSupervCl))
if (length(newSamples) == 0) {
newSamples <- which((probPredsCon & probPredsSupervCon)
& (probPredsCl != probPredsSupervCl))
if (length(newSamples)) {
addRotSuperv <<- TRUE
}
}
}
return(newSamples)
}
#' @description This function choose the insertion rule.
#'
#' @param data1It A data frame with all samples seted first iteration.
#' @param dataXIt A data frame with remaining samples.
#' @param confValue The confidence rate, it's a threshold to select samples.
#' @param index1It The sample from data1It that will be compared.
#' @param index The sample from dataXIt that will be compared.
#' @param comp The insertion rule to be used in the comparation.
#'
#' @return It calls the correspondent insertion rule.
#'
letCheck <- function(data1It, dataXIt, confValue, index1It, index, comp) {
switch(comp,
"1" = {
return(classCheck(data1It, dataXIt, confValue, index1It, index))
},
"2" = {
return(confCheck(data1It, dataXIt, confValue, index1It, index))
},
"3" = {
return(diffClassCheck(data1It, dataXIt, confValue, index1It, index))
},
"4" = {
return(diffConfCheck(data1It, dataXIt, confValue, index1It, index))
}
)
}