r - 如何在 KNN 中找到 K
问题描述
我正在重新创建示例代码
# k-NN using caret:
library(ISLR)
library(caret)
# Split the data:
data(iris)
indxTrain <- createDataPartition(y = iris$Sepal.Length,p = 0.75,list = FALSE)
training <- iris[indxTrain,]
testing <- iris[-indxTrain,]
# Run k-NN:
set.seed(400)
ctrl <- trainControl(method="repeatedcv",repeats = 3)
knnFit <- train(Species ~ ., data = training, method = "knn", trControl = ctrl, preProcess = c("center","scale"),tuneLength = 20)
knnFit
# Use plots to see optimal number of clusters:
# Plotting yields Number of Neighbours Vs accuracy (based on repeated cross validation)
plot(knnFit)
使用上面的示例,在我的数据集上进行了尝试,但是在运行时出现错误
set.seed(400)
ctrl <- trainControl(method="repeatedcv",repeats = 3)
knnFit <- train(Engine ~ ., data =usedcars2, method = "knn", trControl = ctrl,preProcess = c("center","scale"),tuneLength = 20)
输出
Error in na.fail.default(list(Engine = c(1582, 1086, 1198, 998, 1197, :
missing values in object
谁能帮我吗
解决方案
推荐阅读
- mqtt - MQTT 主题长度
- amazon-web-services - 只允许从网站访问 S3 对象
- web-services - 如何使用带有 https 的 jax-ws 客户端调用 Web 服务?
- javascript - 我们如何根据表列对 createEntityAdapter 的实体进行排序?
- c# - 在数据表中查找列表值并在数据表和其他列中更新
- ansible - 无法在 Ansible Tower 上执行不同的任务集,一个任务应该在 linux 主机上执行,而另一组任务应该在 localhost 上执行
- python - 在 SUSElinux 中将 python 2 升级到 python 3.8.8 并安装 pip3
- python - 将 CSV 文件附加到 Google 表格
- firebase - 在 React Native 中使用 Firebase Cloud Functions 发送电子邮件
- graph - 通过 IMSUB 生成的值不会显示在数据透视表中,但使用手动键入的公式创建的值会显示