Skip to content

Instantly share code, notes, and snippets.

@bdewilde
Created October 27, 2012 00:52
Show Gist options
  • Save bdewilde/3962506 to your computer and use it in GitHub Desktop.
Save bdewilde/3962506 to your computer and use it in GitHub Desktop.
benchmark knn model from kaggle hand-written digit classification competition
# fast nearest neighbor package
library(FNN)
# training and test sets, with variable names in the first row
train <- read.csv("train.csv", header=TRUE)
test <- read.csv("test.csv", header=TRUE)
# split train data frame in two
# first column is class labels (0, 1, ..., 9)
labels <- train[,1]
train <- train[,-1]
# save only the knn's class predictions for each observation in test set
results <- (0:9)[knn(train, test, labels, k = 10, algorithm="cover_tree")]
# output to file as a single column of class predictions
write(results, file="knn_benchmark.csv", ncolumns=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment