我已被要求将knn
应用于"收入"的数据,基于"年龄","性别"和"职业"来自adults.txt的数据。将数据加载到R后,这是我的代码。
library(class)
set.seed(1234)
ind <- sample(2, nrow(adult), replace=TRUE, prob=c(0.75, 0.25))
adult.training <- adult[ind==1, c(1,7,10)]
adult.test <- adult[ind==2, c(1,7,10)]
adult.trainLabels <- adult[ind==1, c(15)]
adult.testLabels <- adult[ind==2, c(15)]
adult_pred <- knn(train=adult.training, test=adult.test, cl=adult.trainLabels, k=3)
我收到以下错误:
Error in knn(train=adult.training, test=adult.test, cl=adult.trainLabels, :
NA/NAN/Inf in foreign function call (arg 6)
In addition: Warning messages:
1: In knn(train=adult.training, test=adult.test, cl=adult.trainLabels, :
NAs introduced by coercion
2: In knn(train=adult.training, test=adult.test, cl=adult.trainLabels, :
NAs introduced by coercion`
是否有可能为&#34;收入&#34;生成knn
数据基于上述变量,其中两个是因素?