Description Usage Arguments Details Value Examples
Obtain nearest neighbors and distances from a matrix or disto handle. k nearest or fixed radius neighbors are supported
1 |
x |
Object of class 'disto' or a numeric matrix |
k |
Number of nearest neighbors |
r |
Radius for nearest neighbors |
method |
(string or function) distance metric when x is a matrix. Passed to 'proxy::dist'. Ignored when x is not a matrix. |
... |
Additional arguments for |
Exactly one among k or r has to be provided
Object of class nn. A list with these elements:
triplet: Matrix with three columns: row, col and distance. For a fixed observation(value in 'row'), all corresponding values in 'col' are the indexes of the nearest neighbors. All corresponding values in 'distance' are the distances to those nearest neighbors
size: Size of the distance matrix or number of rows of the matrix
k or r : Depending on the input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ## Not run:
# create a matrix
set.seed(100)
mat <- cbind(rnorm(3e3), rpois(3e3, 1))
# compute a distance matrix and get a disto handle
do <- stats::dist(mat)
dio <- disto(objectname = "do")
# nearest neighbors: k nearest and fixed radius
nn(dio, k = 1)
nn(mat, k = 1) # distance method defaults to 'euclidean'
str(nn(mat, k = 1)) # observe the structure of the output
nn(dio, r = 0.1)
nn(mat, r = 0.1)
# nearest neighbors parallelized: k nearest and fixed radius
# fast computation, higher memory usage
nn(dio, k = 1, nproc = 2)
nn(mat, k = 1, mc.cores = 2)
nn(dio, r = 0.1, nproc = 2)
nn(mat, r = 0.1, mc.cores = 2)
# different distance method
do <- stats::dist(mat, method = "manhattan")
nn(dio, k = 1, nproc = 2)
nn(mat, k = 1, method = "manhattan", mc.cores = 2)
nn(dio, r = 0.1, nproc = 2)
nn(mat, r = 0.1, method = "manhattan", mc.cores = 2)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.