Fast Nearest Neighbor Classification
Source:R/learner_FNN_classif_fnn.R
mlr_learners_classif.fnn.RdFast Nearest Neighbor Classification.
Calls FNN::knn() from FNN.
Meta Information
Task type: “classif”
Predict Types: “response”, “prob”
Feature Types: “integer”, “numeric”
Required Packages: mlr3, mlr3extralearners, FNN
Parameters
| Id | Type | Default | Levels | Range |
| k | integer | 1 | \([1, \infty)\) | |
| algorithm | character | kd_tree | kd_tree, cover_tree, brute | - |
References
Boltz, Sylvain, Debreuve, Eric, Barlaud, Michel (2007). “kNN-based high-dimensional Kullback-Leibler distance for tracking.” In Eighth International Workshop on Image Analysis for Multimedia Interactive Services (WIAMIS'07), 16–16. IEEE.
See also
as.data.table(mlr_learners)for a table of available Learners in the running session (depending on the loaded packages).Chapter in the mlr3book: https://mlr3book.mlr-org.com/chapters/chapter2/data_and_basic_modeling.html#sec-learners
mlr3learners for a selection of recommended learners.
mlr3cluster for unsupervised clustering learners.
mlr3pipelines to combine learners with pre- and postprocessing steps.
mlr3tuning for tuning of hyperparameters, mlr3tuningspaces for established default tuning spaces.
Super classes
mlr3::Learner -> mlr3::LearnerClassif -> LearnerClassifFNN
Methods
Inherited methods
mlr3::Learner$base_learner()mlr3::Learner$configure()mlr3::Learner$encapsulate()mlr3::Learner$format()mlr3::Learner$help()mlr3::Learner$predict()mlr3::Learner$predict_newdata()mlr3::Learner$print()mlr3::Learner$reset()mlr3::Learner$selected_features()mlr3::Learner$train()mlr3::LearnerClassif$predict_newdata_fast()
Examples
# Define the Learner
learner = lrn("classif.fnn")
print(learner)
#>
#> ── <LearnerClassifFNN> (classif.fnn): Fast Nearest Neighbor ────────────────────
#> • Model: -
#> • Parameters: list()
#> • Packages: mlr3, mlr3extralearners, and FNN
#> • Predict Types: [response] and prob
#> • Feature Types: integer and numeric
#> • Encapsulation: none (fallback: -)
#> • Properties: multiclass and twoclass
#> • Other settings: use_weights = 'error', predict_raw = 'FALSE'
# Define a Task
task = tsk("sonar")
# Create train and test set
ids = partition(task)
# Train the learner on the training ids
learner$train(task, row_ids = ids$train)
print(learner$model)
#> $train
#> V1 V10 V11 V12 V13 V14 V15 V16 V17 V18
#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1: 0.0453 0.2872 0.4918 0.6552 0.6919 0.7797 0.7464 0.9444 1.0000 0.8874
#> 2: 0.0100 0.1264 0.0881 0.1992 0.0184 0.2261 0.1729 0.2131 0.0693 0.2281
#> 3: 0.0286 0.3039 0.2988 0.4250 0.6343 0.8198 1.0000 0.9988 0.9508 0.9025
#> 4: 0.0317 0.3513 0.1786 0.0658 0.0513 0.3752 0.5419 0.5440 0.5150 0.4262
#> 5: 0.0123 0.0835 0.0548 0.0847 0.2026 0.2557 0.1870 0.2032 0.1463 0.2849
#> ---
#> 135: 0.0050 0.2282 0.2521 0.3484 0.3309 0.2614 0.1782 0.2055 0.2298 0.3545
#> 136: 0.0366 0.1847 0.2222 0.2648 0.2508 0.2291 0.1555 0.1863 0.2387 0.3345
#> 137: 0.0238 0.2048 0.2652 0.3100 0.2381 0.1918 0.1430 0.1735 0.1781 0.2852
#> 138: 0.0116 0.2271 0.3171 0.2882 0.2657 0.2307 0.1889 0.1791 0.2298 0.3715
#> 139: 0.0335 0.2660 0.3188 0.3553 0.3116 0.1965 0.1780 0.2794 0.2870 0.3969
#> V19 V2 V20 V21 V22 V23 V24 V25 V26 V27
#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1: 0.8024 0.0523 0.7818 0.5212 0.4052 0.3957 0.3914 0.3250 0.3200 0.3271
#> 2: 0.4060 0.0171 0.3973 0.2741 0.3690 0.5556 0.4846 0.3140 0.5334 0.5256
#> 3: 0.7234 0.0453 0.5122 0.2074 0.3985 0.5890 0.2872 0.2043 0.5782 0.5389
#> 4: 0.2024 0.0956 0.4233 0.7723 0.9735 0.9390 0.5559 0.5268 0.6826 0.5713
#> 5: 0.5824 0.0309 0.7728 0.7852 0.8515 0.5312 0.3653 0.5973 0.8275 1.0000
#> ---
#> 135: 0.6218 0.0017 0.7265 0.8346 0.8268 0.8366 0.9408 0.9510 0.9801 0.9974
#> 136: 0.5233 0.0421 0.6684 0.7766 0.7928 0.7940 0.9129 0.9498 0.9835 1.0000
#> 137: 0.5036 0.0318 0.6166 0.7616 0.8125 0.7793 0.8788 0.8813 0.9470 1.0000
#> 138: 0.6223 0.0744 0.7260 0.7934 0.8045 0.8067 0.9173 0.9327 0.9562 1.0000
#> 139: 0.5599 0.0258 0.6936 0.7969 0.7452 0.8203 0.9261 0.8810 0.8814 0.9301
#> V28 V29 V3 V30 V31 V32 V33 V34 V35 V36
#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1: 0.2767 0.4423 0.0843 0.2028 0.3788 0.2947 0.1984 0.2341 0.1306 0.4182
#> 2: 0.2520 0.2090 0.0623 0.3559 0.6260 0.7340 0.6120 0.3497 0.3953 0.3012
#> 3: 0.3750 0.3411 0.0277 0.5067 0.5580 0.4778 0.3299 0.2198 0.1407 0.2856
#> 4: 0.5429 0.2177 0.1321 0.2149 0.5811 0.6323 0.2965 0.1873 0.2969 0.5163
#> 5: 0.8673 0.6301 0.0169 0.4591 0.3940 0.2576 0.2817 0.2641 0.2757 0.2698
#> ---
#> 135: 1.0000 0.9036 0.0270 0.6409 0.3857 0.2908 0.2040 0.1653 0.1769 0.1140
#> 136: 0.9471 0.8237 0.0504 0.6252 0.4181 0.3209 0.2658 0.2196 0.1588 0.0561
#> 137: 0.9739 0.8446 0.0422 0.6151 0.4302 0.3165 0.2869 0.2017 0.1206 0.0271
#> 138: 0.9818 0.8684 0.0367 0.6381 0.3997 0.3242 0.2835 0.2413 0.2321 0.1260
#> 139: 0.9955 0.8576 0.0398 0.6069 0.3934 0.2464 0.1645 0.1140 0.0956 0.0080
#> V37 V38 V39 V4 V40 V41 V42 V43 V44 V45
#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1: 0.3835 0.1057 0.1840 0.0689 0.1970 0.1674 0.0583 0.1401 0.1628 0.0621
#> 2: 0.5408 0.8814 0.9857 0.0205 0.9167 0.6121 0.5006 0.3210 0.3202 0.4295
#> 3: 0.3807 0.4158 0.4054 0.0174 0.3296 0.2707 0.2650 0.0723 0.1238 0.1192
#> 4: 0.6153 0.4283 0.5479 0.1408 0.6133 0.5017 0.2377 0.1957 0.1749 0.1304
#> 5: 0.3994 0.4576 0.3940 0.0313 0.2522 0.1782 0.1354 0.0516 0.0337 0.0894
#> ---
#> 135: 0.0740 0.0941 0.0621 0.0450 0.0426 0.0572 0.1068 0.1909 0.2229 0.2203
#> 136: 0.0948 0.1700 0.1215 0.0250 0.1282 0.0386 0.1329 0.2331 0.2468 0.1960
#> 137: 0.0580 0.1262 0.1072 0.0399 0.1082 0.0360 0.1197 0.2061 0.2054 0.1878
#> 138: 0.0693 0.0701 0.1439 0.0225 0.1475 0.0438 0.0469 0.1476 0.1742 0.1555
#> 139: 0.0702 0.0936 0.0894 0.0570 0.1127 0.0873 0.1020 0.1964 0.2256 0.1814
#> V46 V47 V48 V49 V5 V50 V51 V52 V53 V54
#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1: 0.0203 0.0530 0.0742 0.0409 0.1183 0.0061 0.0125 0.0084 0.0089 0.0048
#> 2: 0.3654 0.2655 0.1576 0.0681 0.0205 0.0294 0.0241 0.0121 0.0036 0.0150
#> 3: 0.1089 0.0623 0.0494 0.0264 0.0384 0.0081 0.0104 0.0045 0.0014 0.0038
#> 4: 0.0597 0.1124 0.1047 0.0507 0.1674 0.0159 0.0195 0.0201 0.0248 0.0131
#> 5: 0.0861 0.0872 0.0445 0.0134 0.0358 0.0217 0.0188 0.0133 0.0265 0.0224
#> ---
#> 135: 0.2265 0.1766 0.1097 0.0558 0.0958 0.0142 0.0281 0.0165 0.0056 0.0010
#> 136: 0.1985 0.1570 0.0921 0.0549 0.0596 0.0194 0.0166 0.0132 0.0027 0.0022
#> 137: 0.2047 0.1716 0.1069 0.0477 0.0788 0.0170 0.0186 0.0096 0.0071 0.0084
#> 138: 0.1651 0.1181 0.0720 0.0321 0.0076 0.0056 0.0202 0.0141 0.0103 0.0100
#> 139: 0.2012 0.1688 0.1037 0.0501 0.0529 0.0136 0.0130 0.0120 0.0039 0.0053
#> V55 V56 V57 V58 V59 V6 V60 V7 V8 V9
#> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1: 0.0094 0.0191 0.0140 0.0049 0.0052 0.2583 0.0044 0.2156 0.3481 0.3337
#> 2: 0.0085 0.0073 0.0050 0.0044 0.0040 0.0368 0.0117 0.1098 0.1276 0.0598
#> 3: 0.0013 0.0089 0.0057 0.0027 0.0051 0.0990 0.0062 0.1201 0.1833 0.2105
#> 4: 0.0070 0.0138 0.0092 0.0143 0.0036 0.1710 0.0103 0.0731 0.1401 0.2083
#> 5: 0.0074 0.0118 0.0026 0.0092 0.0009 0.0102 0.0044 0.0182 0.0579 0.1122
#> ---
#> 135: 0.0027 0.0062 0.0024 0.0063 0.0017 0.0830 0.0028 0.0879 0.1220 0.1977
#> 136: 0.0059 0.0016 0.0025 0.0017 0.0027 0.0252 0.0027 0.0958 0.0991 0.1419
#> 137: 0.0038 0.0026 0.0028 0.0013 0.0035 0.0766 0.0060 0.0881 0.1143 0.1594
#> 138: 0.0034 0.0026 0.0037 0.0044 0.0057 0.0545 0.0035 0.1110 0.1069 0.1708
#> 139: 0.0062 0.0046 0.0045 0.0022 0.0005 0.1091 0.0031 0.1709 0.1684 0.1865
#>
#> $cl
#> [1] R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R
#> [38] R R R R R R R R R R R R R R R R R R R R R R R R R R R M M M M M M M M M M
#> [75] M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M
#> [112] M M M M M M M M M M M M M M M M M M M M M M M M M M M M
#> Levels: M R
#>
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.2173913