【发布时间】:2020-03-18 07:00:51
【问题描述】:
我正在使用类似于以下示例的 xgboost,其中我将一些数值“重新编码”为 0、1、2 中的(数字)值,指示类标签。请注意,我没有将其转换为因子变量。
然后我使用 xgboost 来拟合模型并产生预测。
library(xgboost)
iris$Species <- runif(nrow(iris))
recode <- function(x){
if(x >= 0 & x <= 0.33){
x <- 0
} else if(x > 0.33 & x <= 0.66){
x <- 1
} else if(x > 0.66){
x <- 2
}
}
train <- xgb.DMatrix(data = as.matrix(iris[,-5]),
label = sapply(iris$Species, FUN = recode))
bst <- xgboost(data = train,
max_depth = 4, eta = 0.5, nrounds = 10,
objective = "multi:softprob",
num_class = 3)
pred <- predict(bst, as.matrix(iris[, -5]), reshape = TRUE)
str(pred)
是否可以获取预测矩阵的列标签?或者我可以确定它们是根据我重新编码输入的数值排序的吗?
【问题讨论】: