r - 可变长度与随机森林不同
问题描述
我对 R 真的很陌生,我想制作一个随机森林。但是我不断收到同样的错误-
Error in model.frame.default, lengths of variables differ.
我知道这个问题已经通过使用公式从字符串构造公式在另一个主题中得到解决,as.
但我真的不知道该怎么做。你能帮我吗?谢谢你。
#A vector that has random sample of training values (70% & 30% samples)
index = sample(2,nrow(df), replace = TRUE, prob=c(0.7,0.3))
#Training Date
training = df[index==1,]
#Testing data
testing = df[index==2,]
#Random forest model
RFM = randomForest(df$Rating~., df$Customer_type, data = training)
解决方案
那么你的错误是,你的自变量Rating
来自df
数据框,但你选择了data = training
. 这意味着您的随机森林应该从 2 个不同的数据帧中获取数据,这是不可能的。我想那randomForest(Rating ~ Customer_type, data = training)
会奏效。
推荐阅读
- angular - 如何让源映射适用于 npm 链接的角度库
- angular - Angular 7 Reactive Forms:重置验证器和整个表单包含的问题
- spring - Spring JCache 记录缓存命中
- png - 加载 PNG RGBA 通道,如交错的 JPG RGB 通道
- python - 对称函数的 numpy.gradient 的分量不同
- java - Java 检查递增/递减模式
- rocksdb - 是否可以不覆盖 RocksDB 中的早期值?
- python - 使用 OpenVINO 运行 Facenet
- xamarin.forms - 更改列表视图中的所有 xamarin 表单按钮背景
- javascript - SocketIO:如何从前端克服 CORS?