首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >knn函数出错

knn函数出错
EN

Stack Overflow用户
提问于 2013-06-01 23:03:55
回答 3查看 39.1K关注 0票数 13

我试着运行这行代码:

代码语言:javascript
复制
knn(mydades.training[,-7],mydades.test[,-7],mydades.training[,7],k=5)

但我总是得到这样的错误:

代码语言:javascript
复制
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定义如下:

代码语言:javascript
复制
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,]
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-02 10:24:04

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

代码语言:javascript
复制
NA/NaN/Inf in foreign function call (arg 6)

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

代码语言:javascript
复制
 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函数。因为你有两个错误

代码语言:javascript
复制
NAs introduced by coercion

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

代码语言:javascript
复制
as.double(train)

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

代码语言:javascript
复制
# 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(训练)’和‘as.double(测试)’给出的。由于您没有向我们提供'mydades‘的确切细节,下面是我最好的一些猜测(以及人工多变量正态分布数据):

代码语言:javascript
复制
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)

我不会将数字数据和类标签都放在一个矩阵中,也许您可以将数据拆分为:

代码语言:javascript
复制
mydadesnumeric <- mydades[,1:6] # 6 first columns
mydadesclasses <- mydades[,7]

使用调用

代码语言:javascript
复制
str(mydades); summary(mydades)

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

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

代码语言:javascript
复制
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)
票数 29
EN

Stack Overflow用户

发布于 2016-07-15 21:26:59

@Teemu回答得很好。

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

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

KNN的数据准备通常涉及三个任务:

(1)固定所有NA或"“值

(2)将所有因子转换为一组布尔值,因子中的每个级别对应一个布尔值

(3)将每个变量的值归一化到0:1的范围内,这样变量的范围就不会对距离测量产生过大的影响。

票数 12
EN

Stack Overflow用户

发布于 2017-03-21 01:19:18

我还要指出的是,当使用整数时,函数似乎失败了。在调用knn函数之前,我需要将所有内容转换为"num“类型。这包括目标特性,R中的大多数方法都使用factor类型。因此,需要as.numeric(my_frame$target_feature)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16874038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档