#| child: aaa.Rmd
#| include: false

r descr_models("nearest_neighbor", "kknn")

Tuning Parameters

#| label: kknn-param-info
#| echo: false
defaults <- 
  tibble::tibble(parsnip = c("neighbors", "weight_func", "dist_power"),
                 default = c("5L", "'optimal'", "2.0"))

param <-
  nearest_neighbor() |> 
  set_engine("kknn") |> 
  make_parameter_list(defaults)

This model has r nrow(param) tuning parameters:

#| label: kknn-param-list
#| echo: false
#| results: asis
param$item

Parsnip changes the default range for neighbors to c(1, 15) and dist_power to c(1/10, 2).

Translation from parsnip to the original package (regression)

#| label: kknn-reg
nearest_neighbor(
  neighbors = integer(1),
  weight_func = character(1),
  dist_power = double(1)
) |>  
  set_engine("kknn") |> 
  set_mode("regression") |> 
  translate()

min_rows() will adjust the number of neighbors if the chosen value if it is not consistent with the actual data dimensions.

Translation from parsnip to the original package (classification)

#| label: kknn-cls
nearest_neighbor(
  neighbors = integer(1),
  weight_func = character(1),
  dist_power = double(1)
) |> 
  set_engine("kknn") |> 
  set_mode("classification") |> 
  translate()

Preprocessing requirements

#| child: template-makes-dummies.Rmd
#| child: template-same-scale.Rmd

Examples

The "Fitting and Predicting with parsnip" article contains examples for nearest_neighbor() with the "kknn" engine.

Case weights

#| child: template-no-case-weights.Rmd

Saving fitted model objects

#| child: template-butcher.Rmd

References



tidymodels/parsnip documentation built on June 2, 2025, 8:10 a.m.