【问题标题】:Variable lengths differ with random forest可变长度与随机森林不同
【发布时间】:2021-07-09 19:20:19
【问题描述】:

我对 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)

【问题讨论】:

  • 你应该从数据框中调用它,即randomForest(Rating~., Customer_type, data = training)
  • 是的,非常感谢!

标签: r random-forest


【解决方案1】:

那么您的错误是,您的自变量是来自df 数据框的Rating,但您选择了data = training。这意味着您的随机森林应该从 2 个不同的数据帧中获取数据,这是不可能的。 我猜randomForest(Rating ~ Customer_type, data = training) 会起作用。

【讨论】:

  • 非常感谢,真的是这个小东西!
猜你喜欢
  • 2013-07-25
  • 2016-11-30
  • 2021-09-04
  • 2018-02-21
  • 2017-03-15
  • 2017-03-01
  • 2016-07-05
  • 2016-08-10
  • 2017-08-29
相关资源
最近更新 更多