k-NN示例中的精度测量错误

时间:2017-03-19 02:28:44

标签: r algorithm parameters knn

我是k-NN的新手,因此尝试使用UCI ML回购中的泰坦尼克号数据集,通过Datacamp课程:ML简介。

我已经达成了基于准确度(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)]

1 个答案:

答案 0 :(得分:0)

我想你想要最佳k 你定义为最佳k (最后评论:#计算最佳k )。

但是,调用此选项只能获得最大值

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

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

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