-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultilabelChain.R
111 lines (69 loc) · 2.33 KB
/
multilabelChain.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
library("utiml")
library("mlr")
library("BBmisc")
library("mldr")
library("ParamHelpers")
multilabelChain <- function(learner, mydata, binary.label.data) {
reponse_vector <- rep(0, dim(mydata)[2])
# permLabels <- sample(binary.label.data)
permLabels <- (binary.label.data)
permLabelsNames <- names(permLabels)
nr_labels = length(permLabelsNames)
modList <- list()
for (i in 1 : nr_labels)
{
chain = cbind(mydata, permLabels[i])
binarytask = makeClassifTask(id = "BinaryClassification", data = chain, target = permLabelsNames[i], positive = "TRUE")
mod = train(learner, binarytask)
task.pred = predict(mod, task = binarytask)
response = getPredictionResponse(task.pred)
for(j in 1:length(response))
{
if(response[[j]]==TRUE)
reponse_vector[j] <- 1
else
reponse_vector[j] <- 0
}
new_data = cbind(mydata, reponse_vector)
col_name <- permLabelsNames[i]
colnames(new_data)[dim(new_data)[2]] <- col_name
mydata <- new_data
modList[[i]] <- mod
class(modList) <- c("myChainClassifier")
}
names(modList) <- permLabelsNames
return(modList)
}
predict.myChainClassier <- function(model,testData,labelData)
{
predList <- list()
reponse_vector <- rep(0, dim(testData)[1])
labels <- names(model)
nr_labels <- length(labels)
predMatrix <- matrix(0,dim(testData)[1],nr_labels)
for (i in 1 : nr_labels)
{
data.label <- labelData[labels[i]]
chain = cbind(testData, data.label)
predictModel = model[[i]]
binarytask = makeClassifTask(id = "BinaryClassification", data = chain, target = labels[i], positive = "TRUE")
task.pred = predict(predictModel, task = binarytask)
response = getPredictionResponse(task.pred)
for(j in 1:length(response))
{
if(response[[j]]==TRUE)
reponse_vector[j] <- 1
else
reponse_vector[j] <- 0
}
new_data = cbind(testData, reponse_vector)
col_name <- labels[i]
colnames(new_data)[dim(new_data)[2]] <- col_name
testData <- new_data
# predList[[i]] <- reponse_vector
#class(predList) <- c("myChainClassifier")
predMatrix[,i] <- reponse_vector
}
colnames(predMatrix) <- labels
return(predMatrix)
}