caret (version 5.07-005)

knn3: k-Nearest Neighbour Classification

Description

$k$-nearest neighbour classification that can return class votes for all classes.

Usage

## S3 method for class 'formula':
knn3(formula, data, subset, na.action, k = 5, ...) 

## S3 method for class 'matrix': knn3(x, y, k = 5, ...)

knn3Train(train, test, cl, k=1, l=0, prob = TRUE, use.all=TRUE)

Arguments

Value

Details

knn3 is essentially the same code as ipredknn and knn3Train is a copy of knn. The underlying C code from the class pacakge has been modifed to return the vote percentages for each class (previously the percentage for the winning class was returned).

Examples

Run this code
irisFit1 <- knn3(Species ~ ., iris)

irisFit2 <- knn3(as.matrix(iris[, -5]), iris[,5])

data(iris3)
train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])
cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
knn3Train(train, test, cl, k = 5, prob = TRUE)

Run the code above in your browser using DataCamp Workspace