Skip to contents

k-Nearest-Neighbor regression. Calls kknn::kknn() from package kknn.

Note

There is no training step for k-NN models, just storing the training data to process it during the predict step. Therefore, $model returns a list with the following elements:

  • formula: Formula for calling kknn::kknn() during $predict().

  • data: Training data for calling kknn::kknn() during $predict().

  • pv: Training parameters for calling kknn::kknn() during $predict().

  • kknn: Model as returned by kknn::kknn(), only available after $predict() has been called. This is not stored by default, you must set hyperparameter store_model to TRUE.

Initial parameter values

  • store_model:

    • See note.

Dictionary

This mlr3::Learner can be instantiated via the dictionary mlr3::mlr_learners or with the associated sugar function mlr3::lrn():

mlr_learners$get("regr.kknn")
lrn("regr.kknn")

Meta Information

  • Task type: “regr”

  • Predict Types: “response”

  • Feature Types: “logical”, “integer”, “numeric”, “factor”, “ordered”

  • Required Packages: mlr3, mlr3learners, kknn

Parameters

IdTypeDefaultLevelsRange
kinteger7\([1, \infty)\)
distancenumeric2\([0, \infty)\)
kernelcharacteroptimalrectangular, triangular, epanechnikov, biweight, triweight, cos, inv, gaussian, rank, optimal-
scalelogicalTRUETRUE, FALSE-
ykerneluntypedNULL-
store_modellogicalFALSETRUE, FALSE-

References

Hechenbichler, Klaus, Schliep, Klaus (2004). “Weighted k-nearest-neighbor techniques and ordinal classification.” Technical Report Discussion Paper 399, SFB 386, Ludwig-Maximilians University Munich. doi:10.5282/ubm/epub.1769 .

Samworth, J R (2012). “Optimal weighted nearest neighbour classifiers.” The Annals of Statistics, 40(5), 2733–2763. doi:10.1214/12-AOS1049 .

Cover, Thomas, Hart, Peter (1967). “Nearest neighbor pattern classification.” IEEE transactions on information theory, 13(1), 21–27. doi:10.1109/TIT.1967.1053964 .

See also

Other Learner: mlr_learners_classif.cv_glmnet, mlr_learners_classif.glmnet, mlr_learners_classif.kknn, mlr_learners_classif.lda, mlr_learners_classif.log_reg, mlr_learners_classif.multinom, mlr_learners_classif.naive_bayes, mlr_learners_classif.nnet, mlr_learners_classif.qda, mlr_learners_classif.ranger, mlr_learners_classif.svm, mlr_learners_classif.xgboost, mlr_learners_regr.cv_glmnet, mlr_learners_regr.glmnet, mlr_learners_regr.km, mlr_learners_regr.lm, mlr_learners_regr.nnet, mlr_learners_regr.ranger, mlr_learners_regr.svm, mlr_learners_regr.xgboost

Super classes

mlr3::Learner -> mlr3::LearnerRegr -> LearnerRegrKKNN

Methods

Inherited methods


Method new()

Creates a new instance of this R6 class.

Usage


Method clone()

The objects of this class are cloneable with this method.

Usage

LearnerRegrKKNN$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Define the Learner and set parameter values
learner = lrn("regr.kknn")
print(learner)
#> 
#> ── <LearnerRegrKKNN> (regr.kknn): k-Nearest-Neighbor ───────────────────────────
#> • Model: -
#> • Parameters: k=7
#> • Packages: mlr3, mlr3learners, and kknn
#> • Predict Types: [response]
#> • Feature Types: logical, integer, numeric, factor, and ordered
#> • Encapsulation: none (fallback: -)
#> • Properties:
#> • Other settings: use_weights = 'error', predict_raw = 'FALSE'

# Define a Task
task = tsk("mtcars")

# Create train and test set
ids = partition(task)

# Train the learner on the training ids
learner$train(task, row_ids = ids$train)

# Print the model
print(learner$model)
#> $formula
#> mpg ~ .
#> NULL
#> 
#> $data
#>       mpg    am  carb   cyl  disp  drat  gear    hp  qsec    vs    wt
#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#>  1:  21.0     1     4     6 160.0  3.90     4   110 16.46     0 2.620
#>  2:  21.0     1     4     6 160.0  3.90     4   110 17.02     0 2.875
#>  3:  22.8     1     1     4 108.0  3.85     4    93 18.61     1 2.320
#>  4:  21.4     0     1     6 258.0  3.08     3   110 19.44     1 3.215
#>  5:  18.7     0     2     8 360.0  3.15     3   175 17.02     0 3.440
#>  6:  14.3     0     4     8 360.0  3.21     3   245 15.84     0 3.570
#>  7:  19.2     0     4     6 167.6  3.92     4   123 18.30     1 3.440
#>  8:  17.8     0     4     6 167.6  3.92     4   123 18.90     1 3.440
#>  9:  16.4     0     3     8 275.8  3.07     3   180 17.40     0 4.070
#> 10:  17.3     0     3     8 275.8  3.07     3   180 17.60     0 3.730
#> 11:  15.2     0     3     8 275.8  3.07     3   180 18.00     0 3.780
#> 12:  10.4     0     4     8 472.0  2.93     3   205 17.98     0 5.250
#> 13:  10.4     0     4     8 460.0  3.00     3   215 17.82     0 5.424
#> 14:  30.4     1     2     4  75.7  4.93     4    52 18.52     1 1.615
#> 15:  21.5     0     1     4 120.1  3.70     3    97 20.01     1 2.465
#> 16:  15.5     0     2     8 318.0  2.76     3   150 16.87     0 3.520
#> 17:  15.2     0     2     8 304.0  3.15     3   150 17.30     0 3.435
#> 18:  19.2     0     2     8 400.0  3.08     3   175 17.05     0 3.845
#> 19:  27.3     1     1     4  79.0  4.08     4    66 18.90     1 1.935
#> 20:  15.8     1     4     8 351.0  4.22     5   264 14.50     0 3.170
#> 21:  19.7     1     6     6 145.0  3.62     5   175 15.50     0 2.770
#>       mpg    am  carb   cyl  disp  drat  gear    hp  qsec    vs    wt
#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 
#> $pv
#> $pv$k
#> [1] 7
#> 
#> 
#> $kknn
#> NULL
#> 

# Importance method
if ("importance" %in% learner$properties) print(learner$importance())

# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)

# Score the predictions
predictions$score()
#> regr.mse 
#> 20.15076