我正在用ranger
包装和相同的种子安装两个模型。第一个预测类,第二个返回概率矩阵,我的目标是达到相同的结果,但我在 4 个寄存器上有所不同。有人知道解决方案。我正在使用每个班级的最大概率。切入点应该是什么?
library(ranger)
library(caret)
## fit model 1
mod <- ranger(formula = Species ~., data = iris, seed = 2020)
res1 <- predict(object = mod, data = iris[,-5])$predictions
## fit model 2
mod2 <- ranger(formula = Species ~., data = iris, probability = TRUE, seed = 2020)
res2 <- factor(ifelse(apply(predict(object = mod2, data = iris[,-5])$predictions, 1, which.max) == 1,"setosa",
ifelse(apply(predict(object = mod2, data = iris[,-5])$predictions, 1, which.max) == 2, "versicolor", "virginica")),
levels = c("setosa","versicolor","virginica"))
head(data.frame(res1, res2))
res1 res2
1 setosa setosa
2 setosa setosa
3 setosa setosa
4 setosa setosa
5 setosa setosa
6 setosa setosa
all.equal(res1, res2)
[1] "4 string mismatches"
我的预期输出
all.equal(res1, res2)
[1] TRUE