带有R DMwR封装的KNN

时间:2013-10-30 18:53:23

标签: r nlp ml knn

我正在尝试在文本上运行KNN。但是我不断收到下面的错误。有什么想法吗?

当我运行此代码时:

nn3 <- kNN(Type ~ .,trainQB,testQB,norm=FALSE,k=3)

我一直收到这个错误:

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

我尝试更改测试并将数据训练到矢量:

nn3 <- kNN(as.vector(Type) ~ .,as.vector(trainQB$Category),
       as.vector(testQB$Category),norm=FALSE,k=3)

我收到此错误:

Error in train[, -tgtCol] : incorrect number of dimensions

这是我的数据的示例:

**Error      Category         Type**
signup      Account issue     1
login       Account issue     2

以下是我的其余代码:

View(QBdata)
labels<-QBdata$Type)
View(labels)
idxs <- sample(1:nrow(QBdata),as.integer(0.7*nrow(QBdata)))
trainQB <- QBdata[idxs,]
testQB <- QBdata[-idxs,]
nn3 <- kNN(Type ~ .,trainQB,testQB,norm=FALSE,k=3)

根据DMwR包,函数KNN以这种方式编写:

kNN(form, train, test, norm = T, norm.stats = NULL, ...)

其中form是描述分类模型的功能形式的类公式的对象。

当我运行dput(head(trainQB))时,我得到:

structure(list(Error = structure(c(45L, 23L, 67L, 63L, 12L, 27L
), .Label = c("10k limit switch to online", "access", "accounting ....class = "factor"), Category = structure(c(9L, 
11L, 2L, 7L, 4L, 7L), .Label = c("Access errors", "Account", 
"Bank sync errors", "Convert ", "General Error", "Install errors", 
"plan features", "pricing issues", "setup errors", "signup errors", 
"upgrade errors"), class = "factor"), Type = c(6L, 5L, 8L, 9L, 
7L, 9L)), .Names = c("Error", "Category", "Type"), row.names = c(39L, 
34L, 51L, 53L, 41L, 64L), class = "data.frame"

谢谢!

0 个答案:

没有答案