Fit models and make predictions with a KNN classifier
These functions are used to apply the generic cross-validation mechanism to a K-nearest neighbors (KNN) classifier.
The data matrix, with rows as features and columns as the samples to be classified.
A factor, with two levels, classifying the samples. The length must
equal the number of
A list of additional parameters used by the classifier; see Details.
The function used to make predictions on new data, using the cross-validated classifier.
Another data matrix, with the same number of rows as
A list of additional parameters describing details about the particular classifier; see Details.
Optional extra parameters required by the generic "predict" method.
The input arguments to both
are dictated by the requirements of the general cross-validation
mechanism provided by the
The implementation uses the
knn method from the
class package. The
params argument to
learnKNN must be alist that at least includes the component
k that specifies the number of neighbors used.
learnKNN function returns an object of the
FittedModel-class, logically representing a KNN
classifier that has been fitted on a training
predictKNN function returns a factor containing the
predictions of the model when applied to the new data set.
Kevin R. Coombes <firstname.lastname@example.org>
Ripley, B. D. (1996) Pattern Recognition and Neural Networks. Cambridge.
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth edition. Springer.
Modeler for details
about how to peform cross-validation. See
details about the structure of the object returned by
1 2 3 4 5 6 7 8 9 10 11 12 13
# simulate some data data <- matrix(rnorm(100*20), ncol=20) status <- factor(rep(c("A", "B"), each=10)) # set up the parameter list knn.params <- list(k=5) # learn the model fm <- learnKNN(data, status, knn.params, predictKNN) # Make predictions on some new simulated data newdata <- matrix(rnorm(100*30), ncol=30) predictKNN(newdata, fm@details, status)
Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.