knn函数出错

时间:2013-06-01 15:03:55

标签: r knn

我尝试运行此行:

knn(mydades.training[,-7],mydades.test[,-7],mydades.training[,7],k=5)

但我总是得到这个错误:

Error in knn(mydades.training[, -7], mydades.test[, -7], mydades.training[,  : 
  NA/NaN/Inf in foreign function call (arg 6)
In addition: Warning messages:
1: In knn(mydades.training[, -7], mydades.test[, -7], mydades.training[,  :
  NAs introduced by coercion
2: In knn(mydades.training[, -7], mydades.test[, -7], mydades.training[,  :
  NAs introduced by coercion

请问好吗?

PS:mydades.training和mydades.test定义如下:

N <- nrow(mydades) 
permut <- sample(c(1:N),N,replace=FALSE)
ord <- order(permut)
mydades.shuffled <- mydades[ord,]
prop.train <- 1/3
NOMBRE <- round(prop.train*N)
mydades.training <- mydades.shuffled[1:NOMBRE,]
mydades.test <- mydades.shuffled[(NOMBRE+1):N,]

3 个答案:

答案 0 :(得分:20)

我怀疑你的问题在于'mydades'中有非数字数据字段。错误行:

NA/NaN/Inf in foreign function call (arg 6)

让我怀疑对C语言实现的knn函数调用失败了。 R中的许多函数实际上调用底层的,更有效的C实现,而不是只在R中实现算法。如果在R控制台中键入'knn',则可以检查'knn'的R实现。存在以下行:

 Z <- .C(VR_knn, as.integer(k), as.integer(l), as.integer(ntr), 
        as.integer(nte), as.integer(p), as.double(train), as.integer(unclass(clf)), 
        as.double(test), res = integer(nte), pr = double(nte), 
        integer(nc + 1), as.integer(nc), as.integer(FALSE), as.integer(use.all))

其中.C表示我们使用提供的函数参数调用名为'VR_knn'的C函数。由于你有两个错误

NAs introduced by coercion

我认为两个as.double / as.integer调用失败,并引入了NA值。如果我们开始计算参数,第6个参数是:

as.double(train)

在以下情况下可能会失败:

# as.double can not translate text fields to doubles, they are coerced to NA-values:
> as.double("sometext")
[1] NA
Warning message:
NAs introduced by coercion
# while the following text is cast to double without an error:
> as.double("1.23")
[1] 1.23

你得到两个强制错误,可能由'as.double(train)'和'as.double(test)'给出。由于您没有向我们提供“mydades”的具体细节,以下是我最好的猜测(以及人工多元正态分布数据):

library(MASS)
mydades <- mvrnorm(100, mu=c(1:6), Sigma=matrix(1:36, ncol=6))
mydades <- cbind(mydades, sample(LETTERS[1:5], 100, replace=TRUE))

# This breaks knn
mydades[3,4] <- Inf
# This breaks knn
mydades[4,3] <- -Inf
# These, however, do not introduce the coercion for NA-values error message

# This breaks knn and gives the same error; just some raw text
mydades[1,2] <- mydades[50,1] <- "foo"
mydades[100,3] <- "bar"

# ... or perhaps wrongly formatted exponential numbers?
mydades[1,1] <- "2.34EXP-05"

# ... or wrong decimal symbol?
mydades[3,3] <- "1,23" 
# should be 1.23, as R uses '.' as decimal symbol and not ','

# ... or most likely a whole column is non-numeric, since the error is given twice (as.double problem both in training AND test set)
mydades[,1] <- sample(letters[1:5],100,replace=TRUE)

我不会将数字数据和类标签保存在单个矩阵中,也许您可​​以将数据拆分为:

mydadesnumeric <- mydades[,1:6] # 6 first columns
mydadesclasses <- mydades[,7]

使用电话

str(mydades); summary(mydades)

也可以帮助您/我们找到有问题的数据条目并将其更正为数字条目或省略非数字字段。

您提供的其余运行代码(在破坏数据之后):

N <- nrow(mydades) 
permut <- sample(c(1:N),N,replace=FALSE)
ord <- order(permut)
mydades.shuffled <- mydades[ord,]
prop.train <- 1/3
NOMBRE <- round(prop.train*N)
mydades.training <- mydades.shuffled[1:NOMBRE,]
mydades.test <- mydades.shuffled[(NOMBRE+1):N,]

# 7th column seems to be the class labels
knn(train=mydades.training[,-7],test=mydades.test[,-7],mydades.training[,7],k=5)

答案 1 :(得分:11)

@ Teemu的精彩回答。

由于这是一个很好阅读的问题,我将从分析角度给出相同的答案。

KNN函数通过计算点之间的欧几里德距离来对数据点进行分类。这是一个需要数字的数学计算。因此,KNN中的所有变量必须能够强制数值。

KNN的数据准备通常涉及三个任务:
(1)修正所有NA或“”值
(2)将所有因子转换为一组布尔值,每个因子对应一个布尔值 (3)将每个变量的值归一化到0:1的范围,这样变量的范围就不会对距离测量产生过大的影响。

答案 2 :(得分:0)

我还要指出,使用整数时函数似乎失败了。我需要将所有内容转换为&#34; num&#34;在调用knn函数之前键入。这包括目标功能,R中的大多数方法都使用因子类型。因此,as.numeric(my_frame $ target_feature)是必需的。