# NOT RUN {
# Standard example using the xgboost package example model
# make the xgboost model using xgb.DMatrix object as inputs
# }
# NOT RUN {
library(xgboost)
data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')
train <- agaricus.train
test <- agaricus.test
model1 <- xgboost(data = train$data, label = train$label, max_depth = 2,eta = 1, nthread = 2,
nrounds = 2, objective = "binary:logistic")
# }
# NOT RUN {
# the input feature names for the xgb.DMatrix object can be extracted as colnames(train$data)
# the output field name and categories must be inferred. Looking at train$label informs us
# that the output categories are either 0 or 1. The name cannot be inferred and so will be
# given a name "prediction1" save the tree information required in an external file
# }
# NOT RUN {
xgb.dump(model1, "model1.dumped.trees")
# }
# NOT RUN {
# Now all requiredinput parameters are known:
# }
# NOT RUN {
pmml(model1,inputFeatureNames=colnames(train$data),outputLabelName="prediction1",
outputCategories=c("0","1"),xgbDumpFile="model1.dumped.trees")
# }
# NOT RUN {
# use iris dataset to make a multinomial model
# input data as a matrix
# }
# NOT RUN {
model2 <- xgboost(data = as.matrix(iris[,1:4]), label = as.numeric(iris[,5])-1,
max_depth = 2, eta = 1, nthread = 2, nrounds = 2, objective = "multi:softprob",
num_class=3)
# }
# NOT RUN {
# The field names are easily extracted from the columnnames and the categories are converted to
# numeric format by xgboost.
# save the tree information file
# }
# NOT RUN {
xgb.dump(model2, "model2.dumped.trees")
pmml(model2,inputFeatureNames=colnames(as.matrix(iris[,1:4])),outputLabelName="Species",
outputCategories=c(1,2,3),xgbDumpFile="model2.dumped.trees")
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab