Debian Codegraph @

inst/doc/xgboostfromJSON.R

raw
1 ## -----------------------------------------------------------------------------
2 require(xgboost)
3 require(jsonlite)
4 require(float)
5 options(digits = 22)
6
7 ## -----------------------------------------------------------------------------
8 dates <- c(20180130, 20180130, 20180130,
9 20180130, 20180130, 20180130,
10 20180131, 20180131, 20180131,
11 20180131, 20180131, 20180131,
12 20180131, 20180131, 20180131,
13 20180134, 20180134, 20180134)
14
15 labels <- c(1, 1, 1,
16 1, 1, 1,
17 0, 0, 0,
18 0, 0, 0,
19 0, 0, 0,
20 0, 0, 0)
21
22 data <- data.frame(dates = dates, labels = labels)
23
24 bst <- xgb.train(
25 data = xgb.DMatrix(as.matrix(data$dates), label = labels, missing = NA, nthread = 1),
26 nrounds = 1,
27 params = xgb.params(
28 objective = "binary:logistic",
29 nthread = 2,
30 max_depth = 1
31 )
32 )
33
34 ## -----------------------------------------------------------------------------
35 bst_json <- xgb.dump(bst, with_stats = FALSE, dump_format = 'json')
36 bst_from_json <- fromJSON(bst_json, simplifyDataFrame = FALSE)
37 node <- bst_from_json[[1]]
38 cat(bst_json)
39
40 ## -----------------------------------------------------------------------------
41 bst_preds_logodds <- predict(bst, as.matrix(data$dates), outputmargin = TRUE)
42
43 # calculate the logodds values using the JSON representation
44 bst_from_json_logodds <- ifelse(data$dates < node$split_condition,
45 node$children[[1]]$leaf,
46 node$children[[2]]$leaf)
47
48 bst_preds_logodds
49 bst_from_json_logodds
50
51 # test that values are equal
52 bst_preds_logodds == bst_from_json_logodds
53
54
55 ## -----------------------------------------------------------------------------
56 round(bst_preds_logodds, 2) == round(bst_from_json_logodds, 2)
57
58 ## -----------------------------------------------------------------------------
59 # now convert the dates to floats first
60 bst_from_json_logodds <- ifelse(fl(data$dates) < node$split_condition,
61 node$children[[1]]$leaf,
62 node$children[[2]]$leaf)
63
64 # test that values are equal
65 round(bst_preds_logodds, 2) == round(bst_from_json_logodds, 2)
66
67 ## -----------------------------------------------------------------------------
68 fl(20180131)
69
70 ## -----------------------------------------------------------------------------
71 # test that values are equal
72 bst_preds_logodds == bst_from_json_logodds
73
74 ## -----------------------------------------------------------------------------
75 # now convert the dates to floats first
76 bst_from_json_logodds <- ifelse(fl(data$dates) < fl(node$split_condition),
77 as.numeric(fl(node$children[[1]]$leaf)),
78 as.numeric(fl(node$children[[2]]$leaf)))
79
80 # test that values are equal
81 bst_preds_logodds == bst_from_json_logodds
82
83 ## -----------------------------------------------------------------------------
84 bst_preds <- predict(bst, as.matrix(data$dates))
85
86 # calculate the predictions casting doubles to floats
87 bst_from_json_preds <- ifelse(
88 fl(data$dates) < fl(node$split_condition)
89 , as.numeric(1 / (1 + exp(-1 * fl(node$children[[1]]$leaf))))
90 , as.numeric(1 / (1 + exp(-1 * fl(node$children[[2]]$leaf))))
91 )
92
93 # test that values are equal
94 bst_preds == bst_from_json_preds
95
96 ## -----------------------------------------------------------------------------
97 # calculate the predictions casting doubles to floats
98 bst_from_json_preds <- ifelse(
99 fl(data$dates) < fl(node$split_condition)
100 , as.numeric(fl(1) / (fl(1) + exp(fl(-1) * fl(node$children[[1]]$leaf))))
101 , as.numeric(fl(1) / (fl(1) + exp(fl(-1) * fl(node$children[[2]]$leaf))))
102 )
103
104 # test that values are equal
105 bst_preds == bst_from_json_preds
106