R/KNN1.R

Defines functions KNN1

Documented in KNN1

# Simple kNN classifier with features resized between 0 and 1
# ----------------------------------------------------------------------
#' kNN classifier
#'
#' Classify the input with a k nearest neighbors classifier.
#'
#' @param data A list generated by the function PrepareData. It is a list with two components. 
#' One is a vector of labels, the other is a data frame of features. The other element of the list
#' is a data frame of test features.
#' @param k.seq A vector vector of vaues of k that should be tested.
#' @return A list with a dataframe of errors with corresponding k and a vector of predictions with
#' the best k
#' @import class
#' @import doParallel
#' @import iterators parallel
#' @import foreach
#' @export
#' @examples 
#' # create dataset
#' path <- "/home/rishabh/mres/ml_comp/data/"
#' data <- PrepareData(path, mode = 2, sample = TRUE, size = 100)
#' k.seq <- seq(1, 15, 2)
#' knn.result <- KNN1(data, k.seq)

KNN1 <- function(data, k.seq){
  # Create cluster with desired number of cores
  cl <- makeCluster(detectCores() - 1)
  # Register cluster
  registerDoParallel(cl)
  # Find out how many

  x <- foreach(i = k.seq, .combine = c) %dopar% {
    1 - sum(class::knn.cv(data$train$features, cl = data$train$label, k = i) == data$train$label)/length(data$train$label)
    }

  stopCluster(cl)

  prediction <- class::knn(train = data$train$features,
             test = data$test, cl = data$train$label,k = k.seq[which.min(x)])
  
  knn.err.df <- data.frame(k = seq(1, 15, 2), error = x)

  return(list(error = knn.err.df, prediction= prediction))
}
rishi1226/classrish documentation built on May 27, 2019, 9:10 a.m.