kNN: kNN classifier

Description Usage Arguments Value Examples

View source: R/kNN.R

Description

Classify the input with a k nearest neighbors classifier.

Usage

1
kNN(data, trueClasses, memory = NULL, k = 1, p = 2, type = "train")

Arguments

data

A data frame or a matrix where rows are observations and columns are features. If type is "train" this is training dataset, and if it is "predict" it is test dataset.

trueClasses

A vector with labels for each row in data if type is "train", and with labels for each row in memory if type is "predict".

memory

A data frame or a matrix where rows are observations and columns are features. If type is "train" this argument is not needed, and if it is "predict" it is a training dataset.

k

Number of neighbors that the classifier should use. It has to be an odd number.

p

Distance metric the classifier should use, the value can be either 1, 2 or Inf.

type

Whether the goal is to train the classifier or predict classes of new observations based on past ones. The value can be either "train" or "predict".

Value

A list with following elements: predictedClasses, prob, accuracy and errorCount.

Examples

1
2
3
4
5
6
# create artificial dataset
inputsTest   <- matrix(rnorm(200), ncol=2)
inputsTrain  <- matrix(rnorm(200), ncol=2)
classesTrain <- c(rep(0, 50), rep(1, 50))
# get the kNN predictions for the test set
kNN(inputsTest, classesTrain, inputsTrain, k=15, p=2, type="predict")

hstojic/hclass documentation built on May 17, 2019, 6:16 p.m.