Class BallTree

  • All Implemented Interfaces:
    java.io.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.

    BibTeX:

     @techreport{Omohundro1989,
        author = {Stephen M. Omohundro},
        institution = {International Computer Science Institute},
        month = {December},
        number = {TR-89-063},
        title = {Five Balltree Construction Algorithms},
        year = {1989}
     }
     
     @article{Uhlmann1991,
        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)
    Version:
    $Revision: 1.2 $
    Author:
    Ashraf M. Kibriya (amk14[at-the-rate]cs[dot]waikato[dot]ac[dot]nz)
    See Also:
    Serialized Form
    • Constructor Detail

      • 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.
        Parameters:
        insts - The instances/points on which the BallTree should be built on.
    • Method Detail

      • globalInfo

        public java.lang.String globalInfo()
        Returns a string describing this nearest neighbour search algorithm.
        Overrides:
        globalInfo in class NearestNeighbourSearch
        Returns:
        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
        Returns:
        the technical information about this class
      • kNearestNeighbours

        public Instances kNearestNeighbours​(Instance target,
                                            int k)
                                     throws java.lang.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
        Parameters:
        target - The instance to find the k nearest neighbours for.
        k - The number of nearest neighbours to find.
        Returns:
        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).
        Throws:
        java.lang.Exception - If the neighbours could not be found.
      • nearestNeighbour

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

        public double[] getDistances()
                              throws java.lang.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
        Returns:
        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.
        Throws:
        java.lang.Exception - if called before calling kNearestNeighbours or nearestNeighbours.
      • update

        public void update​(Instance ins)
                    throws java.lang.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
        Parameters:
        ins - The instance to be added. Usually the newly added instance in the training set.
        Throws:
        java.lang.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.
        Overrides:
        addInstanceInfo in class NearestNeighbourSearch
        Parameters:
        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 java.lang.Exception
        Builds the BallTree based on the given set of instances.
        Overrides:
        setInstances in class NearestNeighbourSearch
        Parameters:
        insts - The insts for which the BallTree is to be built.
        Throws:
        java.lang.Exception - If some error occurs while building the BallTree
      • ballTreeConstructorTipText

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

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

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

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

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

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

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

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

        public void setOptions​(java.lang.String[] options)
                        throws java.lang.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
        Overrides:
        setOptions in class NearestNeighbourSearch
        Parameters:
        options - the list of options as an array of strings
        Throws:
        java.lang.Exception - if an option is not supported
      • getRevision

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