我正在使用Flink(Kmeans)提供的集群示例,并尝试扩展其功能。目的是通过计算由每个质心之间的距离组成的多维数组来减少距离计算的次数,以便可以在double[][]
数组中找到这些距离。当为点分配聚类时,必须在每次迭代的开始时计算并广播此数组。
我尝试了以下方法:
public static final class computeCentroidInterDistance implements MapFunction<Tuple2<Centroid, Centroid>, Tuple3<Integer, Integer, Double>> {
@Override
public Tuple3<Integer, Integer, Double> map(Tuple2<Centroid, Centroid> centroid) throws Exception {
return new Tuple3<>(centroid.f0.id, centroid.f1.id, centroid.f0.euclideanDistance(centroid.f1));
}
}
DataSet<Centroid> centroids = getCentroidDataSet(..);
DataSet<Tuple3<Integer, Integer, Double>> distances = centroids
.crossWithTiny(centroids)
.map(new computeCentroidInterDistance());
但是,我没有看到DataSet的距离如何用于我的用例,因为它没有以可用于查找两个不同质心之间的距离的任何特定顺序返回。有更好的方法吗?
数据集本质上是无序和分片的,两者都不适合您的用例。
您要做的是首先在一个方法调用中收集所有质心。
DataSet<double[][]> matrix = centroids.reduceGroup(...)
在reduceGroup中,您可以访问所有元素,并且可以执行计算。输出应该是您的double [] []矩阵。
然后可以通过广播分发矩阵。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句