knn {clustering} | R Documentation |
k-nearest neighbour classification for test set from training set. For each
row of the test set, the k nearest (in Euclidean distance) training set
vectors are found, and the classification is decided by majority vote, with
ties broken at random. If there are ties for the kth nearest vector, all
candidates are included in the vote.
knn(train, test, cl,
k = 1,
l = 0,
prob = FALSE,
use.all = TRUE);
Factor of classifications of test set. doubt will be returned as NA.
imports "dataset" from "MLkit";
data(bezdekIris);
print(bezdekIris);
# create training/test for demo
set.seed(123);
let [training, test] = split_training_test(iris, ratio = 0.7);
let predictions = knn(train = training[, -"class"], test = test[, -"class"], cl = training$class, k = 3);
print(predictions);