在R,w/分类和数值预测中运行KNN

在R,w/分类和数值预测中运行KNN,r,knn,R,Knn,获取以下错误: Error in knn(train.x, test.x, train.y, k = 1) : NA/NaN/Inf in foreign function call (arg 6) In addition: Warning messages: 1: In knn(train.x, test.x, train.y, k = 1) : NAs introduced by coercion 2: In knn(train.x, test.x, train.y, k = 1) :

获取以下错误:

Error in knn(train.x, test.x, train.y, k = 1) : 
NA/NaN/Inf in foreign function call (arg 6)

In addition: Warning messages:
1: In knn(train.x, test.x, train.y, k = 1) : NAs introduced by coercion
2: In knn(train.x, test.x, train.y, k = 1) : NAs introduced by coercion
产生此错误的代码如下所示:

test = 1:200
train.x = absentknn[-test,]
test.x = absentknn[test,]
train.y <- Target[-test]
test.y = Target[test]
set.seed(1)
knn.pred = knn(train.x,test.x,train.y,k=1)
输出如下:

structure(list(Reason.for.absence = structure(c(3L, 1L), .Label = c("Diseases", 
"Other", "W/o ICD"), class = "factor"), Month.of.absence = structure(c(8L, 
8L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12"), class = "factor"), Day.of.the.week = structure(c(2L, 
2L), .Label = c("2", "3", "4", "5", "6"), class = "factor"), 
    Seasons = structure(c(1L, 1L), .Label = c("1", "2", "3", 
    "4"), class = "factor"), Disciplinary.failure = structure(1:2, .Label = c("0", 
    "1"), class = "factor"), Education = structure(c(1L, 1L), .Label = c("1", 
    "2", "3", "4"), class = c("ordered", "factor")), Son = structure(c(3L, 
    2L), .Label = c("0", "1", "2", "3", "4"), class = "factor"), 
    Social.drinker = structure(c(2L, 2L), .Label = c("0", "1"
    ), class = "factor"), Social.smoker = structure(c(1L, 1L), .Label = c("0", 
    "1"), class = "factor"), Pet = structure(c(2L, 1L), .Label = c("0", 
    "1", "2", "4", "5", "8"), class = "factor"), Target = structure(c(1L, 
    1L), .Label = c("Below THreshold", "Above Threshold"), class = "factor"), 
    Transportation.expense = c(1.01072476745574, -1.54333530271458
    ), Distance.from.Residence.to.Work = c(0.429265332324082, 
    -1.12093537978198), Service.time = c(0.101700985294323, 1.24198475980643
    ), Age = c(-0.532508283408938, 2.09144557686699), Work.load.Average.day = c(-0.817659381760693, 
    -0.817659381760693), Hit.target = c(0.638254115594372, 0.638254115594372
    ), Weight = c(0.851097236884886, 1.4720604661625), Height = c(-0.019033134875066, 
    0.975168263304808), Body.mass.index = c(0.775407774938871, 
    1.00875537699449)), .Names = c("Reason.for.absence", "Month.of.absence", 
"Day.of.the.week", "Seasons", "Disciplinary.failure", "Education", 
"Son", "Social.drinker", "Social.smoker", "Pet", "Target", "Transportation.expense", 
"Distance.from.Residence.to.Work", "Service.time", "Age", "Work.load.Average.day", 
"Hit.target", "Weight", "Height", "Body.mass.index"), row.names = 1:2, class = "data.frame")

您确定
knn
可以接受您提供的输入类型吗?实现
?knn
提到了欧氏距离,因此当它看到非数字输入时,很可能会中断。尝试再次使用数字输入构建模型,然后看……谢谢,尝试过了——但我得到了相同的结果。这两种方法都没有解释为什么我得到这个代码的空值:train.y什么是
Target
?您没有在代码中指定如何创建此项。很抱歉,是的。Target是一个booleen,是另一个变量旷工的函数。缺勤$Target[缺勤$旷工.time.in.hours
structure(list(Reason.for.absence = structure(c(3L, 1L), .Label = c("Diseases", 
"Other", "W/o ICD"), class = "factor"), Month.of.absence = structure(c(8L, 
8L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12"), class = "factor"), Day.of.the.week = structure(c(2L, 
2L), .Label = c("2", "3", "4", "5", "6"), class = "factor"), 
    Seasons = structure(c(1L, 1L), .Label = c("1", "2", "3", 
    "4"), class = "factor"), Disciplinary.failure = structure(1:2, .Label = c("0", 
    "1"), class = "factor"), Education = structure(c(1L, 1L), .Label = c("1", 
    "2", "3", "4"), class = c("ordered", "factor")), Son = structure(c(3L, 
    2L), .Label = c("0", "1", "2", "3", "4"), class = "factor"), 
    Social.drinker = structure(c(2L, 2L), .Label = c("0", "1"
    ), class = "factor"), Social.smoker = structure(c(1L, 1L), .Label = c("0", 
    "1"), class = "factor"), Pet = structure(c(2L, 1L), .Label = c("0", 
    "1", "2", "4", "5", "8"), class = "factor"), Target = structure(c(1L, 
    1L), .Label = c("Below THreshold", "Above Threshold"), class = "factor"), 
    Transportation.expense = c(1.01072476745574, -1.54333530271458
    ), Distance.from.Residence.to.Work = c(0.429265332324082, 
    -1.12093537978198), Service.time = c(0.101700985294323, 1.24198475980643
    ), Age = c(-0.532508283408938, 2.09144557686699), Work.load.Average.day = c(-0.817659381760693, 
    -0.817659381760693), Hit.target = c(0.638254115594372, 0.638254115594372
    ), Weight = c(0.851097236884886, 1.4720604661625), Height = c(-0.019033134875066, 
    0.975168263304808), Body.mass.index = c(0.775407774938871, 
    1.00875537699449)), .Names = c("Reason.for.absence", "Month.of.absence", 
"Day.of.the.week", "Seasons", "Disciplinary.failure", "Education", 
"Son", "Social.drinker", "Social.smoker", "Pet", "Target", "Transportation.expense", 
"Distance.from.Residence.to.Work", "Service.time", "Age", "Work.load.Average.day", 
"Hit.target", "Weight", "Height", "Body.mass.index"), row.names = 1:2, class = "data.frame")