简体   繁体   English

随机森林预测模型

[英]random forest prediction model

I am trying to build a random forest model for prices prediction problem.我正在尝试为价格预测问题建立一个随机森林模型。 I have went through the following steps:我经历了以下步骤:

1) split the data into 3 sets train, test and valid (it is required to split into 3 sets not only train and test) 1)将数据拆分为3组train、test和valid(不仅需要拆分为train和test,还需要拆分为3组)

set.seed(1234)
assignment <- sample(1:3, size = nrow(train), prob = c(0.7, 0.15, 0.15), replace = TRUE) 
#Create a train, validation and tests from the train data
train_train <- train[assignment == 1, ]  
train_valid <- train[assignment == 2, ]  
train_test <- train[assignment == 3, ] 

2) I have built the model with x and y being from the train set 2)我已经建立了模型,其中 x 和 y 来自训练集

fit_rf_train <- train(x = train_train[, -which(names(train_train) %in% 
c("Item_Identifier", "Item_Outlet_Sales"))], 
                y = train_train$Item_Outlet_Sales,
                method = "ranger",
                metric = "RMSE",
                tuneGrid = expand.grid(
                  .mtry = 6,
                  .splitrule = "variance",
                  .min.node.size = c(10,15,20)),
                trControl = trControl,
                importance = "permutation",
                num.trees = 350)

I have the following screenshot for model output on the same train data:对于同一列车数据上的模型输出,我有以下屏幕截图:

训练数据的模型输出

3) Using predict function I used the model with the two other data sets, valid and test using the following line of code: 3) 使用预测函数,我将模型与其他两个数据集一起使用,使用以下代码行进行验证和测试:

prediction_test <- predict(fit_rf_train, train_test)
prediction_valid <- predict(fit_rf_train, train_valid)

My question is how can I measure the performance of the model on the un seen data (test and valid)?我的问题是如何衡量模型在未见数据(测试和有效)上的性能?

If you want to stick with caret , then you can do the following:如果您想坚持使用caret ,那么您可以执行以下操作:

library(caret)
trainda<-createDataPartition(iris$Sepal.Length,p=0.8,list=F)
valid_da<-iris[-trainda,]
trainda<-iris[trainda,]
ctrl<-trainControl(method="cv",number=5)
set.seed(233)
m<-train(Sepal.Length~.,data=trainda,method="rf",metric="RMSE",trControl = ctrl,verbose=F)
m1<-predict(m,valid_da)
RMSE(m1,valid_da$Sepal.Length)

Result:结果:

[1] 0.3499783

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM