I am using predict.xgb.Booster
with "multi:softprob"
objective and 3 classes, in this way:
library(xgboost)
data(iris)
iris$Species <- as.factor(iris$Species)
# extract 80% random samples as training set
ix <- sample(nrow(iris), 0.8 * nrow(iris))
# all
all <- xgb.DMatrix(data.matrix(iris[, 1:ncol(iris)-1]),
label = as.numeric(iris$Species)-1)
# training set
train <- xgb.DMatrix(data.matrix(iris[ix, 1:ncol(iris)-1]),
label = as.numeric(iris$Species[ix])-1)
# test set (20% of the dataset)
test <- xgb.DMatrix(data.matrix(iris[-ix, 1:ncol(iris)-1]),
label = as.numeric(iris$Species[-ix])-1)
params <- list(
objective = "multi:softprob",
learning_rate = 0.05,
subsample = 0.9,
colsample_bynode = 1,
reg_lambda = 2,
max_depth = 35,
num_class = length(unique(iris$Species))
)
# https://www.rdocumentation.org/packages/xgboost/versions/1.4.1.1/topics/xgb.train
mod <- xgb.train(
params,
data = train,
watchlist = list(valid = test),
early_stopping_rounds = 50,
print_every_n = 100,
nrounds = 10000 # early stopping
)
pred <- predict(mod, newdata = all, reshape = TRUE)
With the above code, pred
looks like:
V1 V2 V3
1 0.95375967 0.02518489 0.02105547
2 0.95375967 0.02518489 0.02105547
3 0.95375967 0.02518489 0.02105547
...
I would need to create a vector storing the classes with the highest value in each row.
For instance, in the above example it would be V1
for all the three rows.
My doubt is, how do I know to which of my 3 classes do V1 to V3 refer to?