# You can write R code here and then click "Run" to run it on our platform

library(readr)

# The competition datafiles are in the directory ../input
train <- read_csv("../input/train.csv")
test <- read_csv("../input/test.csv")


library(FNN)
library(caret)

set.seed(1234)

# Splitting the train data into 67:33 
sample_index = createDataPartition(train$label, p = .67, list = FALSE, times = 1)

# Set the Dimension of the train & test
dim(train)
dim(test)

# KNN method at k = 5
pc <- proc.time()
fnn.kd <- knn(train[,-1], test, train$label , k=5 , algorithm = c("kd_tree"))
proc.time() - pc


submission <- data.frame(ImageId=1:nrow(test), Label=fnn.kd)
head(submission)

write_csv(submission, "KNN_submit.csv") 