r - 我收到错误 "Error in knn(train = prc_train, test = prc_test, cl = prc_train_labels, : no missing values are allowed"

标签 r machine-learning rstudio data-science knn

我是使用 R 的数据科学初学者,无法解决此错误,并且使用的数据集是前列腺癌数据集。 错误位于 prc_test_pred 处,其中显示 Error in knn(train = prc_train, test = prc_test, cl = prc_train_labels, : no Missing Values are allowed.

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) ```



最佳答案

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

#
prc <- read.csv("https://raw.githubusercontent.com/duttashi/learnr/master/data/misc/Prostate_Cancer.csv", header = TRUE, stringsAsFactors = FALSE)

prc <- prc[-1]  

prc$diagnosis <- factor(prc$diagnosis_result, levels = c("B", "M"), labels = c("Benign", "Malignant"))


normalize <- function(x) {
  return ((x - min(x)) / (max(x) - min(x))) } 

prc_n <- as.data.frame(lapply(prc[2:9], normalize))


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) 

# -------------------------------------------------------------------------


# Cell Contents
#   |-------------------------|
#   |                       N |
#   |           N / Row Total |
#   |           N / Col Total |
#   |         N / Table Total |
#   |-------------------------|
#   
#   
#   Total Observations in Table:  35 
# 
# 
#                   | prc_test_pred 
#   prc_test_labels |         B |         M | Row Total | 
#   ----------------|-----------|-----------|-----------|
#                 B |         6 |        13 |        19 | 
#                   |     0.316 |     0.684 |     0.543 | 
#                   |     0.857 |     0.464 |           | 
#                   |     0.171 |     0.371 |           | 
#   ----------------|-----------|-----------|-----------|
#                 M |         1 |        15 |        16 | 
#                   |     0.062 |     0.938 |     0.457 | 
#                   |     0.143 |     0.536 |           | 
#                   |     0.029 |     0.429 |           | 
#   ----------------|-----------|-----------|-----------|
#      Column Total |         7 |        28 |        35 | 
#                   |     0.200 |     0.800 |           | 
#   ----------------|-----------|-----------|-----------|

希望你能重现同样的内容。

关于r - 我收到错误 "Error in knn(train = prc_train, test = prc_test, cl = prc_train_labels, : no missing values are allowed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58704951/

相关文章:

java - R中的自定义距离度量,用于聚集聚类

r - 如何在ggplot2中对齐旋转的多行x轴文本?

python - MNIST 和 SGDClassifier 分类器

python - 没有交叉验证的 Scikit Learn GridSearchCV(无监督学习)

python - python 中的优化函数

r - 在 r plotly barchart 中排序

r - 从字符串末尾开始使用 strsplit

r - 我的计算机在 r 中找不到我的 csv 文件

r - 使用 "twocolumn" "classoption"进行 Knit 转 PDF 时如何创建全宽图形?

github - 同一个 RStudio 中的两个不同 Github 帐户