Record Class NearestNeighborGraph
java.lang.Object
java.lang.Record
smile.graph.NearestNeighborGraph
- Record Components:
k- k-nearest neighbor.neighbors- The indices of k-nearest neighbors.distances- The distances to k-nearest neighbors.index- The sample index of each vertex in original dataset.
public record NearestNeighborGraph(int k, int[][] neighbors, double[][] distances, int[] index)
extends Record
The k-nearest neighbor graph builder.
-
Constructor Summary
ConstructorsConstructorDescriptionNearestNeighborGraph(int k, int[][] neighbors, double[][] distances) Constructor.NearestNeighborGraph(int k, int[][] neighbors, double[][] distances, int[] index) Creates an instance of aNearestNeighborGraphrecord class. -
Method Summary
Modifier and TypeMethodDescriptionstatic NearestNeighborGraphdescent(double[][] data, int k) Creates an approximate nearest neighbor graph with random projection forest and Euclidean distance.static NearestNeighborGraphdescent(double[][] data, int k, int numTrees, int leafSize, int maxCandidates, int maxIter, double delta) Creates an approximate nearest neighbor graph with random projection forest and Euclidean distance.static <T> NearestNeighborGraphCreates an approximate nearest neighbor graph with the NN-Descent algorithm.static <T> NearestNeighborGraphCreates an approximate nearest neighbor graph with the NN-Descent algorithm.double[][]Returns the value of thedistancesrecord component.final booleanIndicates whether some other object is "equal to" this one.graph(boolean digraph) Returns the nearest neighbor graph.final inthashCode()Returns a hash code value for this object.int[]index()Returns the value of theindexrecord component.intk()Returns the value of thekrecord component.largest(boolean digraph) Returns the largest connected component of a nearest neighbor graph.int[][]Returns the value of theneighborsrecord component.static NearestNeighborGraphof(double[][] data, int k) Creates a nearest neighbor graph with Euclidean distance.static <T> NearestNeighborGraphCreates a nearest neighbor graph.static <T> NearestNeighborGraphCreates a random neighbor graph.intsize()Returns the number of vertices.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
NearestNeighborGraph
public NearestNeighborGraph(int k, int[][] neighbors, double[][] distances) Constructor.- Parameters:
k- k-nearest neighbor.neighbors- The indices of k-nearest neighbors.distances- The distances to k-nearest neighbors.
-
NearestNeighborGraph
public NearestNeighborGraph(int k, int[][] neighbors, double[][] distances, int[] index) Creates an instance of aNearestNeighborGraphrecord class.
-
-
Method Details
-
size
public int size()Returns the number of vertices.- Returns:
- the number of vertices.
-
graph
Returns the nearest neighbor graph.- Parameters:
digraph- create a directed graph if true.- Returns:
- the nearest neighbor graph.
-
of
Creates a nearest neighbor graph with Euclidean distance.- Parameters:
data- the dataset.k- k-nearest neighbor.- Returns:
- k-nearest neighbor graph.
-
largest
Returns the largest connected component of a nearest neighbor graph.- Parameters:
digraph- create a directed graph if true.- Returns:
- the largest connected component.
-
of
Creates a nearest neighbor graph.- Type Parameters:
T- the type of data objects.- Parameters:
data- the dataset.distance- the distance function.k- k-nearest neighbor.- Returns:
- k-nearest neighbor graph.
-
random
Creates a random neighbor graph.- Type Parameters:
T- the type of data objects.- Parameters:
data- the dataset.distance- the distance function.k- k-random neighbor.- Returns:
- k-random neighbor graph.
-
descent
Creates an approximate nearest neighbor graph with random projection forest and Euclidean distance.- Parameters:
data- the dataset.k- k-nearest neighbor.- Returns:
- approximate k-nearest neighbor graph.
-
descent
public static NearestNeighborGraph descent(double[][] data, int k, int numTrees, int leafSize, int maxCandidates, int maxIter, double delta) Creates an approximate nearest neighbor graph with random projection forest and Euclidean distance.- Parameters:
data- the dataset.k- k-nearest neighbor.numTrees- the number of trees.leafSize- the maximum size of leaf node.maxCandidates- the maximum number of candidates in nearest neighbor search.maxIter- the maximum number of iterations.delta- Controls the early stop due to limited progress. Larger values will result in earlier aborts, providing less accurate indexes, and less accurate searching.- Returns:
- approximate k-nearest neighbor graph.
-
descent
Creates an approximate nearest neighbor graph with the NN-Descent algorithm.- Type Parameters:
T- the type of data objects.- Parameters:
data- the dataset.distance- the distance function.k- k-nearest neighbor.- Returns:
- approximate k-nearest neighbor graph.
-
descent
public static <T> NearestNeighborGraph descent(T[] data, Metric<T> distance, int k, int maxCandidates, int maxIter, double delta) Creates an approximate nearest neighbor graph with the NN-Descent algorithm.- Type Parameters:
T- the type of data objects.- Parameters:
data- the dataset.distance- the distance function.k- k-nearest neighbor.maxCandidates- the maximum number of candidates in nearest neighbor search.maxIter- the maximum number of iterations.delta- Controls the early stop due to limited progress. Larger values will result in earlier aborts, providing less accurate indexes, and less accurate searching.- Returns:
- approximate k-nearest neighbor graph.
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
k
-
neighbors
-
distances
-
index
-