weka.core.DistanceFunction Java Examples

The following examples show how to use weka.core.DistanceFunction. 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: NearestNeighbourSearch.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses a given list of options. Valid options are:
 *
 <!-- options-start -->
 <!-- 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 nnSearchClass = Utils.getOption('A', options);
  if(nnSearchClass.length() != 0) {
    String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass);
    if(nnSearchClassSpec.length == 0) { 
      throw new Exception("Invalid DistanceFunction specification string."); 
    }
    String className = nnSearchClassSpec[0];
    nnSearchClassSpec[0] = "";

    setDistanceFunction( (DistanceFunction)
                          Utils.forName( DistanceFunction.class, 
                                         className, nnSearchClassSpec) );
  }
  else {
    setDistanceFunction(new EuclideanDistance());
  }
  
  setMeasurePerformance(Utils.getFlag('P',options));
}
 
Example #3
Source File: ClusteringUtilities.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static double[][] createDistanceMatrix(Instances data, DistanceFunction distFunc){
    double[][] distMatrix = new double[data.numInstances()][];
    distFunc.setInstances(data);

    for (int i = 1; i < data.numInstances(); i++){
        distMatrix[i] = new double[i];
        Instance first = data.get(i);

        for (int n = 0; n < i; n++){
            distMatrix[i][n] = distFunc.distance(first, data.get(n));
        }
    }

    return distMatrix;
}
 
Example #4
Source File: InstanceTools.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static Pair<Instance, Double> findMinDistance(Instances data, Instance inst, DistanceFunction dist){
    double min = dist.distance(data.get(0), inst);
    Instance minI = data.get(0);
    for (int i = 1; i < data.numInstances(); i++) {
        double temp = dist.distance(data.get(i), inst);
        if(temp < min){
            min = temp;
            minI = data.get(i);
        }
    }
    
    return new Pair(minI, min);
}
 
Example #5
Source File: KDTree.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * sets the distance function to use for nearest neighbour search.
 * 
 * @param df		the distance function to use
 * @throws Exception	if not EuclideanDistance
 */
public void setDistanceFunction(DistanceFunction df) throws Exception {
  if (!(df instanceof EuclideanDistance))
    throw new Exception("KDTree currently only works with "
        + "EuclideanDistanceFunction.");
  m_DistanceFunction = m_EuclideanDistance = (EuclideanDistance) df;
}
 
Example #6
Source File: XMeans.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Gets the distance function specification string, which contains the 
  * class name of the distance function class and any options to it.
  *
  * @return the distance function specification string
  */
 protected String getDistanceFSpec() {
   
   DistanceFunction d = getDistanceF();
   if (d instanceof OptionHandler) {
     return d.getClass().getName() + " "
+ Utils.joinOptions(((OptionHandler) d).getOptions());
   }
   return d.getClass().getName();
 }
 
Example #7
Source File: SimpleKMeans.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * sets the distance function to use for instance comparison.
 * 
 * @param df the new distance function to use
 * @throws Exception if instances cannot be processed
 */
public void setDistanceFunction(DistanceFunction df) throws Exception {
  if (!(df instanceof EuclideanDistance)
      && !(df instanceof ManhattanDistance)) {
    throw new Exception(
        "SimpleKMeans currently only supports the Euclidean and Manhattan distances.");
  }
  m_DistanceFunction = df;
}
 
Example #8
Source File: TransformedDistanceMeasure.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public DistanceFunction getDistanceFunction() {
    return distanceFunction;
}
 
Example #9
Source File: KMedoids.java    From apogen with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the options
 * 
 * @param options
 *            a list of options as an array of strings
 * @throws Exception
 *             if an option is not support
 */
public void setOptions(String[] options) throws Exception {

	// Set the number of the cluster
	String optionString = Utils.getOption('N', options);
	if (optionString.length() != 0) {
		setNumClusters(Integer.parseInt(optionString));
	}

	// Set the number of the maximum iterations
	optionString = Utils.getOption("I", options);
	if (optionString.length() != 0) {
		setMaxIterations(Integer.parseInt(optionString));
	}

	// Set the repeat times
	optionString = Utils.getOption("J", options);
	if (optionString.length() != 0) {
		setRepeatTimes(Integer.parseInt(optionString));
	}

	// Set the distance function
	String distFunctionClass = Utils.getOption('A', options);
	if (distFunctionClass.length() != 0) {
		String distFunctionClassSpec[] = Utils.splitOptions(distFunctionClass);
		if (distFunctionClassSpec.length == 0) {
			throw new Exception("Invalid DistanceFunction specification string.");
		}
		String className = distFunctionClassSpec[0];
		distFunctionClassSpec[0] = "";

		setDistanceFunction(
				(DistanceFunction) Utils.forName(DistanceFunction.class, className, distFunctionClassSpec));
	} else {
		setDistanceFunction(new EuclideanDistance());
	}

	// Set whether to output the cluster result
	m_SaveClusterResult = Utils.getFlag("s", options);

	// Other options
	super.setOptions(options);
}
 
Example #10
Source File: AbstractVectorClusterer.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public void setDistanceFunction(DistanceFunction distFunc){
    this.distFunc = distFunc;
}
 
Example #11
Source File: BallNode.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Calculates the radius of a node.
 * 
 * @param start The start index of the portion in indices array 
 * that belongs to the node.
 * @param end The end index of the portion in indices array 
 * that belongs to the node. 
 * @param instList The indices array holding indices of 
 * instances. 
 * @param insts The actual instances. instList points to 
 * instances in this object. 
 * @param pivot The centre/pivot of the node. 
 * @param distanceFunction The distance function to use to 
 * calculate the radius. 
 * @return The radius of the node. 
 * @throws Exception If there is some problem calculating the 
 * radius. 
 */
public static double calcRadius(int start, int end, int[] instList, 
                               Instances insts, Instance pivot, 
                               DistanceFunction distanceFunction) 
                                                           throws Exception {
  double radius = Double.NEGATIVE_INFINITY;
  
  for(int i=start; i<=end; i++) {
    double dist = distanceFunction.distance(pivot, 
                                            insts.instance(instList[i]), Double.POSITIVE_INFINITY);
    
    if(dist>radius)
      radius = dist;
  }
  return Math.sqrt(radius);
}
 
Example #12
Source File: HierarchicalClusterer.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/>
 * 
  <!-- 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 {
  m_bPrintNewick = Utils.getFlag('P', options);

  String optionString = Utils.getOption('N', options); 
  if (optionString.length() != 0) {
    Integer temp = new Integer(optionString);
    setNumClusters(temp);
  }
  else {
    setNumClusters(2);
  }

  setDebug(Utils.getFlag('D', options));
  setDistanceIsBranchLength(Utils.getFlag('B', options));

  String sLinkType = Utils.getOption('L', options);


  if (sLinkType.compareTo("SINGLE") == 0) {setLinkType(new SelectedTag(SINGLE, TAGS_LINK_TYPE));}
  if (sLinkType.compareTo("COMPLETE") == 0) {setLinkType(new SelectedTag(COMPLETE, TAGS_LINK_TYPE));}
  if (sLinkType.compareTo("AVERAGE") == 0) {setLinkType(new SelectedTag(AVERAGE, TAGS_LINK_TYPE));}
  if (sLinkType.compareTo("MEAN") == 0) {setLinkType(new SelectedTag(MEAN, TAGS_LINK_TYPE));}
  if (sLinkType.compareTo("CENTROID") == 0) {setLinkType(new SelectedTag(CENTROID, TAGS_LINK_TYPE));}
  if (sLinkType.compareTo("WARD") == 0) {setLinkType(new SelectedTag(WARD, TAGS_LINK_TYPE));}
  if (sLinkType.compareTo("ADJCOMLPETE") == 0) {setLinkType(new SelectedTag(ADJCOMLPETE, TAGS_LINK_TYPE));}
  if (sLinkType.compareTo("NEIGHBOR_JOINING") == 0) {setLinkType(new SelectedTag(NEIGHBOR_JOINING, TAGS_LINK_TYPE));}

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

    setDistanceFunction( (DistanceFunction)
        Utils.forName( DistanceFunction.class, 
            className, nnSearchClassSpec) );
  }
  else {
    setDistanceFunction(new EuclideanDistance());
  }

  Utils.checkForRemainingOptions(options);
}
 
Example #13
Source File: KNN.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
@Override public void setParams(final ParamSet params) {
    ParamHandler.setParam(params, DistanceMeasureable.getDistanceFunctionFlag(), this::setDistanceFunction, DistanceFunction.class);
    ParamHandler.setParam(params, getKFlag(), this::setK, Integer.class);
    ParamHandler.setParam(params, getEarlyAbandonFlag(), this::setEarlyAbandon, Boolean.class);
    ParamHandler.setParam(params, getRandomTieBreakFlag(), this::setRandomTieBreak, Boolean.class);
}
 
Example #14
Source File: TransformDistanceMeasure.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
@Override public void setParams(final ParamSet param) {
    super.setParams(param);
    ParamHandler.setParam(param, getTransformerFlag(), this::setTransformer, Filter.class);
    ParamHandler.setParam(param, DistanceMeasureable.getDistanceFunctionFlag(), this::setDistanceFunction,
                          DistanceFunction.class);
}
 
Example #15
Source File: TransformDistanceMeasure.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setDistanceFunction(DistanceFunction distanceFunction) {
    super.setDistanceFunction(distanceFunction);
}
 
Example #16
Source File: TransformDistanceMeasure.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public TransformDistanceMeasure(String name,
    Filter transformer, DistanceFunction distanceFunction) {
    super(name, transformer, distanceFunction);
}
 
Example #17
Source File: KNNLOOCV.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public KNNLOOCV(DistanceFunction df) {
    super(df);
    setAbleToEstimateOwnPerformance(true);
}
 
Example #18
Source File: KNN.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public KNN(DistanceFunction df) {
    this();
    setDistanceFunction(df);
}
 
Example #19
Source File: TransformedDistanceMeasure.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
protected void setDistanceFunction(DistanceFunction distanceFunction) {
    if(distanceFunction == null) throw new NullPointerException();
    this.distanceFunction = distanceFunction;
}
 
Example #20
Source File: TransformedDistanceMeasure.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public TransformedDistanceMeasure(String name, Filter transformer,
    DistanceFunction distanceFunction) {
    setName(name);
    setDistanceFunction(distanceFunction);
    setTransformer(transformer);
}
 
Example #21
Source File: KNN.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public DistanceFunction getDistanceFunction() {
    return distanceFunction;
}
 
Example #22
Source File: KNN.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public void setDistanceFunction(final DistanceFunction distanceFunction) {
    this.distanceFunction = distanceFunction;
}
 
Example #23
Source File: KMedoids.java    From apogen with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the distance function to use for instance comparison.
 * 
 * @param df
 *            the new distance function to use
 * @throws Exception
 *             if df is not EuclideanDistance or ManhattanDistance
 */
public void setDistanceFunction(DistanceFunction df) throws Exception {
	if ((df instanceof EuclideanDistance) || (df instanceof ManhattanDistance)) {
		m_DistanceFunction = df;
	} else {
		throw new Exception("MyPAM only support the Euclidean or Manhattan distance.");
	}
}
 
Example #24
Source File: SimpleKMeansWithSilhouette.java    From apogen with Apache License 2.0 3 votes vote down vote up
/**
 * sets the distance function to use for instance comparison.
 * 
 * @param df
 *            the new distance function to use
 * @throws Exception
 *             if instances cannot be processed
 */
public void setDistanceFunction(DistanceFunction df) throws Exception {
	if (!(df instanceof EuclideanDistance) && !(df instanceof ManhattanDistance)) {
		throw new Exception("SimpleKMeans currently only supports the Euclidean and Manhattan distances.");
	}
	m_DistanceFunction = df;
}
 
Example #25
Source File: BallNode.java    From tsml with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Calculates the radius of node.
 *  
 * @param instList The indices array containing the indices of the 
 * instances inside the node. 
 * @param insts The actual instances object. instList points to 
 * instances in this object.
 * @param pivot The centre/pivot of the node.
 * @param distanceFunction The distance fuction to use to calculate 
 * the radius. 
 * @return The radius of the node. 
 * @throws Exception If there is some problem in calculating the 
 * radius. 
 */
public static double calcRadius(int[] instList, Instances insts,Instance pivot, 
                               DistanceFunction distanceFunction) 
                                                throws Exception {
  return calcRadius(0, instList.length-1, instList, insts, 
                    pivot, distanceFunction);
}
 
Example #26
Source File: BallNode.java    From tsml with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Calculates the radius of a node based on its two 
 * child nodes (if merging two nodes).
 * @param child1 The first child of the node.
 * @param child2 The second child of the node.
 * @param pivot The centre/pivot of the node. 
 * @param distanceFunction The distance function to 
 * use to calculate the radius
 * @return The radius of the node. 
 * @throws Exception If there is some problem 
 * in calculating the radius.
 */
public static double calcRadius(BallNode child1, BallNode child2, 
                                Instance pivot, 
                                DistanceFunction distanceFunction) 
                                                           throws Exception {
  Instance p1 = child1.getPivot(), p2 = child2.getPivot();                                                               
  
  double radius = child1.getRadius() + distanceFunction.distance(p1, p2) + 
                  child2.getRadius();
  
  return radius/2;
}
 
Example #27
Source File: CoverTree.java    From tsml with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Sets the distance function to use for nearest neighbour search.
 * Currently only EuclideanDistance is supported.
 * 
 * @param df 		the distance function to use 
 * @throws Exception 	if not EuclideanDistance
 */
public void setDistanceFunction(DistanceFunction df) throws Exception {
  if (!(df instanceof EuclideanDistance))
    throw new Exception("CoverTree currently only works with "
 + "EuclideanDistanceFunction.");
  m_DistanceFunction = m_EuclideanDistance = (EuclideanDistance) df;
}
 
Example #28
Source File: KMedoids.java    From apogen with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the distance function currently in use.
 * 
 * @return the distance function currently in use
 */
public DistanceFunction getDistanceFunction() {
	return m_DistanceFunction;
}
 
Example #29
Source File: LLGC.java    From collective-classification-weka-package with GNU General Public License v3.0 2 votes vote down vote up
/** 
 * sets the distance function to use 
 * 
 * @param value	the distance function to use
 */
public void setDistanceFunction(DistanceFunction value) {
  m_DistanceFunction = value;
}
 
Example #30
Source File: SimpleKMeansWithSilhouette.java    From apogen with Apache License 2.0 2 votes vote down vote up
/**
 * returns the distance function currently in use.
 * 
 * @return the distance function
 */
public DistanceFunction getDistanceFunction() {
	return m_DistanceFunction;
}