gpt4 book ai didi

r - k-NN 示例中的错误准确度度量

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:15 27 4
gpt4 key购买 nike

我是 k-NN 的新手,因此尝试通过 Datacamp 类(class):机器学习简介,使用 UCI 机器学习库中的泰坦尼克号数据集来学习它。

我已经完成了根据准确度 (accs) 选择最佳 k 参数的部分。但是,当我在 Rstudio 上复制练习时,我得到 k=1;答案应该是 k=73。我哪里出错了?

#load dataset
titanic_train<-read.csv("https://kaggle2.blob.core.windows.net/competitions-data/kaggle/3136/train.csv?sv=2015-12-11&sr=b&sig=coHTg7HQb86RdaxfD2f9SiN492A4XLIDDRKVxLvw8Ys%3D&se=2017-03-19T10%3A26%3A52Z&sp=r")

# Omit NAs
titanic_train<-na.omit(titanic_train[,c("Survived", "Pclass", "Age", "Sex")])
library(class)
library(dplyr)

set.seed(1)
n <- nrow(titanic_train)
shuffled <- titanic_train[sample(n), ]

# Split the data : train and test
train_indices <- 1:round(0.7 * n)
train <- shuffled[train_indices, ]
test_indices <- (round(0.7 * n) + 1):n
test <- shuffled[test_indices, ]

# Store the Survived column of train and test in train_labels and test_labels
train_labels<-train$Survived
test_labels<-test$Survived

# Copy train and test to knn_train and knn_test
train$Sex = as.numeric(factor(train$Sex))
train <- train %>% mutate(Sex=ifelse(Sex==2, 1,0))
train$Survived=factor(train$Survived)
summary(train$Sex)
knn_train<-train
knn_train$Age<-round(knn_train$Age,7)

test$Sex = as.numeric(factor(test$Sex))
test <- test %>% mutate(Sex=ifelse(Sex==2, 1,0))
test$Survived=factor(test$Survived)
summary(test$Sex)
knn_test<-test

# Drop Survived column for knn_train and knn_test
knn_train$Survived<-NULL
knn_test$Survived<-NULL

# Normalize Pclass
min_class <- min(knn_train$Pclass)
max_class <- max(knn_train$Pclass)
knn_train$Pclass <- (knn_train$Pclass - min_class) / (max_class - min_class)
knn_test$Pclass <- (knn_test$Pclass - min_class) / (max_class - min_class)

# Normalize Age
min_age <- min(knn_train$Age)
max_age <- max(knn_train$Age)
knn_train$Age <- (knn_train$Age-min_age)/(max_age-min_age)
knn_test$Age <- (knn_test$Age-min_age)/(max_age-min_age)
summary(train)

# Set random seed.
set.seed(1)

# define range and accs
range <- 1:round(0.2 * nrow(knn_train))
accs <- rep(0, length(range))

for (k in range) {
pred <- knn(knn_train, knn_test, train_labels, k = k)
pred<-factor(pred, levels=c(1,0))
test_labels<-factor(test_labels, levels=c(1,0))
conf <- table(test_labels, pred)
accs[k] <- sum(diag(conf)) / sum(conf)
}

# Plot the accuracies.
plot(range, accs, xlab = "k")

# Calculate the best k
which.max(accs)
accs[which.max(accs)]

最佳答案

我猜你想要你定义为最佳 k最佳 k(最后评论:# Calculate the best k)。 p>

然而调用它只会让你得到最大值

which.max(accs)
accs[which.max(accs)]

毫无疑问,k=1 时的最大精度为 0.8364486。

如果您想找到最佳的 k,您需要目视检查它(使用碎石图)。找到最长的弯头并设置 k 的值。 (这是一个主观决定,因人而异。)

关于r - k-NN 示例中的错误准确度度量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42882263/

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