Class NewNNSearch

  • All Implemented Interfaces:
    Serializable, weka.core.AdditionalMeasureProducer, weka.core.OptionHandler, weka.core.RevisionHandler
    Direct Known Subclasses:
    PCANNSearch, TransformNNSearch

    public class NewNNSearch
    extends weka.core.neighboursearch.NearestNeighbourSearch
    Class implementing the brute force search algorithm for nearest neighbour search.

    Valid options are:

     -S
      Skip identical instances (distances equal to zero).
     
    Version:
    $Revision$
    Author:
    Dale
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  NewNNSearch.InstanceNode  
      • Nested classes/interfaces inherited from class weka.core.neighboursearch.NearestNeighbourSearch

        weka.core.neighboursearch.NearestNeighbourSearch.MyHeap, weka.core.neighboursearch.NearestNeighbourSearch.MyHeapElement, weka.core.neighboursearch.NearestNeighbourSearch.NeighborList, weka.core.neighboursearch.NearestNeighbourSearch.NeighborNode
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected double[] m_Distances
      Array holding the distances of the nearest neighbours.
      protected adams.data.SortedList<NewNNSearch.InstanceNode> m_neighbours  
      protected boolean m_SkipIdentical
      Whether to skip instances from the neighbours that are identical to the query instance.
      • Fields inherited from class weka.core.neighboursearch.NearestNeighbourSearch

        m_DistanceFunction, m_Instances, m_kNN, m_MeasurePerformance, m_Stats
    • Constructor Summary

      Constructors 
      Constructor Description
      NewNNSearch()
      Constructor.
      NewNNSearch​(weka.core.Instances insts)
      Constructor that uses the supplied set of instances.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addInstanceInfo​(weka.core.Instance ins)
      Adds the given instance info.
      double[] getDistances()
      Returns the distances of the k nearest neighbours.
      String[] getOptions()
      Gets the current settings.
      String getRevision()
      Returns the revision string.
      boolean getSkipIdentical()
      Gets whether if identical instances are skipped from the neighbourhood.
      String globalInfo()
      Returns a string describing this nearest neighbour search algorithm.
      weka.core.Instances kNearestNeighbours​(weka.core.Instance target, int kNN)
      Returns k nearest instances in the current neighbourhood to the supplied instance.
      Enumeration listOptions()
      Returns an enumeration describing the available options.
      weka.core.Instance nearestNeighbour​(weka.core.Instance target)
      Returns the nearest instance in the current neighbourhood to the supplied instance.
      void setInstances​(weka.core.Instances insts)
      Sets the instances comprising the current neighbourhood.
      void setOptions​(String[] options)
      Parses a given list of options.
      void setSkipIdentical​(boolean skip)
      Sets the property to skip identical instances (with distance zero from the target) from the set of neighbours returned.
      String skipIdenticalTipText()
      Returns the tip text for this property.
      void update​(weka.core.Instance ins)
      Updates the LinearNNSearch to cater for the new added instance.
      • Methods inherited from class weka.core.neighboursearch.NearestNeighbourSearch

        combSort11, distanceFunctionTipText, enumerateMeasures, getDistanceFunction, getInstances, getMeasure, getMeasurePerformance, getPerformanceStats, measurePerformanceTipText, partition, quickSort, setDistanceFunction, setMeasurePerformance
    • Field Detail

      • m_Distances

        protected double[] m_Distances
        Array holding the distances of the nearest neighbours. It is filled up both by nearestNeighbour() and kNearestNeighbours().
      • m_SkipIdentical

        protected boolean m_SkipIdentical
        Whether to skip instances from the neighbours that are identical to the query instance.
    • Constructor Detail

      • NewNNSearch

        public NewNNSearch()
        Constructor. Needs setInstances(Instances) to be called before the class is usable.
      • NewNNSearch

        public NewNNSearch​(weka.core.Instances insts)
        Constructor that uses the supplied set of instances.
        Parameters:
        insts - the instances to use
    • Method Detail

      • globalInfo

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

        public Enumeration listOptions()
        Returns an enumeration describing the available options.
        Specified by:
        listOptions in interface weka.core.OptionHandler
        Overrides:
        listOptions in class weka.core.neighboursearch.NearestNeighbourSearch
        Returns:
        an enumeration of all the available options.
      • setOptions

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

        Valid options are:

         -S
          Skip identical instances (distances equal to zero).
         
        Specified by:
        setOptions in interface weka.core.OptionHandler
        Overrides:
        setOptions in class weka.core.neighboursearch.NearestNeighbourSearch
        Parameters:
        options - the list of options as an array of strings
        Throws:
        Exception - if an option is not supported
      • getOptions

        public String[] getOptions()
        Gets the current settings.
        Specified by:
        getOptions in interface weka.core.OptionHandler
        Overrides:
        getOptions in class weka.core.neighboursearch.NearestNeighbourSearch
        Returns:
        an array of strings suitable for passing to setOptions()
      • skipIdenticalTipText

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

        public void setSkipIdentical​(boolean skip)
        Sets the property to skip identical instances (with distance zero from the target) from the set of neighbours returned.
        Parameters:
        skip - if true, identical intances are skipped
      • getSkipIdentical

        public boolean getSkipIdentical()
        Gets whether if identical instances are skipped from the neighbourhood.
        Returns:
        true if identical instances are skipped
      • nearestNeighbour

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

        public weka.core.Instances kNearestNeighbours​(weka.core.Instance target,
                                                      int kNN)
                                               throws Exception
        Returns k nearest instances in the current neighbourhood to the supplied instance.
        Specified by:
        kNearestNeighbours in class weka.core.neighboursearch.NearestNeighbourSearch
        Parameters:
        target - The instance to find the k nearest neighbours for.
        kNN - The number of nearest neighbours to find.
        Returns:
        the k nearest neighbors
        Throws:
        Exception - if the neighbours 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, if either of the nearestNeighbour functions are called at any point in the past then no exception is thrown and the distances of the training set from the last supplied target instance (to either one of the nearestNeighbour functions) is/are returned.
        Specified by:
        getDistances in class weka.core.neighboursearch.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:
        Exception - if called before calling kNearestNeighbours or nearestNeighbours.
      • setInstances

        public void setInstances​(weka.core.Instances insts)
                          throws Exception
        Sets the instances comprising the current neighbourhood.
        Overrides:
        setInstances in class weka.core.neighboursearch.NearestNeighbourSearch
        Parameters:
        insts - The set of instances on which the nearest neighbour search is carried out. Usually this set is the training set.
        Throws:
        Exception - if setting of instances fails
      • update

        public void update​(weka.core.Instance ins)
                    throws Exception
        Updates the LinearNNSearch to cater for the new added instance. This implementation only updates the ranges of the DistanceFunction class, since our set of instances is passed by reference and should already have the newly added instance.
        Specified by:
        update in class weka.core.neighboursearch.NearestNeighbourSearch
        Parameters:
        ins - The instance to add. Usually this is the instance that is added to our neighbourhood i.e. the training instances.
        Throws:
        Exception - if the given instances are null
      • addInstanceInfo

        public void addInstanceInfo​(weka.core.Instance ins)
        Adds the given instance info. This implementation updates the range datastructures of the DistanceFunction class.
        Overrides:
        addInstanceInfo in class weka.core.neighboursearch.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.
      • getRevision

        public String getRevision()
        Returns the revision string.
        Returns:
        the revision