NNLearnCV: Cross-validation using nearest neighbors

Description Usage Arguments Value Examples

View source: R/NNLearnCV.R

Description

Using nearest neighbors algorithm for cross validation to find out the best K and give a prediction of the test data based on it.

Usage

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

Arguments

X.mat

numeric train feature matrix [n x p]

y.vec

numeric train label vector [n x 1], 0/1 for binary classification, real number for regression.

max.neighbors

scalar integer, max number of the neighbors

fold.vec

integer vector that holds fold ID number [n x 1]

n.folds

scalar integer, number of the folds

Value

A list that contains training features and labels, loss matrix and loss vector of training and validation sets, best neighbor number, and a predict function using learning result

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
####################Regression#################
data(zip.train, package = "ElemStatLearn")
X.mat <- zip.train[1:100, -1]
y.vec <- zip.train[1:100, 1]
testX.mat <- matrix(zip.train[101:105, -1],ncol = ncol(X.mat))
max.neighbors <- 30L
cv.list <- NNLearnCV(X.mat,y.vec,max.neighbors,NULL,5L)
cv.list$predict(testX.mat)
zip.train[101:105, 1]

#################Binary Classification################
data(spam, package = "ElemStatLearn")
n.folds = 3L
X.mat <- data.matrix(subset(spam,select = -c(spam)))
y.vec <- spam$spam
levels(y.vec) <- c(0,1)
y.vec <- as.double(as.vector(y.vec))
testX.mat <- X.mat[c(1,nrow(X.mat)),]
max.neighbors <- 30L
fold.vec <- sample(rep(1:n.folds, l = nrow(X.mat)))
C.pred.model <- NNLearnCV(X.mat, y.vec, max.neighbors, fold.vec, n.folds)
prediction.output <- C.pred.model$predict(testX.mat)
prediction.output
y.vec[c(1,nrow(X.mat))]

SixianZhang/CS499-Coding-Project-1 documentation built on May 26, 2019, 6:41 p.m.