1
私はk-NNを初めて使用しているため、UCI MLリポジトリのタイタニックデータセットで学ぶことをお勧めします。k-NNの精度が間違っています
私は精度(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)]