Skip to content

Instantly share code, notes, and snippets.

@nivdul
Created April 19, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nivdul/1ee82f923991fea93bc6 to your computer and use it in GitHub Desktop.
Save nivdul/1ee82f923991fea93bc6 to your computer and use it in GitHub Desktop.
Average absolute difference
/**
* @return Vector [ (1 / n ) * ∑ |b - mean_b|, for b in {x,y,z} ]
*/
public static double[] computeAvgAbsDifference(JavaRDD<double[]> data, double[] mean) {
// then for each point x compute x - mean
// then apply an absolute value: |x - mean|
JavaRDD<Vector> abs = data.map(record -> new double[]{Math.abs(record[0] - mean[0]),
Math.abs(record[1] - mean[1]),
Math.abs(record[2] - mean[2])})
.map(Vectors::dense);
// And to finish apply the mean: for each axis (1 / n ) * ∑ |b - mean|
return Statistics.colStats(abs.rdd()).mean().toArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment