【发布时间】:2018-03-23 12:45:46
【问题描述】:
我已经完成了以下操作,但它给了我一个错误,说 NAs 是由强制引入的
ourBoston=data.frame(Boston)
ourBoston$high.medv=NA
levels(ourBoston$high.medv)=c("no","yes")
ourBoston$high.medv[Boston$medv>25]<-"yes"
ourBoston$high.medv[Boston$medv<=25]<-"no"
result<-rep(0,50)
for (i in 1:50) {
result=knn(train=data.frame(ourBoston$lstat),test=data.frame(ourBoston$lstat),cl=ourBoston$high.medv,k=i)
result[i]=sum(as.integer(ourBoston$high.medv))/length(result)
}
qplot(1:50,result[1:50])
我创建了一个列并将其添加到一个新的数据框->ourBoston
lstat 变量是我必须为响应变量 high.medv 选择的最佳预测变量。
训练和测试数据集必须相同。
在运行代码时,我收到 50 条警告:In as.integer(ourBoston$high.medv) : NAs引入了强制
【问题讨论】:
标签: r machine-learning data-science