Learn R Programming

daltoolboxdp (version 1.2.737)

skcla_knn: K-Nearest Neighbors Classifier

Description

Implements classification using the k-Nearest Neighbors algorithm. Wraps scikit-learn's KNeighborsClassifier through reticulate.

Usage

skcla_knn(
  attribute,
  slevels,
  n_neighbors = 5,
  weights = "uniform",
  algorithm = "auto",
  leaf_size = 30,
  p = 2,
  metric = "minkowski",
  metric_params = NULL,
  n_jobs = NULL
)

Value

A skcla_knn classifier object.

Arguments

attribute

Target attribute name for model building

slevels

List of possible values for classification target

n_neighbors

Number of neighbors to use for queries

weights

Weight function used in prediction ('uniform', 'distance')

algorithm

Algorithm used to compute nearest neighbors ('auto', 'ball_tree', 'kd_tree', 'brute')

leaf_size

Leaf size passed to BallTree or KDTree

p

Power parameter for the Minkowski metric

metric

Distance metric for the tree ('euclidean', 'manhattan', 'chebyshev', 'minkowski', etc.)

metric_params

Additional parameters for the metric function

n_jobs

Number of parallel jobs for neighbor searches

Details

K-Nearest Neighbors Classifier

References

Cover, T., & Hart, P. (1967). Nearest neighbor pattern classification.

Examples

Run this code
if (FALSE) {
data(iris)

# 1) Initialize KNN (k=7) with target attribute + levels
clf <- skcla_knn(attribute = 'Species', slevels = levels(iris$Species), n_neighbors = 7)

# 2) Fit and predict; factors are handled internally
clf <- daltoolbox::fit(clf, iris)
pred <- predict(clf, iris)
table(pred, iris$Species)
}

# More examples:
# https://github.com/cefet-rj-dal/daltoolboxdp/blob/main/examples/skcla_knn.md

Run the code above in your browser using DataLab