# How to transform the regression output to binary output in xgboost algorithm?

#1

I am currently solving a classification problem using xgboost in R and while solving it I have created the model and also used the model for predication in test data set.But I am getting not getting 0 or 1 instead of it I am getting some value between 0 and 1.

``````data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')
train <- agaricus.train # data set
test <- agaricus.test

str(train)
List of 2
\$ data :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. ..@ i       : int [1:143286] 2 6 8 11 18 20 21 24 28 32 ...
.. ..@ p       : int [1:127] 0 369 372 3306 5845 6489 6513 8380 8384 10991 ...
.. ..@ Dim     : int [1:2] 6513 126
.. ..@ Dimnames:List of 2
.. .. ..\$ : NULL
.. .. ..\$ : chr [1:126] "cap-shape=bell" "cap-shape=conical" "cap-shape=convex" "cap-shape=flat" ...
.. ..@ x       : num [1:143286] 1 1 1 1 1 1 1 1 1 1 ...
.. ..@ factors : list()
\$ label: num [1:6513] 1 0 0 1 0 0 0 1 0 0 ...

dtrain <- xgb.DMatrix(data = train\$data, label = train\$label) # creation of matrix for input in the model
bst <- xgboost(data = dtrain, max.depth = 2, eta = 1, nthread = 2, nround = 2, objective = "binary:logistic", verbose = 2) # classification model
pred <- predict(bst, test\$data) # predication
print(head(pred)) # predication result for first six values.
[1] 0.28583017 0.92392391 0.28583017 0.28583017 0.05169873 0.92392391
``````

While searching about this problem I found that The only thing that XGBoost does is a regression.I want to know how to convert regression output to binary output for classification problem.

#2

@harry- you can select any threshold value for creating the level of 1 and 0.

``````prediction <- as.numeric(pred > 0.5)
``````

Here the threshold value is 0.5.If the value is greater than 0.5 the answer would be 1 and if the value is less than 0.5 the answer would be 0.

Hope this helps!

Regards,
Hinduja