Skip to contents

Computes k nearest neighbors from embedding.

Usage

emb2knn(x, k, BNPARAM = NULL)

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.

Value

A knn list.

Examples

d1 <- mockData()
emb2knn(as.matrix(d1[,seq_len(2)]),k=5)
#> $index
#>       [,1] [,2] [,3] [,4] [,5]
#>  [1,]   19   18   17   20   22
#>  [2,]   15   14    4   11    3
#>  [3,]   33    9   12    5   29
#>  [4,]   14   25   15   22    2
#>  [5,]    9   23    8   33   34
#>  [6,]   13   24   10    7   12
#>  [7,]   12   29   10    6   33
#>  [8,]   36   23    5   11   21
#>  [9,]    5   33    3   23    8
#> [10,]   13    6    7   12   29
#> [11,]   21   16    8    2   15
#> [12,]   29    7   33    3    9
#> [13,]    6   10   24    7   12
#> [14,]    4   15    2   25   22
#> [15,]   14    2    4   25   11
#> [16,]   21   11   15    2    8
#> [17,]   20   19   18   22   25
#> [18,]   22   20   25    4   14
#> [19,]    1   18   20   17   22
#> [20,]   18   17   22   25   19
#> [21,]   11   16    8   36   23
#> [22,]   25    4   18   14   15
#> [23,]   36    8   34    5    9
#> [24,]    6   13   10    7   25
#> [25,]    4   22   14   15   18
#> [26,]   32   28   40   38   21
#> [27,]   39   38   34   31   30
#> [28,]   40   26   38   32   39
#> [29,]   12   33    7    3    9
#> [30,]   37   31   34   27    5
#> [31,]   34    5    9   23   33
#> [32,]   26   28   40   38   21
#> [33,]    9    3   29   12    5
#> [34,]   23   31    5   36    8
#> [35,]   37   30   39   27   31
#> [36,]    8   23    5   34   38
#> [37,]   30   31   34   27   33
#> [38,]   36   23   34   27    8
#> [39,]   27   40   38   28   34
#> [40,]   28   39   38   27   26
#> 
#> $distance
#>            [,1]      [,2]      [,3]      [,4]      [,5]
#>  [1,] 0.9351326 1.8240751 2.0100110 2.0516017 2.0775787
#>  [2,] 0.2521830 0.3587982 0.5079719 0.6249692 0.6466524
#>  [3,] 0.3677335 0.4199126 0.5664558 0.6180117 0.6398327
#>  [4,] 0.1512093 0.2934633 0.3548648 0.3576453 0.5079719
#>  [5,] 0.2106084 0.4129520 0.4888427 0.5383705 0.5691441
#>  [6,] 0.2583704 0.6917553 0.7248265 0.7646004 1.1390793
#>  [7,] 0.3825643 0.5295547 0.7269356 0.7646004 0.8531056
#>  [8,] 0.2593973 0.3345982 0.4888427 0.5499476 0.5524783
#>  [9,] 0.2106084 0.3481019 0.4199126 0.6208060 0.6360720
#> [10,] 0.5664998 0.7248265 0.7269356 1.0379233 1.0855443
#> [11,] 0.3341474 0.4319729 0.5499476 0.6249692 0.6933050
#> [12,] 0.2018095 0.3825643 0.4731934 0.5664558 0.8029753
#> [13,] 0.2583704 0.5664998 0.7219823 0.8562987 1.2385636
#> [14,] 0.1512093 0.2471735 0.3587982 0.3723327 0.5076262
#> [15,] 0.2471735 0.2521830 0.3548648 0.6181078 0.6933050
#> [16,] 0.3701047 0.4319729 0.7748010 0.8458200 0.8870181
#> [17,] 0.6646962 1.1371694 1.1403808 1.4578285 1.6774095
#> [18,] 0.3611668 0.5651806 0.6767089 0.7077195 0.8588908
#> [19,] 0.9351326 0.9841743 1.1192142 1.1371694 1.3053776
#> [20,] 0.5651806 0.6646962 0.8217170 1.0142912 1.1192142
#> [21,] 0.3341474 0.3701047 0.5524783 0.6750143 0.8768693
#> [22,] 0.3387363 0.3576453 0.3611668 0.5076262 0.6935692
#> [23,] 0.2830905 0.3345982 0.3928029 0.4129520 0.6208060
#> [24,] 0.6917553 0.7219823 1.2826442 1.4527218 1.6210153
#> [25,] 0.2934633 0.3387363 0.3723327 0.6181078 0.6767089
#> [26,] 0.5568654 1.1745553 1.6881958 1.8314100 1.8387386
#> [27,] 0.6698111 0.9236015 0.9605452 1.1882100 1.1913554
#> [28,] 0.5180691 1.1745553 1.1950878 1.2587367 1.5667422
#> [29,] 0.2018095 0.4178786 0.5295547 0.6398327 0.7659675
#> [30,] 0.2652954 0.7892914 1.0616034 1.1913554 1.3796854
#> [31,] 0.4227474 0.5916806 0.6777386 0.6993530 0.7647443
#> [32,] 0.5568654 1.2587367 1.7603752 2.1998626 2.3788764
#> [33,] 0.3481019 0.3677335 0.4178786 0.4731934 0.5383705
#> [34,] 0.3928029 0.4227474 0.5691441 0.6370769 0.7250634
#> [35,] 1.7177422 1.8226267 1.9348844 1.9370287 2.5228281
#> [36,] 0.2593973 0.2830905 0.6325422 0.6370769 0.6731068
#> [37,] 0.2652954 1.0275188 1.3238471 1.4040984 1.5802980
#> [38,] 0.6731068 0.7874505 0.8430430 0.9236015 0.9324859
#> [39,] 0.6698111 1.0852918 1.2545840 1.5667422 1.5953819
#> [40,] 0.5180691 1.0852918 1.1263752 1.3968236 1.6881958
#>