Package weka.core.neighboursearch
Class BallTree
- java.lang.Object
-
- weka.core.neighboursearch.NearestNeighbourSearch
-
- weka.core.neighboursearch.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
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addInstanceInfo(Instance ins)
Adds the given instance's info.java.lang.String
ballTreeConstructorTipText()
Returns the tip text for this property.java.util.Enumeration
enumerateMeasures()
Returns an enumeration of the additional measure names.BallTreeConstructor
getBallTreeConstructor()
Returns the BallTreeConstructor currently in use.double[]
getDistances()
Returns the distances of the k nearest neighbours.double
getMeasure(java.lang.String additionalMeasureName)
Returns the value of the named measure.java.lang.String[]
getOptions()
Gets the current settings of KDtree.java.lang.String
getRevision()
Returns the revision string.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.java.lang.String
globalInfo()
Returns a string describing this nearest neighbour search algorithm.Instances
kNearestNeighbours(Instance target, int k)
Returns k nearest instances in the current neighbourhood to the supplied instance.java.util.Enumeration
listOptions()
Returns an enumeration describing the available options.double
measureMaxDepth()
Returns the depth of the tree.double
measureNumLeaves()
Returns the number of leaves.double
measureTreeSize()
Returns the size of the tree.Instance
nearestNeighbour(Instance target)
Returns the nearest instance in the current neighbourhood to the supplied instance.void
setBallTreeConstructor(BallTreeConstructor constructor)
Sets the BallTreeConstructor for building the BallTree (default TopDownConstructor).void
setInstances(Instances insts)
Builds the BallTree based on the given set of instances.void
setMeasurePerformance(boolean measurePerformance)
Sets whether to calculate the performance statistics or not.void
setOptions(java.lang.String[] options)
Parses a given list of options.void
update(Instance ins)
Adds one instance to the BallTree.-
Methods inherited from class weka.core.neighboursearch.NearestNeighbourSearch
combSort11, distanceFunctionTipText, getDistanceFunction, getInstances, getMeasurePerformance, getPerformanceStats, measurePerformanceTipText, quickSort, setDistanceFunction
-
-
-
-
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 classNearestNeighbourSearch
- 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 interfaceTechnicalInformationHandler
- 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 classNearestNeighbourSearch
- 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 classNearestNeighbourSearch
- 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 classNearestNeighbourSearch
- 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 classNearestNeighbourSearch
- 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 classNearestNeighbourSearch
- 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 classNearestNeighbourSearch
- 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
-
enumerateMeasures
public java.util.Enumeration enumerateMeasures()
Returns an enumeration of the additional measure names.- Specified by:
enumerateMeasures
in interfaceAdditionalMeasureProducer
- Overrides:
enumerateMeasures
in classNearestNeighbourSearch
- Returns:
- an enumeration of the measure names
-
getMeasure
public double getMeasure(java.lang.String additionalMeasureName)
Returns the value of the named measure.- Specified by:
getMeasure
in interfaceAdditionalMeasureProducer
- Overrides:
getMeasure
in classNearestNeighbourSearch
- 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 classNearestNeighbourSearch
- Parameters:
measurePerformance
- This should be true if performance statistics are to be calculated.
-
listOptions
public java.util.Enumeration listOptions()
Returns an enumeration describing the available options.- Specified by:
listOptions
in interfaceOptionHandler
- Overrides:
listOptions
in classNearestNeighbourSearch
- Returns:
- an enumeration of all the available options.
-
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 interfaceOptionHandler
- Overrides:
setOptions
in classNearestNeighbourSearch
- Parameters:
options
- the list of options as an array of strings- Throws:
java.lang.Exception
- if an option is not supported
-
getOptions
public java.lang.String[] getOptions()
Gets the current settings of KDtree.- Specified by:
getOptions
in interfaceOptionHandler
- Overrides:
getOptions
in classNearestNeighbourSearch
- Returns:
- an array of strings suitable for passing to setOptions
-
getRevision
public java.lang.String getRevision()
Returns the revision string.- Specified by:
getRevision
in interfaceRevisionHandler
- Returns:
- the revision
-
-