尝试使用 的输出randomForest
对新数据(甚至原始训练数据)进行分类时,出现以下错误:
> res.rf5 <- predict(model.rf5, train.rf5)
Error in predict.randomForest(model.rf5, train.rf5) :
New factor levels not present in the training data
这个错误是什么意思?为什么即使我尝试预测我用来训练的相同数据也会出现此错误?
下面是一个可用于重现错误的小示例。
train.rf5 <- structure(
list(A = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 3L),
.Label = c("(-0.1,19.9]", "(19.9,40]", "(80.1,100]"),
class = c("ordered", "factor")),
B = structure(c(3L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 4L),
.Label = c("1", "2", "4", "5"),
class = c("ordered", "factor")),
C = structure(c(1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L),
.Label = c("FALSE", "TRUE"),
class = "factor")),
.Names = c("A", "B", "C"),
row.names = c(7L, 8L, 10L, 11L, 13L, 15L, 16L, 17L, 18L, 19L),
class = "data.frame")
# A B C
# 7 (19.9,40] 4 FALSE
# 8 (-0.1,19.9] 1 FALSE
# 10 (-0.1,19.9] 1 TRUE
# 11 (-0.1,19.9] 1 FALSE
# 13 (-0.1,19.9] 1 FALSE
# 15 (-0.1,19.9] 1 TRUE
# 16 (80.1,100] 2 TRUE
# 17 (-0.1,19.9] 1 FALSE
# 18 (-0.1,19.9] 1 FALSE
# 19 (80.1,100] 5 TRUE
require(randomForest)
model.rf5 <- randomForest(C ~ ., data = train.rf5)
res.rf5 <- predict(model.rf5, train.rf5) # Causes error
我在 SO 上看到了一些可能相关的问题,但我认为它们不能直接解决我的问题
与 1) 不同,我没有数据中未表示的因子水平,并且与 2) 不同,我的训练数据和测试数据中的因子水平是相同的。
编辑:附加信息:
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] randomForest_4.6-7
loaded via a namespace (and not attached):
[1] tools_3.0.1