是否可以将预测函数与 r 中的随机森林模型一起使用



我可以对随机森林使用预测函数吗?PFB 我的代码,用于使用随机森林创建回归模型

Subsales<-read.csv('Sales.csv')
head(Subsales)

示例数据集

Date               SKU                            City   Sales
      <date>                               <chr>   <chr> <dbl>
1 2014-08-11 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   378
2 2014-08-18 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   348
3 2014-08-25 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   314
4 2014-09-01 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   324
5 2014-09-08 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   352
6 2014-09-15 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   453

法典

train_len=round(nrow(SubSales)*0.8) 
test_len=nrow(SubSales)
######Splitting dataset into training and testing#####
#### Training Set
training<-slice(SubSales,1:train_len) 
#### Testing Set
testing<-slice(SubSales,train_len+1:test_len)
training=training[c(1,4)]
testing=testing[c(1,4)]
library(randomForest)
set.seed(1234)
regressor = randomForest(formula=Sales~.,
                data=training,
                ntree=100)
y_pred = predict(regressor,newdata = testing)

我可以使用预测函数代替预测吗?

既然你问了一个相当广泛的问题,我会给你一个广泛的答案。您必须从训练随机森林模型开始。然后使用训练模型进行预测的函数是预测

最新更新