Learn R Programming

daltoolbox (version 1.2.747)

cla_knn: K-Nearest Neighbors (KNN) Classification

Description

Classification by majority vote among the k nearest neighbors. Uses class::knn.

Usage

cla_knn(attribute, slevels, k = 1)

Value

returns a knn object.

Arguments

attribute

attribute target to model building.

slevels

possible values for the target classification.

k

a vector of integers indicating the number of neighbors to be considered.

Details

KNN is a simple, non‑parametric method. Choice of k trades bias/variance; distance metric is Euclidean by default.

References

Cover, T. and Hart, P. (1967). Nearest neighbor pattern classification. IEEE Trans. Info. Theory.

Examples

Run this code
data(iris)
slevels <- levels(iris$Species)
model <- cla_knn("Species", slevels, k=3)

# preparing dataset for random sampling
sr <- sample_random()
sr <- train_test(sr, iris)
train <- sr$train
test <- sr$test

model <- fit(model, train)

prediction <- predict(model, test)
predictand <- adjust_class_label(test[,"Species"])
test_eval <- evaluate(model, predictand, prediction)
test_eval$metrics

Run the code above in your browser using DataLab