我收到错误“ knn中的错误(火车= prc_train,测试= prc_test,cl = prc_train_labels ,:不允许缺少任何值”]]

问题描述 投票:0回答:1

我是使用R的数据科学初学者,无法解决此错误,并且使用的数据集是前列腺癌数据集。错误出现在prc_test_pred

,它表示knn中的错误(train = prc_train,test = prc_test,cl = prc_train_labels ,:不允许缺少任何值。
stringsAsFactors = FALSE 
str(prc) 
prc <- prc[-1]  #removes the first variable(id) from the data set.
table(prc$diagnosis_result)  # it helps us to get the numbers of patients
prc$diagnosis <- factor(prc$diagnosis_result, levels = c("B", "M"), labels = c("Benign", "Malignant")) #rename
round(prop.table(table(prc$diagnosis)) * 100, digits = 1)  # it gives the result in the percentage form rounded of to 1 decimal place( and so it’s digits = 1)

normalize <- function(x) {
  return ((x - min(x)) / (max(x) - min(x))) } #very important step (normalizes to a common scale)

prc_n <- as.data.frame(lapply(prc[2:9], normalize))
summary(prc_n$radius)
prc_train <- prc_n[1:65,]
prc_test <- prc_n[66:100,]

prc_train_labels <- prc[1:65, 1]
prc_test_labels <- prc[66:100, 1] 

library(class)

prc_test_pred <- knn(train = prc_train, test = prc_test, cl = prc_train_labels,k=10)
library(gmodels)
CrossTable(x=prc_test_labels, y=prc_test_pred, prop.chisq=FALSE) ```



我是使用R的数据科学的初学者,无法解决此错误,并且使用的数据集是前列腺癌数据集。错误发生在prc_test_pred,它表示knn中的错误(火车= prc_train,测试= ...

r machine-learning rstudio data-science knn
1个回答
1
投票

我不确定您面临什么样的问题。可能是您的第一行(读取csv文件的行有问题)。只是为了您重现,这是一个简单的分类器,使用KNN使用除代码第一行以外的所有代码

© www.soinside.com 2019 - 2024. All rights reserved.