Class BallTree

All Implemented Interfaces:
Serializable, AdditionalMeasureProducer, OptionHandler, RevisionHandler, TechnicalInformationHandler

public class BallTree extends NearestNeighbourSearch implements TechnicalInformationHandler
Class implementing the BallTree/Metric Tree algorithm for nearest neighbour search.
The connection to dataset is only a reference. For the tree structure the indexes are stored in an array.
See the implementing classes of different construction methods of the trees for details on its construction.

For more information see also:

Stephen M. Omohundro (1989). Five Balltree Construction Algorithms.

Jeffrey K. Uhlmann (1991). Satisfying general proximity/similarity queries with metric trees. Information Processing Letters. 40(4):175-179.


    author = {Stephen M. Omohundro},
    institution = {International Computer Science Institute},
    month = {December},
    number = {TR-89-063},
    title = {Five Balltree Construction Algorithms},
    year = {1989}
    author = {Jeffrey K. Uhlmann},
    journal = {Information Processing Letters},
    month = {November},
    number = {4},
    pages = {175-179},
    title = {Satisfying general proximity/similarity queries with metric trees},
    volume = {40},
    year = {1991}

Valid options are:

 -C <classname and options>
  The construction method to employ. Either TopDown or BottomUp
  (default: weka.core.TopDownConstructor)
$Revision: 10141 $
Ashraf M. Kibriya (amk14[at-the-rate]cs[dot]waikato[dot]ac[dot]nz)
See Also:
  • Constructor Details

    • BallTree

      public BallTree()
      Creates a new instance of BallTree.
    • BallTree

      public BallTree(Instances insts)
      Creates a new instance of BallTree. It also builds the tree on supplied set of Instances.
      insts - The instances/points on which the BallTree should be built on.
  • Method Details

    • globalInfo

      public String globalInfo()
      Returns a string describing this nearest neighbour search algorithm.
      globalInfo in class NearestNeighbourSearch
      a description of the algorithm for displaying in the explorer/experimenter gui
    • getTechnicalInformation

      public TechnicalInformation getTechnicalInformation()
      Returns an instance of a TechnicalInformation object, containing detailed information about the technical background of this class, e.g., paper reference or book this class is based on.
      Specified by:
      getTechnicalInformation in interface TechnicalInformationHandler
      the technical information about this class
    • kNearestNeighbours

      public Instances kNearestNeighbours(Instance target, int k) throws Exception
      Returns k nearest instances in the current neighbourhood to the supplied instance. >k instances can be returned if there is more than one instance at the kth boundary (i.e. if there are more than 1 instance with the same distance as the kth nearest neighbour).
      Specified by:
      kNearestNeighbours in class NearestNeighbourSearch
      target - The instance to find the k nearest neighbours for.
      k - The number of nearest neighbours to find.
      The k nearest neighbours of the given target instance (>k nearest neighbours, if there are more instances that have same distance as the kth nearest neighbour).
      Exception - If the neighbours could not be found.
    • nearestNeighbour

      public Instance nearestNeighbour(Instance target) throws Exception
      Returns the nearest instance in the current neighbourhood to the supplied instance.
      Specified by:
      nearestNeighbour in class NearestNeighbourSearch
      target - The instance to find the nearest neighbour for.
      The nearest neighbour of the given target instance.
      Exception - if the nearest neighbour could not be found.
    • getDistances

      public double[] getDistances() throws Exception
      Returns the distances of the k nearest neighbours. The kNearestNeighbours or nearestNeighbour must always be called before calling this function. If this function is called before calling either the kNearestNeighbours or the nearestNeighbour, then it throws an exception. If, however, any one of the two functions is called at any point in the past, then no exception is thrown and the distances of NN(s) from the training set for the last supplied target instance (to either one of the nearestNeighbour functions) is/are returned.
      Specified by:
      getDistances in class NearestNeighbourSearch
      array containing the distances of the nearestNeighbours. The length and ordering of the array is the same as that of the instances returned by nearestNeighbour functions.
      Exception - if called before calling kNearestNeighbours or nearestNeighbours.
    • update

      public void update(Instance ins) throws Exception
      Adds one instance to the BallTree. This involves creating/updating the structure to reflect the newly added training instance
      Specified by:
      update in class NearestNeighbourSearch
      ins - The instance to be added. Usually the newly added instance in the training set.
      Exception - If the instance cannot be added to the tree.
    • addInstanceInfo

      public void addInstanceInfo(Instance ins)
      Adds the given instance's info. This implementation updates the attributes' range datastructures of EuclideanDistance class.
      addInstanceInfo in class NearestNeighbourSearch
      ins - The instance to add the information of. Usually this is the test instance supplied to update the range of attributes in the distance function.
    • setInstances

      public void setInstances(Instances insts) throws Exception
      Builds the BallTree based on the given set of instances.
      setInstances in class NearestNeighbourSearch
      insts - The insts for which the BallTree is to be built.
      Exception - If some error occurs while building the BallTree
    • ballTreeConstructorTipText

      public String ballTreeConstructorTipText()
      Returns the tip text for this property.
      tip text for this property suitable for displaying in the explorer/experimenter gui
    • getBallTreeConstructor

      public BallTreeConstructor getBallTreeConstructor()
      Returns the BallTreeConstructor currently in use.
      The BallTreeConstructor currently in use.
    • setBallTreeConstructor

      public void setBallTreeConstructor(BallTreeConstructor constructor)
      Sets the BallTreeConstructor for building the BallTree (default TopDownConstructor).
      constructor - The new BallTreeConstructor.
    • measureTreeSize

      public double measureTreeSize()
      Returns the size of the tree.
      the size of the tree
    • measureNumLeaves

      public double measureNumLeaves()
      Returns the number of leaves.
      the number of leaves
    • measureMaxDepth

      public double measureMaxDepth()
      Returns the depth of the tree.
      the number of rules
    • enumerateMeasures

      public Enumeration<String> enumerateMeasures()
      Returns an enumeration of the additional measure names.
      Specified by:
      enumerateMeasures in interface AdditionalMeasureProducer
      enumerateMeasures in class NearestNeighbourSearch
      an enumeration of the measure names
    • getMeasure

      public double getMeasure(String additionalMeasureName)
      Returns the value of the named measure.
      Specified by:
      getMeasure in interface AdditionalMeasureProducer
      getMeasure in class NearestNeighbourSearch
      additionalMeasureName - the name of the measure to query for its value.
      the value of the named measure.
      IllegalArgumentException - if the named measure is not supported.
    • setMeasurePerformance

      public void setMeasurePerformance(boolean measurePerformance)
      Sets whether to calculate the performance statistics or not.
      setMeasurePerformance in class NearestNeighbourSearch
      measurePerformance - This should be true if performance statistics are to be calculated.
    • listOptions

      public Enumeration<Option> listOptions()
      Returns an enumeration describing the available options.
      Specified by:
      listOptions in interface OptionHandler
      listOptions in class NearestNeighbourSearch
      an enumeration of all the available options.
    • setOptions

      public void setOptions(String[] options) throws Exception
      Parses a given list of options. Valid options are:

       -C <classname and options>
        The construction method to employ. Either TopDown or BottomUp
        (default: weka.core.TopDownConstructor)
      Specified by:
      setOptions in interface OptionHandler
      setOptions in class NearestNeighbourSearch
      options - the list of options as an array of strings
      Exception - if an option is not supported
    • getOptions

      public String[] getOptions()
      Gets the current settings of KDtree.
      Specified by:
      getOptions in interface OptionHandler
      getOptions in class NearestNeighbourSearch
      an array of strings suitable for passing to setOptions
    • getRevision

      public String getRevision()
      Returns the revision string.
      Specified by:
      getRevision in interface RevisionHandler
      the revision