Computes k nearest neighbors from embedding.
Arguments
- x
A numeric matrix (with features as columns and items as rows) from which nearest neighbors will be computed.
- k
The number of nearest neighbors.
- BNPARAM
A BiocNeighbors parameter object to compute kNNs. Ignored unless the input is a matrix or data.frame. If omitted, the Annoy approximation will be used if there are more than 500 elements.
Examples
d1 <- mockData()
emb2knn(as.matrix(d1[,1:2]),k=5)
#> $index
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 19 3 12 16 2
#> [2,] 16 13 19 12 6
#> [3,] 12 19 4 2 16
#> [4,] 13 12 6 2 18
#> [5,] 10 20 21 8 24
#> [6,] 13 18 2 35 4
#> [7,] 25 24 23 5 10
#> [8,] 21 20 10 5 17
#> [9,] 39 15 30 31 18
#> [10,] 20 5 21 8 24
#> [11,] 31 30 15 14 39
#> [12,] 19 2 3 4 16
#> [13,] 6 18 2 35 4
#> [14,] 31 11 17 30 15
#> [15,] 30 11 31 39 9
#> [16,] 2 19 6 12 13
#> [17,] 22 14 8 31 21
#> [18,] 35 6 13 39 9
#> [19,] 12 2 16 3 13
#> [20,] 10 21 5 8 24
#> [21,] 20 8 5 10 17
#> [22,] 17 26 21 8 14
#> [23,] 25 24 10 5 3
#> [24,] 5 10 20 25 21
#> [25,] 7 24 23 5 10
#> [26,] 22 28 17 14 21
#> [27,] 33 29 34 40 28
#> [28,] 26 32 34 27 22
#> [29,] 33 27 40 36 37
#> [30,] 15 11 31 39 9
#> [31,] 11 30 15 14 9
#> [32,] 38 28 34 26 27
#> [33,] 29 27 40 37 36
#> [34,] 27 28 32 38 29
#> [35,] 18 39 6 13 9
#> [36,] 37 40 33 11 39
#> [37,] 40 11 30 15 31
#> [38,] 32 34 28 26 27
#> [39,] 15 9 30 35 18
#> [40,] 37 11 31 14 30
#>
#> $distance
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.29566420 1.3421067 1.4715387 1.6048505 1.7086970
#> [2,] 0.37452175 0.4444869 0.4458890 0.4612560 0.4666427
#> [3,] 0.62968457 0.7546696 0.9842686 1.0909192 1.3163584
#> [4,] 0.60121886 0.6580306 0.6860728 0.7425212 0.8131569
#> [5,] 0.18657183 0.2548209 0.3146041 0.5396107 0.7037336
#> [6,] 0.08575145 0.3500797 0.4666427 0.4716242 0.6860728
#> [7,] 0.93513257 1.8240751 2.0100110 2.1228417 2.2680189
#> [8,] 0.25218300 0.3587982 0.5079719 0.5396107 0.6249692
#> [9,] 0.36773351 0.4199126 0.4412507 0.5610582 0.5664558
#> [10,] 0.15120930 0.1865718 0.3548648 0.5079719 0.7077195
#> [11,] 0.15292110 0.1869890 0.2106084 0.4888427 0.5383705
#> [12,] 0.25837036 0.4612560 0.6296846 0.6580306 0.7248265
#> [13,] 0.08575145 0.3825643 0.4444869 0.5295547 0.6012189
#> [14,] 0.40406159 0.4888427 0.5499476 0.6172360 0.6360720
#> [15,] 0.02363391 0.2106084 0.2321602 0.3481019 0.4199126
#> [16,] 0.37452175 0.5664998 0.7091540 0.7248265 0.7269356
#> [17,] 0.43197293 0.5499476 0.6249692 0.6805494 0.6933050
#> [18,] 0.20180947 0.3500797 0.3825643 0.4731934 0.5664558
#> [19,] 0.25837036 0.4458890 0.5664998 0.7546696 0.8562987
#> [20,] 0.15120930 0.2471735 0.2548209 0.3587982 0.8588908
#> [21,] 0.24717347 0.2521830 0.3146041 0.3548648 0.6933050
#> [22,] 0.43197293 0.5359545 0.7748010 0.8458200 0.8870181
#> [23,] 1.13716937 1.1403808 1.8144949 1.8405530 1.8694539
#> [24,] 0.70373359 0.7077195 0.8588908 0.9841743 1.0148838
#> [25,] 0.93513257 0.9841743 1.1371694 1.4900812 1.5841110
#> [26,] 0.53595454 0.8217170 0.8963990 1.1594005 1.2948579
#> [27,] 0.81728422 0.8768693 0.9860296 1.1192155 1.1657922
#> [28,] 0.82171700 0.9478323 1.0768017 1.1657922 1.1850271
#> [29,] 0.37241440 0.8768693 1.3225420 1.4892807 1.5601800
#> [30,] 0.02363391 0.1869890 0.2140727 0.3687323 0.4412507
#> [31,] 0.15292110 0.2140727 0.2321602 0.4040616 0.5610582
#> [32,] 0.55686537 0.9478323 1.1745553 1.3934585 1.8566828
#> [33,] 0.37241440 0.8172842 0.9605452 1.1882100 1.1913554
#> [34,] 0.98602961 1.0768017 1.1745553 1.2587367 1.6696433
#> [35,] 0.20180947 0.4178786 0.4716242 0.5295547 0.6398327
#> [36,] 0.78929140 1.0616034 1.1913554 1.3796854 1.4111328
#> [37,] 0.42274741 0.5916806 0.6659964 0.6777386 0.7444686
#> [38,] 0.55686537 1.2587367 1.4407066 1.9503217 2.1388739
#> [39,] 0.34810191 0.3677335 0.3687323 0.4178786 0.4731934
#> [40,] 0.42274741 0.5691441 0.6853185 0.7250634 0.7344199
#>