randomForest 模型没有 运行
randomForest model doesn't run
我尝试在没有变量 Petal.Length
的情况下 运行 基于 iris
数据的 randomForest
模型。代码给我预测错误。我怎样才能正确编码?感谢帮助。
理查德
data (iris)
attach (iris)
iris$id <- 1:nrow(iris)
library (dplyr)
train <- iris %>%
sample_frac (0.8)
test <- iris %>%
anti_join(train, by = "id")
library (randomForest)
library (caret)
fit <- randomForest(Species ~
Sepal.Length +Sepal.Width +Petal.Width, data = train,)
prediction <- predict (fit, test [1:2 , 4])
confusionMatrix (test$Species,prediction)
你对测试数据集的子集是错误的。只需使用
prediction <- predict (fit, newdata = test)
代替
predict (fit, test [1:2 , 4])
它会自动取所需的自变量。
或者你可以使用 like
prediction <- predict (fit, subset(test, select = -c(Petal.Length)))
在prediction
函数中,您必须提供用于训练的所有数值数据。试试这个
prediction <- predict (fit, test[ , c(1:4)])
我尝试在没有变量 Petal.Length
的情况下 运行 基于 iris
数据的 randomForest
模型。代码给我预测错误。我怎样才能正确编码?感谢帮助。
理查德
data (iris)
attach (iris)
iris$id <- 1:nrow(iris)
library (dplyr)
train <- iris %>%
sample_frac (0.8)
test <- iris %>%
anti_join(train, by = "id")
library (randomForest)
library (caret)
fit <- randomForest(Species ~
Sepal.Length +Sepal.Width +Petal.Width, data = train,)
prediction <- predict (fit, test [1:2 , 4])
confusionMatrix (test$Species,prediction)
你对测试数据集的子集是错误的。只需使用
prediction <- predict (fit, newdata = test)
代替
predict (fit, test [1:2 , 4])
它会自动取所需的自变量。 或者你可以使用 like
prediction <- predict (fit, subset(test, select = -c(Petal.Length)))
在prediction
函数中,您必须提供用于训练的所有数值数据。试试这个
prediction <- predict (fit, test[ , c(1:4)])