gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:40:58 25 4
gpt4 key购买 nike

我是使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com