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

    Constructors
    Constructor
    Description
    NearestNeighborGraph(int k, int[][] neighbors, double[][] distances)
    Constructor.
    NearestNeighborGraph(int k, int[][] neighbors, double[][] distances, int[] index)
    Creates an instance of a NearestNeighborGraph record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    descent(double[][] data, int k)
    Creates an approximate nearest neighbor graph with random projection forest and Euclidean distance.
    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.
    descent(T[] data, Metric<T> distance, int k)
    Creates an approximate nearest neighbor graph with the NN-Descent algorithm.
    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.
    double[][]
    Returns the value of the distances record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    graph(boolean digraph)
    Returns the nearest neighbor graph.
    final int
    Returns a hash code value for this object.
    int[]
    Returns the value of the index record component.
    int
    k()
    Returns the value of the k record component.
    largest(boolean digraph)
    Returns the largest connected component of a nearest neighbor graph.
    int[][]
    Returns the value of the neighbors record component.
    of(double[][] data, int k)
    Creates a nearest neighbor graph with Euclidean distance.
    of(T[] data, Distance<T> distance, int k)
    Creates a nearest neighbor graph.
    random(T[] data, Distance<T> distance, int k)
    Creates a random neighbor graph.
    int
    Returns the number of vertices.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 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 a NearestNeighborGraph record class.
      Parameters:
      k - the value for the k record component
      neighbors - the value for the neighbors record component
      distances - the value for the distances record component
      index - the value for the index record component
  • Method Details

    • size

      public int size()
      Returns the number of vertices.
      Returns:
      the number of vertices.
    • graph

      public AdjacencyList graph(boolean digraph)
      Returns the nearest neighbor graph.
      Parameters:
      digraph - create a directed graph if true.
      Returns:
      the nearest neighbor graph.
    • of

      public static NearestNeighborGraph of(double[][] data, int k)
      Creates a nearest neighbor graph with Euclidean distance.
      Parameters:
      data - the dataset.
      k - k-nearest neighbor.
      Returns:
      k-nearest neighbor graph.
    • largest

      public NearestNeighborGraph largest(boolean digraph)
      Returns the largest connected component of a nearest neighbor graph.
      Parameters:
      digraph - create a directed graph if true.
      Returns:
      the largest connected component.
    • of

      public static <T> NearestNeighborGraph of(T[] data, Distance<T> distance, int k)
      Creates a nearest neighbor graph.
      Parameters:
      data - the dataset.
      distance - the distance function.
      k - k-nearest neighbor.
      Returns:
      k-nearest neighbor graph.
    • random

      public static <T> NearestNeighborGraph random(T[] data, Distance<T> distance, int k)
      Creates a random neighbor graph.
      Parameters:
      data - the dataset.
      distance - the distance function.
      k - k-random neighbor.
      Returns:
      k-random neighbor graph.
    • descent

      public static NearestNeighborGraph descent(double[][] data, int k)
      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

      public static <T> NearestNeighborGraph descent(T[] data, Metric<T> distance, int k)
      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

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • k

      public int k()
      Returns the value of the k record component.
      Returns:
      the value of the k record component
    • neighbors

      public int[][] neighbors()
      Returns the value of the neighbors record component.
      Returns:
      the value of the neighbors record component
    • distances

      public double[][] distances()
      Returns the value of the distances record component.
      Returns:
      the value of the distances record component
    • index

      public int[] index()
      Returns the value of the index record component.
      Returns:
      the value of the index record component