knn {LearnSL}R Documentation

K-Nearest Neighbors

Description

This function applies knn algorithm to classify data.

Usage

knn(
  data,
  ClassLabel,
  p1,
  d_method = "euclidean",
  k,
  p = 3,
  details = FALSE,
  waiting = TRUE
)

Arguments

data

Data frame with already classified observations. Each column represents a parameter of the values. The last column contains the output, this means, the expected output when the other column values are inputs. Each row is a different observation.

ClassLabel

String containing the name of the column of the classes we want to classify

p1

Vector containing the parameters of the new value that we want to classify.

d_method

String with the name of the distance method that will be used. It must be one of "Euclidean", "Manhattan", "Cosine", "Chebyshev", "Minkowski", "Canberra", "Octile", "Hamming", "Binary"or "Jaccard". Where both "Hamming" and "Binary" use the same method, as it is known by both names.

k

Number of closest values that will be considered in order to classify the new value ("p1").

p

Exponent used in the Minkowski distance. 3 by default, otherwise if specified.

details

Boolean value. If it is set to "TRUE" multiple clarifications and explanations are printed along the code

waiting

If TRUE while details = TRUE. The code will stop in each "block" of code and wait for the user to press "enter" to continue.

Value

Value of the new classified example.

Author(s)

VĂ­ctor Amador Padilla, victor.amador@edu.uah.es

Examples

# example code
knn(db_flowers,"ClassLabel", c(4.7, 1.2, 5.3, 2.1), "chebyshev", 4)
knn(db_flowers,"ClassLabel", c(4.7, 1.5, 5.3, 2.1), "chebyshev", 5)
knn(db_flowers,"ClassLabel", c(6.7, 1.5, 5.3, 2.1), "Euclidean", 2, details = TRUE, waiting = FALSE)
knn(db_per_or,"y", c(1,1,1), "Hamming", 3, details = TRUE, waiting = FALSE)


[Package LearnSL version 1.0.0 Index]