NNLearnCV: Nearest Neighbors Learning Algorithm

Description Usage Arguments Value Examples

Description

This is a learning algorithm that uses cross-validation to select the number of neighbors that minimizes the mean validation loss

Usage

1
2
NNLearnCV(X.mat, y.vec, max.neighbors = 30, fold.vec = NULL,
  n.folds = 5)

Arguments

X.mat

a training data set

y.vec

a training data set

max.neighbors=30

The max number of neighbors to fin the best k in nearest neighbors

fold.vec=NULL

is a vector of fold ID numbers. If fold.vec is NULL randomly assign fold IDs from 1 to n.folds

n.folds=5

is the number of folds used to compute error

Value

returnList a list containing: X.mat - training data y.vec - training data train.loss.mat - matrice of loss values for each fold and number of neighbors validation.loss.mat - matrice of loss values for each fold and number of neighbors train.loss.vec - vector with max.neighbors elements: mean loss over all folds validation.loss.vec - vector with max.neighbors elements: mean loss over all folds selected.neighbors - number of neighbors selected by minimizing the mean validation loss predict(testX.mat) - a function that takes a matrix of inputs/features and returns a vector of predictions. It should check the type/dimension of testX.mat and stop() with an informative error message if there are any issues.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
   library(codungProject1)
   
   data(zip.train, package = "ElemStatLearn")
   X.mat<-zip.train[1:50,-1]
   y.vec<-zip.train[1:50, 1]
   max.neighbors <- 30
   n.folds <- 7
   fold.vec <- sample(rep(1:n.folds, l=nrow(X.mat)))
   
   result <- NNLearnCV(X.mat, y.vec, max.neighbors, fold.vec, n.folds)

mertayD/Coding_Project_1_NN documentation built on June 1, 2019, 3:57 a.m.