我有以下数据
structure(list(Color = c(0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0,1,0, 0, 0, 1, 1, 0, 1, 0), Size = c(1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1,0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1), Act = c(1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0,1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1), Age = c(0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,1,0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1), Inflated = c("F", "F", "F", "F", "F","F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T")), row.names = c(NA, -28L), class = c("tbl_df", "tbl", "data.frame"))
我已经使用这段代码尝试在数据集上运行knn测试。它已经在我以前使用过的数据集上工作过,我已经对它进行了调整以适应这一个。
Inf_True <- originaldata%>%
filter(Inflated == "T")
Inf_False <- originaldata%>%
filter(Inflated == "F")
set.seed(4567)
# samples
ran_true <- sample(1:nrow(Inf_True), 0.8*nrow(Inf_True))
ran_false <- sample(1:nrow(Inf_False), 0.8*nrow(Inf_False))
ran <- sample(1:nrow(originaldata), 0.8*nrow(originaldata))
# Create training set
Training_set <- rbind(Inf_True[ran_true,], Inf_False[ran_false,])
# Create testing set
Test_set <- rbind(Inf_True[-ran_true,], Inf_False[-ran_false,])
# extract 5th column of train dataset because it will be used as 'cl' argument in knn function
target_category <- originaldata[ran,5]
a<-target_category$Inflated
aa<-factor(a)
# extract 5th column of test dataset to measure the accuracy
test_category <- originaldata[-ran,5]
library(class)
## Knn = 1
#run knn function
print("Knn = 1")
knn_one <- knn(Training_set, Test_set, cl=aa, k=1)
我得到这个错误:
警告:强制引入的NAs警告:knn(训练集,测试集,cl = aa,k = 1)中强制错误引入的NA:外部函数调用中的NA/NaN/Inf(参数6)
我想这和“a”有关,但我不确定。
1条答案
按热度按时间wlzqhblo1#
将字符列转换为数字:
不能是文本(字符)。