问题描述
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列,但我不断得到
I want to predict column 3 of test_set
, but I keep getting
即使我指定了 test_set [3]
不是 test_set [1]
,其中包含 Rm
Even though I specify test_set[3]
not test_set[1]
which contain Rm
列名称如下: Rm
, Lstat
和 Medv
。
test_set [3]
和 test_set [2]
给出相同的以下错误:
test_set[3]
and test_set[2]
give the same following error:
和 test_set [1]
给出:
我尝试了以下操作:
-
names(test_set)< -c('Rm','Lstat','Medv')
:我明确地重命名了。 -
is.data.f rame(test_set)
:我检查了test_set是否是一个数据框。
names(test_set) <- c('Rm', 'Lstat','Medv')
: I renamed explicitly.is.data.frame(test_set)
: i checked if test_set is a dataframe.
推荐答案
我通过以下代码解决了问题
I solved the problem by the following code
y_pred = predict(classifier, newdata = test_set[-3], type ='class')
R文档中的
报价
https://www.rdocumentation.org/packages/rpart/versions/4.1-13/topics/predict.rpartquote from R documentation
newdata:包含需要预测的值的数据框。在公式(对象)右侧引用的预测变量必须按名称出现在newdata中。如果丢失,则返回拟合值。
"newdata : data frame containing the values at which predictions are required. The predictors referred to in the right side of formula(object) must be present by name in newdata. If missing, the fitted values are returned."
这篇关于eval(predvars,data,env)中的错误:找不到对象“ Rm”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!