gpt4 book ai didi

R 中的随机森林 - 应用于测试/验证集

转载 作者:行者123 更新时间:2023-12-04 04:13:38 27 4
gpt4 key购买 nike

我是使用随机森林的初学者。我正在尝试训练随机森林模型,然后将其应用于测试数据集,但在获取两个长度相同的数据集时遇到问题。我训练了一个很好的模型,但需要看看它在我的测试数据上的表现如何。请在下面查看我的代码。任何提示将不胜感激。

#Import Data
url <- "http://groupware.les.inf.puc-rio.br/static/WLE/WearableComputing_weight_lifting_exercises_biceps_curl_variations.csv"
df <- read.csv(url, header = TRUE, na.strings=c("NA","#DIV/0!",""))

#Remove columns containing ALL NA values
df <- df[,colSums(is.na(df)) == 0]

#Remove all irrelevant columns that you will not need as predictors
df <- subset(df, select = -c(1:7))

#Create training and testing datasets
library(caret)
inTrain <- createDataPartition(y = df$classe,
p=0.7, list = FALSE)
training <- df[inTrain,]
testing <- df[-inTrain,]

set.seed(2020)

rfmodel <- randomForest(classe ~ ., data = training, method="rf", ntree=100, importance = TRUE)
print(rfmodel) #Error rate of 0.17% = good!

#validating that this method works on training set
prediction_train <- predict(rfmodel, data = training, type = "class")
table(prediction_train, training$classe)

#Cannot figure out what is going wrong here
prediction_test <- predict(rfmodel, data = testing)
length(prediction_test) #27472
length(testing$classe) #11770
table(prediction_test, testing$classe) #ERROR (see below)
#Error in table(prediction_test, testing$classe) : all arguments must have the same length

我正在使用的软件包:

version$version.string [1] "R version 3.5.3 (2019-03-11)" packageVersion("caret", lib.loc = NULL) [1] ‘6.0.85’ packageVersion("rattle", lib.loc = NULL) [1] ‘5.3.0’ packageVersion("randomForest", lib.loc = NULL) [1] ‘4.6.14’ packageVersion("randomForestExplainer", lib.loc = NULL) [1] ‘0.10.0’

最佳答案

问题出在测试时的 data = 中。干杯。

rfmodel <- randomForest(training$classe ~ ., data = training[,-51], method="rf", ntree=100, importance = TRUE)
prediction_test <- predict(rfmodel, testing[,-51])
table(prediction_test, testing$classe)

prediction_test A B C D E
A 3346 3 0 0 0
B 1 2274 4 0 0
C 0 0 2049 15 0
D 0 0 0 1913 0
E 0 0 0 1 2164


关于R 中的随机森林 - 应用于测试/验证集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61211933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com