learnKNN: Fit models and make predictions with a KNN classifier

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/m07-KNN.R

Description

These functions are used to apply the generic train-and-test mechanism to a K-nearest neighbors (KNN) classifier.

Usage

1
2
learnKNN(data, status, params, pfun)
predictKNN(newdata, details, status, ...)

Arguments

data

The data matrix, with rows as features and columns as the samples to be classified.

status

A factor, with two levels, classifying the samples. The length must equal the number of data columns.

params

A list of additional parameters used by the classifier; see Details.

pfun

The function used to make predictions on new data, using the trained classifier.

newdata

Another data matrix, with the same number of rows as data.

details

A list of additional parameters describing details about the particular classifier; see Details.

...

Optional extra parameters required by the generic "predict" method.

Details

The input arguments to both learnKNN and predictKNN are dictated by the requirements of the general train-and-test mechanism provided by the Modeler-class.

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.

Value

The learnKNN function returns an object of the FittedModel-class, logically representing a KNN classifier that has been fitted on a training data set.

The predictKNN function returns a factor containing the predictions of the model when applied to the new data set.

Author(s)

Kevin R. Coombes <krc@silicovore.com>

References

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.

See Also

See Modeler-class and Modeler for details about how to train and test models. See FittedModel-class and FittedModel for details about the structure of the object returned by learnPCALR.

Examples

 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)

Example output

Loading required package: ClassDiscovery
Loading required package: cluster
Loading required package: oompaBase
Loading required package: ClassComparison
 [1] A B A A B B A A A B B B B B B B A B B A A A B B B B A A B A
Levels: A B

Modeler documentation built on May 7, 2019, 1:03 a.m.