gpt4 book ai didi

r - eval 中的错误(predvars、数据、env): object 'Rm' not found

转载 作者:行者123 更新时间:2023-12-01 23:35:17 25 4
gpt4 key购买 nike

dataset = read.csv('dataset/housing.header.binary.txt')
dataset1 = dataset[6] #higest positive correlation
dataset2 = dataset[13] #lowest negative correlation
dependentVal= dataset[14] #dependent value
new_dataset = cbind(dataset1,dataset2, dependentVal) # new matrix

#split dataset
#install.packages('caTools')
library(caTools)

set.seed(123) #this is needed to garantee that every run will produce the same output
split = sample.split(new_dataset, SplitRatio = 0.75)
train_set = subset(new_dataset, split == TRUE)
test_set = subset(new_dataset, split == FALSE)


#Fitting Decision Tree to training set
install.packages('rpart')
library(rpart)
classifier = rpart(formula = Medv ~ Rm + Lstat,
data = train_set)

#predicting the test set results
y_pred = predict(classifier, newdata = test_set[3], type ='class')

我想预测 test_set 的第 3 列,但我不断

Error in eval(predvars, data, env) : object 'Rm' not found

即使我指定 test_set[3]不是test_set[1]其中包含 Rm

列名称如下:Rm , Lstat ,和Medv .

test_set[3]test_set[2]给出相同的以下错误:

Error in eval(predvars, data, env) : object Rm not found

test_set[1]给出:

Error in eval(predvars, data, env) : object 'Lstat' not found

我尝试过以下方法:

  1. names(test_set) <- c('Rm', 'Lstat','Medv') :我明确地重命名了。
  2. is.data.frame(test_set) :我检查了 test_set 是否是一个数据框。

最佳答案

我通过以下代码解决了这个问题

y_pred = predict(classifier, newdata = test_set[-3], type ='class')

https://www.rdocumentation.org/packages/rpart/versions/4.1-13/topics/predict.rpart引用自 R 文档

“newdata:包含需要预测的值的数据框。公式(对象)右侧引用的预测变量必须按名称出现在newdata中。如果丢失,则返回拟合值。”

关于r - eval 中的错误(predvars、数据、env): object 'Rm' not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50539633/

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