我是使用R的数据科学初学者,无法解决此错误,并且使用的数据集是前列腺癌数据集。 错误出现在 prc_test_pred 上,该错误表示knn中的错误(火车= 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) ```