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

if (requireNamespace("kknn", quietly = TRUE)) {
# Define the Learner and set parameter values
learner = lrn("regr.kknn")
print(learner)

# 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)

# 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()
}
#> <LearnerRegrKKNN:regr.kknn>: k-Nearest-Neighbor
#> * Model: -
#> * Parameters: k=7
#> * Packages: mlr3, mlr3learners, kknn
#> * Predict Types:  [response]
#> * Feature Types: logical, integer, numeric, factor, ordered
#> * Properties: -
#> $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 17.02     0 2.875
#>  2:  22.8     1     1     4 108.0  3.85     4    93 18.61     1 2.320
#>  3:  21.4     0     1     6 258.0  3.08     3   110 19.44     1 3.215
#>  4:  18.7     0     2     8 360.0  3.15     3   175 17.02     0 3.440
#>  5:  18.1     0     1     6 225.0  2.76     3   105 20.22     1 3.460
#>  6:  14.3     0     4     8 360.0  3.21     3   245 15.84     0 3.570
#>  7:  24.4     0     2     4 146.7  3.69     4    62 20.00     1 3.190
#>  8:  22.8     0     2     4 140.8  3.92     4    95 22.90     1 3.150
#>  9:  19.2     0     4     6 167.6  3.92     4   123 18.30     1 3.440
#> 10:  16.4     0     3     8 275.8  3.07     3   180 17.40     0 4.070
#> 11:  15.2     0     3     8 275.8  3.07     3   180 18.00     0 3.780
#> 12:  14.7     0     4     8 440.0  3.23     3   230 17.42     0 5.345
#> 13:  32.4     1     1     4  78.7  4.08     4    66 19.47     1 2.200
#> 14:  15.2     0     2     8 304.0  3.15     3   150 17.30     0 3.435
#> 15:  13.3     0     4     8 350.0  3.73     3   245 15.41     0 3.840
#> 16:  19.2     0     2     8 400.0  3.08     3   175 17.05     0 3.845
#> 17:  27.3     1     1     4  79.0  4.08     4    66 18.90     1 1.935
#> 18:  26.0     1     2     4 120.3  4.43     5    91 16.70     0 2.140
#> 19:  15.8     1     4     8 351.0  4.22     5   264 14.50     0 3.170
#> 20:  19.7     1     6     6 145.0  3.62     5   175 15.50     0 2.770
#> 21:  15.0     1     8     8 301.0  3.54     5   335 14.60     0 3.570
#>       mpg    am  carb   cyl  disp  drat  gear    hp  qsec    vs    wt
#> 
#> $pv
#> $pv$k
#> [1] 7
#> 
#> 
#> $kknn
#> NULL
#> 
#> regr.mse 
#> 17.01076