Package smile.graph
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
ConstructorDescriptionNearestNeighborGraph
(int k, int[][] neighbors, double[][] distances) Constructor.NearestNeighborGraph
(int k, int[][] neighbors, double[][] distances, int[] index) Creates an instance of aNearestNeighborGraph
record class. -
Method Summary
Modifier and TypeMethodDescriptionstatic NearestNeighborGraph
descent
(double[][] data, int k) Creates an approximate nearest neighbor graph with random projection forest and Euclidean distance.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.static <T> NearestNeighborGraph
Creates an approximate nearest neighbor graph with the NN-Descent algorithm.static <T> NearestNeighborGraph
Creates an approximate nearest neighbor graph with the NN-Descent algorithm.double[][]
Returns the value of thedistances
record component.final boolean
Indicates whether some other object is "equal to" this one.graph
(boolean digraph) Returns the nearest neighbor graph.final int
hashCode()
Returns a hash code value for this object.int[]
index()
Returns the value of theindex
record component.int
k()
Returns the value of thek
record component.largest
(boolean digraph) Returns the largest connected component of a nearest neighbor graph.int[][]
Returns the value of theneighbors
record component.static NearestNeighborGraph
of
(double[][] data, int k) Creates a nearest neighbor graph with Euclidean distance.static <T> NearestNeighborGraph
Creates a nearest neighbor graph.static <T> NearestNeighborGraph
Creates a random neighbor graph.int
size()
Returns the number of vertices.final String
toString()
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 aNearestNeighborGraph
record 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.- Parameters:
data
- the dataset.distance
- the distance function.k
- k-nearest neighbor.- Returns:
- k-nearest neighbor graph.
-
random
Creates a random neighbor graph.- 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.- Returns:
- approximate k-nearest neighbor graph.
-
descent
Creates an approximate nearest neighbor graph with the NN-Descent algorithm.- 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.- 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
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
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 '=='. -
k
public int k()Returns the value of thek
record component.- Returns:
- the value of the
k
record component
-
neighbors
public int[][] neighbors()Returns the value of theneighbors
record component.- Returns:
- the value of the
neighbors
record component
-
distances
public double[][] distances()Returns the value of thedistances
record component.- Returns:
- the value of the
distances
record component
-
index
public int[] index()Returns the value of theindex
record component.- Returns:
- the value of the
index
record component
-