Description Usage Arguments Value Examples
Using nearest neighbors algorithm for cross validation to find out the best K and give a prediction of the test data based on it.
1 2 |
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 |
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
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))]
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.