我正在尝试在文本上运行 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"
谢谢!