Java Code Examples for org.apache.commons.math3.ml.distance.EuclideanDistance#compute()

The following examples show how to use org.apache.commons.math3.ml.distance.EuclideanDistance#compute() . 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: EuclideanDistanceEvaluator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object doWork(Object first, Object second) throws IOException{
  if(null == first){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));
  }
  if(null == second){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));
  }
  if(!(first instanceof List<?>)){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));
  }
  if(!(second instanceof List<?>)){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));
  }

  EuclideanDistance distance = new EuclideanDistance();
  return distance.compute(
    ((List)first).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray(),
    ((List)second).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray()
  );
}
 
Example 2
Source File: HierarchicalClustering.java    From HMMRATAC with GNU General Public License v3.0 6 votes vote down vote up
private void iterate(){
	ArrayList<ClusterNode> temp = new ArrayList<ClusterNode>();
	EuclideanDistance ed = new EuclideanDistance();
	for (int i = 0; i < clusters.size();i++){
		double min = Double.POSITIVE_INFINITY;
		int best = -1;
		for (int a = 0; a < clusters.size();a++){
			if (i != a){
				double dis = ed.compute(clusters.get(i).getKey(), clusters.get(a).getKey());
				if (dis < min){
					min = dis;
					best = a;
				}
			}
		}
		
	}
}
 
Example 3
Source File: WeightVectorNeighborhood.java    From jMetal with MIT License 6 votes vote down vote up
private void initializeNeighborhood() {
  EuclideanDistance euclideanDistance = new EuclideanDistance();
  double[] x = new double[numberOfWeightVectors];
  int[] idx = new int[numberOfWeightVectors];

  for (int i = 0; i < numberOfWeightVectors; i++) {
    // calculate the distances based on weight vectors
    for (int j = 0; j < numberOfWeightVectors; j++) {
      x[j] = euclideanDistance.compute(weightVector[i], weightVector[j]);
      idx[j] = j;
    }

    // find 'niche' nearest neighboring subproblems
    minFastSort(x, idx, numberOfWeightVectors, neighborhoodSize);

    System.arraycopy(idx, 0, neighborhood[i], 0, neighborhoodSize);
  }
}
 
Example 4
Source File: DBScanModel.java    From egads with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void tune(DataSequence observedSeries,
                 DataSequence expectedSeries) throws Exception {
    // Compute the time-series of errors.
    HashMap<String, ArrayList<Float>> allErrors = aes.initAnomalyErrors(observedSeries, expectedSeries);
    List<IdentifiedDoublePoint> points = new ArrayList<IdentifiedDoublePoint>();
    EuclideanDistance ed = new EuclideanDistance();
    int n = observedSeries.size();
    
    for (int i = 0; i < n; i++) {
        double[] d = new double[(aes.getIndexToError().keySet()).size()];
       
        for (int e = 0; e < (aes.getIndexToError().keySet()).size(); e++) {
             d[e] = allErrors.get(aes.getIndexToError().get(e)).get(i);
        }
        points.add(new IdentifiedDoublePoint(d, i));
    }
    
    double sum = 0.0;
    double count = 0.0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            sum += ed.compute(points.get(i).getPoint(), points.get(j).getPoint());
            count++;
        }
    }
    eps = ((double) this.sDAutoSensitivity) * (sum / count);   
    minPoints = ((int) Math.ceil(((double) this.amntAutoSensitivity) * ((double) n)));     
    dbscan = new DBSCANClusterer<IdentifiedDoublePoint>(eps, minPoints);
}