jsat.linear.vectorcollection
Class BallTree<V extends Vec>
- java.lang.Object
-
- jsat.linear.vectorcollection.BallTree<V>
-
- Type Parameters:
V-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, DualTree<V>, IncrementalCollection<V>, VectorCollection<V>
public class BallTree<V extends Vec> extends java.lang.Object implements IncrementalCollection<V>, DualTree<V>
This class implements the Ball Tree algorithm for accelerating nearest neighbor queries. Contained within this class are multiple methods of building Ball Trees. Options for changing construction can alter the method ofconstructionof the hierarchy is made, or howpivotis selected.
The default method of construction and pivot selection for ball trees will work for most cases, but is not appicable for all distance metrics. If you are using an exotic distance metric theBallTree.ConstructionMethod.TOP_DOWN_FARTHESTandBallTree.PivotSelection.MEDOIDwill work for any dataset, but may be slower.
See:- Omohundro, S. M. (1989). Five Balltree Construction Algorithms (No. TR-89-063).
- Moore, A. W. (2000). The Anchors Hierarchy: Using the Triangle Inequality to Survive High Dimensional Data. In Proceedings of the Sixteenth Conference on Uncertainty in Artificial Intelligence (pp. 397–405). San Francisco, CA, USA: Morgan Kaufmann Publishers Inc. Retrieved from here
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classBallTree.ConstructionMethodstatic classBallTree.PivotSelection-
Nested classes/interfaces inherited from interface jsat.linear.vectorcollection.DualTree
DualTree.DualTreeTraversalAction, DualTree.SelfAsChildNode<N extends IndexNode<N>>
-
-
Field Summary
Fields Modifier and Type Field and Description static intDEFAULT_LEAF_SIZE-
Fields inherited from interface jsat.linear.vectorcollection.DualTree
COMP_SCORE
-
-
Constructor Summary
Constructors Constructor and Description BallTree()BallTree(BallTree toCopy)Copy constructorBallTree(DistanceMetric dm, BallTree.ConstructionMethod method, BallTree.PivotSelection pivot_method)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidbuild(boolean parallel, java.util.List<V> collection, DistanceMetric dm)Builds this metric index from the given collection of points using the given distance metric.BallTree<V>clone()Vget(int indx)Accesses a vector from this collection via index.java.util.List<java.lang.Double>getAccelerationCache()BallTree.ConstructionMethodgetConstruction_method()DistanceMetricgetDistanceMetric()intgetLeafSize()intgetMaxDepth()Computes the maximum depth of the current tree.BallTree.PivotSelectiongetPivot_method()IndexNodegetRoot()voidinsert(V x)Incrementally adds the given datapoint into the collectionvoidsearch(Vec query, double range, java.util.List<java.lang.Integer> neighbors, java.util.List<java.lang.Double> distances)Performs a range search of the current collection.voidsearch(Vec query, int numNeighbors, java.util.List<java.lang.Integer> neighbors, java.util.List<java.lang.Double> distances)Performs k-Nearest Neighbor search of the current collection.voidsetConstruction_method(BallTree.ConstructionMethod construction_method)voidsetDistanceMetric(DistanceMetric dm)Sets the distance metric used for this collection.voidsetLeafSize(int leaf_size)Sets the number of points stored within a leaf node of the index.voidsetPivot_method(BallTree.PivotSelection pivot_method)intsize()Returns the number of vectors stored in the collection-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jsat.linear.vectorcollection.DualTree
computeKnnBound, dist, dual_depth_first, search, search, traverse
-
-
-
-
Field Detail
-
DEFAULT_LEAF_SIZE
public static final int DEFAULT_LEAF_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BallTree
public BallTree()
-
BallTree
public BallTree(DistanceMetric dm, BallTree.ConstructionMethod method, BallTree.PivotSelection pivot_method)
-
BallTree
public BallTree(BallTree toCopy)
Copy constructor- Parameters:
toCopy- the object to copy
-
-
Method Detail
-
getAccelerationCache
public java.util.List<java.lang.Double> getAccelerationCache()
- Specified by:
getAccelerationCachein interfaceVectorCollection<V extends Vec>
-
setDistanceMetric
public void setDistanceMetric(DistanceMetric dm)
Description copied from interface:VectorCollectionSets the distance metric used for this collection.- Specified by:
setDistanceMetricin interfaceVectorCollection<V extends Vec>- Parameters:
dm- the distance metric to use
-
getDistanceMetric
public DistanceMetric getDistanceMetric()
- Specified by:
getDistanceMetricin interfaceVectorCollection<V extends Vec>- Returns:
- the distance metric to use
-
setLeafSize
public void setLeafSize(int leaf_size)
Sets the number of points stored within a leaf node of the index. Larger values avoid search overhead, but reduce opportunities for pruning.- Parameters:
leaf_size- the size of a leaf node. Must be at least 2
-
getLeafSize
public int getLeafSize()
- Returns:
- the number of points to store within a leaf node
-
getMaxDepth
public int getMaxDepth()
Computes the maximum depth of the current tree. A value of zero indicates that only a root node exists or the tree is empty. Any other value is the maximum number of children a node contains.- Returns:
- the maximum current depth of this Ball Tree
-
setPivot_method
public void setPivot_method(BallTree.PivotSelection pivot_method)
-
getPivot_method
public BallTree.PivotSelection getPivot_method()
-
setConstruction_method
public void setConstruction_method(BallTree.ConstructionMethod construction_method)
-
getConstruction_method
public BallTree.ConstructionMethod getConstruction_method()
-
build
public void build(boolean parallel, java.util.List<V> collection, DistanceMetric dm)Description copied from interface:VectorCollectionBuilds this metric index from the given collection of points using the given distance metric.- Specified by:
buildin interfaceVectorCollection<V extends Vec>- Parameters:
parallel-trueif the index should be built in parallel, orfalseif it should be done in a single thread.collection- the list of vectors to put into the indexdm- the distance metric to build the index using.
-
insert
public void insert(V x)
Description copied from interface:IncrementalCollectionIncrementally adds the given datapoint into the collection- Specified by:
insertin interfaceIncrementalCollection<V extends Vec>- Parameters:
x- the vector to add to the collection
-
search
public void search(Vec query, double range, java.util.List<java.lang.Integer> neighbors, java.util.List<java.lang.Double> distances)
Description copied from interface:VectorCollectionPerforms a range search of the current collection. The index and distance of each found neighbor will be placed into the given Lists.- Specified by:
searchin interfaceVectorCollection<V extends Vec>- Parameters:
query- the point to search for the neighbors within a given radius.range- the radius to search for all the neighbors with a distance ≤ range.neighbors- the list to store the index of the neighbors in. Will be sorted by distance to the query, and paired with the values in distances.distances- the list to store the distance of the neighbors to the query in. Will be sorted, and paired with the values in neighbors.
-
search
public void search(Vec query, int numNeighbors, java.util.List<java.lang.Integer> neighbors, java.util.List<java.lang.Double> distances)
Description copied from interface:VectorCollectionPerforms k-Nearest Neighbor search of the current collection. The index and distance of each found neighbor will be placed into the given Lists.- Specified by:
searchin interfaceDualTree<V extends Vec>- Specified by:
searchin interfaceVectorCollection<V extends Vec>- Parameters:
query- the point to search for the k-nearest neighbors ofnumNeighbors- the number of neighbors k to search for.neighbors- the list to store the index of the neighbors in. Will be sorted by distance to the query, and paired with the values in distances.distances- the list to store the distance of the neighbors to the query in. Will be sorted, and paired with the values in neighbors.
-
get
public V get(int indx)
Description copied from interface:VectorCollectionAccesses a vector from this collection via index.- Specified by:
getin interfaceVectorCollection<V extends Vec>- Parameters:
indx- the index in [0,VectorCollection.size()) of the vector to access- Returns:
- the vector from the collection
-
size
public int size()
Description copied from interface:VectorCollectionReturns the number of vectors stored in the collection- Specified by:
sizein interfaceVectorCollection<V extends Vec>- Returns:
- the size of the collection
-
-
DataMelt 3.0 © DataMelt by jWork.ORG