-
Notifications
You must be signed in to change notification settings - Fork 18
/
xgboost.R
131 lines (108 loc) · 3.16 KB
/
xgboost.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
#' Dispatches execution to the most appropriate XGBoost feature map generation function.
#'
#' @param x A dataset object.
as.fmap = function(x){
UseMethod("as.fmap")
}
#' Generates an XGBoost feature map based on feature data.
#'
#' @param x A "data.frame" object with independent variables.
#'
#' @return A "data.frame" object.
#'
#' @examples
#' data(iris)
#' iris.df = iris[, 1:4]
#' iris.fmap = as.fmap(iris.df)
as.fmap.data.frame = function(x){
feature_names = list()
feature_types = list()
names = colnames(x)
terms = attr(x, "terms")
if(!is.null(terms)){
names = attr(terms, "term.labels")
}
for(name in names){
col = x[[name]]
if(is.factor(col)){
feature_names = append(feature_names, lapply(levels(col), FUN = function(level){ paste(name, "=", level, sep = "") }))
feature_types = append(feature_types, rep("i", length(levels(col))))
} else
if(is.integer(col)){
feature_names = append(feature_names, name)
feature_types = append(feature_types, "int")
} else
if(is.numeric(col)){
feature_names = append(feature_names, name)
feature_types = append(feature_types, "q")
} else
{
stop()
}
}
fmap = .makeFMap(feature_names, feature_types)
return(fmap)
}
#' Generates an XGBoost feature map based on feature data.
#'
#' @param x A "matrix" object with independent variables.
#'
#' @return A "data.frame" object.
#'
#' @examples
#' data(iris)
#' iris.matrix = model.matrix(Species ~ . - 1, data = iris)
#' iris.fmap = as.fmap(iris.matrix)
as.fmap.matrix = function(x){
cat_features = list()
contrasts = attr(x, "contrasts")
if(!is.null(contrasts)){
contrast2features = function(contrasts, name){
mat = contrasts[[name]]
if(!identical(rownames(mat), colnames(mat))){
stop()
}
keys = lapply(colnames(mat), FUN = function(level){ paste(name, level, sep = "") })
values = lapply(colnames(mat), FUN = function(level){ paste(name, "=", level, sep = "") })
dict = c(values)
names(dict) = keys
return (dict)
}
for(name in names(contrasts)){
features = contrast2features(contrasts, name)
cat_features = append(cat_features, features)
}
}
feature_names = list()
feature_types = list()
for(name in colnames(x)){
cat_feature = cat_features[[name]]
if(!is.null(cat_feature)){
feature_names = append(feature_names, cat_feature)
feature_types = append(feature_types, "i")
} else
{
feature_names = append(feature_names, name)
feature_types = append(feature_types, "q")
}
}
fmap = .makeFMap(feature_names, feature_types)
return(fmap)
}
#' Writes XGBoost feature map to a file.
#'
#' @param fmap An XGBoost feature map as a "data.frame" object.
#' @param file A filesystem path to the result file.
write.fmap = function(fmap, file){
write.table(fmap, file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE)
}
.makeFMap = function(feature_names, feature_types){
fmap = data.frame("name" = unlist(feature_names), "type" = unlist(feature_types))
fmap = cbind("id" = seq(from = 0, to = (nrow(fmap) - 1)), fmap)
fmap$id = as.integer(fmap$id)
fmap$name = as.factor(fmap$name)
fmap$type = as.factor(fmap$type)
class(fmap) = c("fmap", class(fmap))
row.names(fmap) = NULL
return(fmap)
}