hnswSearcher
Hierarchical Navigable Small World (HNSW) nearest neighbor searcher class.
The hnswSearcher
class implements the HNSW algorithm for efficient
nearest neighbor queries. It stores training data and supports various
distance metrics for performing searches. The HNSW algorithm builds a
multilayer graph structure that enables fast approximate nearest neighbor
searches by navigating through the graph. It facilitates a nearest neighbor
search using knnsearch
.
You can either use the hnswSearcher
class constructor or the
createns
function to create an hnswSearcher
object.
See also: createns, ExhaustiveSearcher, KDTreeSearcher, knnsearch
Source Code: hnswSearcher
"minkowski"
, a positive scalar exponent (default 2).
"seuclidean"
, a nonnegative vector of scaling factors
matching the number of columns in X
(default is standard
deviation of X
).
"mahalanobis"
, a positive definite covariance matrix
matching the dimensions of X
(default is cov (X)
).
obj = hnswSearcher (X)
constructs an
hnswSearcher
object with training data X using the
default "euclidean"
distance metric. X must be an
numeric matrix, where rows represent observations and columns
represent features.
obj = hnswSearcher (X, name, value)
allows customization through name-value pairs:
Name | Value | |
---|---|---|
"Distance" | Distance metric, specified as a
character vector (e.g., "euclidean" , "minkowski" ,
"cityblock" ). Default is "euclidean" . See pdist2
for supported metrics. | |
"P" | Minkowski distance exponent, a positive
scalar. Valid only when "Distance" is "minkowski" .
Default is 2. | |
"Scale" | Nonnegative vector of scaling factors
matching the number of columns in X. Valid only when
"Distance" is "seuclidean" . Default is std (X) . | |
"Cov" | Positive definite covariance matrix
matching the number of columns in X. Valid only when
"Distance" is "mahalanobis" . Default is cov (X) . | |
"MaxNumLinksPerNode" | Maximum number of neighbors per node in the HNSW graph, a positive integer. Default is 16. | |
"TrainSetSize" | Size of the dynamic candidate list during graph construction, a positive integer. Default is 200. |
See also: hnswSearcher, knnsearch, createns, pdist2
[idx, D] = knnsearch (obj, Y)
returns the
indices idx and distances D of the nearest neighbor in
obj.X to each point in Y, using the distance metric specified
in obj.Distance.
hnswSearcher
object.
[idx, D] = knnsearch (obj, Y, name, value)
allows additional options via name-value pairs:
Name | Value | |
---|---|---|
"K" | A positive integer specifying the number of nearest neighbors to find. Default is 1. | |
"SearchSetSize" | A positive integer specifying the
size of the candidate list of nearest neighbors for a single query point
during the search process. Default is max (10, C) , where
C is the number of columns in obj.X. "SearchSetSize"
must be at least C and no more than the number of rows in training
data obj.X. |
See also: hnswSearcher, pdist2
## Create an hnswSearcher with Euclidean distance X = [1, 2; 3, 4; 5, 6]; obj = hnswSearcher (X); ## Find the nearest neighbor to [2, 3] Y = [2, 3]; [idx, D] = knnsearch (obj, Y, "K", 1); disp ("Nearest neighbor index:"); disp (idx); disp ("Distance:"); disp (D); Nearest neighbor index: 2 Distance: 1.4142 |
## Create an hnswSearcher with Minkowski distance (P=3) X = [0, 0; 1, 0; 2, 0]; obj = hnswSearcher (X, "Distance", "minkowski", "P", 3); ## Find the nearest neighbor to [1, 0] Y = [1, 0]; [idx, D] = knnsearch (obj, Y, "K", 1); disp ("Nearest neighbor index:"); disp (idx); disp ("Distance:"); disp (D); Nearest neighbor index: 2 Distance: 0 |