weka.core.neighboursearch.NearestNeighbourSearch Java Examples

The following examples show how to use weka.core.neighboursearch.NearestNeighbourSearch. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KNNAugSpaceSampler.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * @param nearestNeighbour The nearest neighbour search algorithm to use.
 * @author Michael
 *
 */
public KNNAugSpaceSampler(final Instances preciseInsts, final Random rng, final int k, final NearestNeighbourSearch nearestNeighbour) {
	super(preciseInsts, rng);
	this.k = k;
	DistanceFunction dist = new EuclideanDistance(preciseInsts);
	String distOptionColumns = String.format("-R first-%d", preciseInsts.numAttributes() - 1);
	String[] distOptions = {distOptionColumns};

	try {
		dist.setOptions(distOptions);
		nearestNeighbour.setDistanceFunction(dist);
		nearestNeighbour.setInstances(preciseInsts);
	} catch (Exception e) {
		logger.error("Could not configure distance function or setup nearest neighbour: {}", e);
	}
	nearestNeighbour.setMeasurePerformance(false);
	this.nearestNeighbour = nearestNeighbour;
}
 
Example #2
Source File: kNN.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public final void setDistanceFunction(DistanceFunction df){
	dist=df;
	NearestNeighbourSearch s = super.getNearestNeighbourSearchAlgorithm();
	try{
		s.setDistanceFunction(df);
	}catch(Exception e){
		System.err.println(" Exception thrown setting distance function ="+e+" in "+this);
                       e.printStackTrace();
                       System.exit(0);
	}
}
 
Example #3
Source File: IBk.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Parses a given list of options. <p/>
 *
 <!-- options-start -->
 * Valid options are: <p/>
 * 
 * <pre> -I
 *  Weight neighbours by the inverse of their distance
 *  (use when k &gt; 1)</pre>
 * 
 * <pre> -F
 *  Weight neighbours by 1 - their distance
 *  (use when k &gt; 1)</pre>
 * 
 * <pre> -K &lt;number of neighbors&gt;
 *  Number of nearest neighbours (k) used in classification.
 *  (Default = 1)</pre>
 * 
 * <pre> -E
 *  Minimise mean squared error rather than mean absolute
 *  error when using -X option with numeric prediction.</pre>
 * 
 * <pre> -W &lt;window size&gt;
 *  Maximum number of training instances maintained.
 *  Training instances are dropped FIFO. (Default = no window)</pre>
 * 
 * <pre> -X
 *  Select the number of nearest neighbours between 1
 *  and the k value specified using hold-one-out evaluation
 *  on the training data (use when k &gt; 1)</pre>
 * 
 * <pre> -A
 *  The nearest neighbour search algorithm to use (default: weka.core.neighboursearch.LinearNNSearch).
 * </pre>
 * 
 <!-- options-end -->
 *
 * @param options the list of options as an array of strings
 * @throws Exception if an option is not supported
 */
public void setOptions(String[] options) throws Exception {
  
  String knnString = Utils.getOption('K', options);
  if (knnString.length() != 0) {
    setKNN(Integer.parseInt(knnString));
  } else {
    setKNN(1);
  }
  String windowString = Utils.getOption('W', options);
  if (windowString.length() != 0) {
    setWindowSize(Integer.parseInt(windowString));
  } else {
    setWindowSize(0);
  }
  if (Utils.getFlag('I', options)) {
    setDistanceWeighting(new SelectedTag(WEIGHT_INVERSE, TAGS_WEIGHTING));
  } else if (Utils.getFlag('F', options)) {
    setDistanceWeighting(new SelectedTag(WEIGHT_SIMILARITY, TAGS_WEIGHTING));
  } else {
    setDistanceWeighting(new SelectedTag(WEIGHT_NONE, TAGS_WEIGHTING));
  }
  setCrossValidate(Utils.getFlag('X', options));
  setMeanSquared(Utils.getFlag('E', options));

  String nnSearchClass = Utils.getOption('A', options);
  if(nnSearchClass.length() != 0) {
    String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass);
    if(nnSearchClassSpec.length == 0) { 
      throw new Exception("Invalid NearestNeighbourSearch algorithm " +
                          "specification string."); 
    }
    String className = nnSearchClassSpec[0];
    nnSearchClassSpec[0] = "";

    setNearestNeighbourSearchAlgorithm( (NearestNeighbourSearch)
                Utils.forName( NearestNeighbourSearch.class, 
                               className, 
                               nnSearchClassSpec)
                                      );
  }
  else 
    this.setNearestNeighbourSearchAlgorithm(new LinearNNSearch());
  
  Utils.checkForRemainingOptions(options);
}
 
Example #4
Source File: LWL.java    From tsml with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Parses a given list of options. <p/>
 *
 <!-- options-start -->
 * Valid options are: <p/>
 * 
 * <pre> -A
 *  The nearest neighbour search algorithm to use (default: weka.core.neighboursearch.LinearNNSearch).
 * </pre>
 * 
 * <pre> -K &lt;number of neighbours&gt;
 *  Set the number of neighbours used to set the kernel bandwidth.
 *  (default all)</pre>
 * 
 * <pre> -U &lt;number of weighting method&gt;
 *  Set the weighting kernel shape to use. 0=Linear, 1=Epanechnikov,
 *  2=Tricube, 3=Inverse, 4=Gaussian.
 *  (default 0 = Linear)</pre>
 * 
 * <pre> -D
 *  If set, classifier is run in debug mode and
 *  may output additional info to the console</pre>
 * 
 * <pre> -W
 *  Full name of base classifier.
 *  (default: weka.classifiers.trees.DecisionStump)</pre>
 * 
 * <pre> 
 * Options specific to classifier weka.classifiers.trees.DecisionStump:
 * </pre>
 * 
 * <pre> -D
 *  If set, classifier is run in debug mode and
 *  may output additional info to the console</pre>
 * 
 <!-- options-end -->
 *
 * @param options the list of options as an array of strings
 * @throws Exception if an option is not supported
 */
public void setOptions(String[] options) throws Exception {

  String knnString = Utils.getOption('K', options);
  if (knnString.length() != 0) {
    setKNN(Integer.parseInt(knnString));
  } else {
    setKNN(-1);
  }

  String weightString = Utils.getOption('U', options);
  if (weightString.length() != 0) {
    setWeightingKernel(Integer.parseInt(weightString));
  } else {
    setWeightingKernel(LINEAR);
  }
  
  String nnSearchClass = Utils.getOption('A', options);
  if(nnSearchClass.length() != 0) {
    String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass);
    if(nnSearchClassSpec.length == 0) { 
      throw new Exception("Invalid NearestNeighbourSearch algorithm " +
                          "specification string."); 
    }
    String className = nnSearchClassSpec[0];
    nnSearchClassSpec[0] = "";

    setNearestNeighbourSearchAlgorithm( (NearestNeighbourSearch)
                Utils.forName( NearestNeighbourSearch.class, 
                               className, 
                               nnSearchClassSpec)
                                      );
  }
  else 
    this.setNearestNeighbourSearchAlgorithm(new LinearNNSearch());

  super.setOptions(options);
}
 
Example #5
Source File: LWL.java    From tsml with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the current nearestNeighbourSearch algorithm in use.
 * @return the NearestNeighbourSearch algorithm currently in use.
 */
public NearestNeighbourSearch getNearestNeighbourSearchAlgorithm() {
  return m_NNSearch;
}
 
Example #6
Source File: LWL.java    From tsml with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Sets the nearestNeighbourSearch algorithm to be used for finding nearest
 * neighbour(s).
 * @param nearestNeighbourSearchAlgorithm - The NearestNeighbourSearch class.
 */
public void setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch nearestNeighbourSearchAlgorithm) {
  m_NNSearch = nearestNeighbourSearchAlgorithm;
}
 
Example #7
Source File: IBk.java    From tsml with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the current nearestNeighbourSearch algorithm in use.
 * @return the NearestNeighbourSearch algorithm currently in use.
 */
public NearestNeighbourSearch getNearestNeighbourSearchAlgorithm() {
  return m_NNSearch;
}
 
Example #8
Source File: IBk.java    From tsml with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Sets the nearestNeighbourSearch algorithm to be used for finding nearest
 * neighbour(s).
 * @param nearestNeighbourSearchAlgorithm - The NearestNeighbourSearch class.
 */
public void setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch nearestNeighbourSearchAlgorithm) {
  m_NNSearch = nearestNeighbourSearchAlgorithm;
}
 
Example #9
Source File: YATSI.java    From collective-classification-weka-package with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the current nearestNeighbourSearch algorithm in use.
 * @return the NearestNeighbourSearch algorithm currently in use.
 */
public NearestNeighbourSearch getNearestNeighbourSearchAlgorithm() {
  return m_NNSearch;
}
 
Example #10
Source File: YATSI.java    From collective-classification-weka-package with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Sets the nearestNeighbourSearch algorithm to be used for finding nearest
 * neighbour(s).
 * @param value The NearestNeighbourSearch class.
 */
public void setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch value) {
  m_NNSearch = value;
}
 
Example #11
Source File: CollectiveIBk.java    From collective-classification-weka-package with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the current nearestNeighbourSearch algorithm in use.
 * 
 * @return 		the NearestNeighbourSearch algorithm currently in use.
 */
public NearestNeighbourSearch getNearestNeighbourSearchAlgorithm() {
  return m_Classifier.getNearestNeighbourSearchAlgorithm();
}
 
Example #12
Source File: CollectiveIBk.java    From collective-classification-weka-package with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Sets the nearestNeighbourSearch algorithm to be used for finding nearest
 * neighbour(s).
 * 
 * @param value 	The NearestNeighbourSearch class.
 */
public void setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch value) {
  m_Classifier.setNearestNeighbourSearchAlgorithm(value);
}