KMeansUtil implements Serializable { public static Tuple2<Integer, Double> getClosestClusterIndex(FastDistanceVectorData sample, FastDistanceMatrixData centroids, int k, FastDistance distance, DenseMatrix distanceMatrix) { getClusterDistances(sample, centroids, distance, distanceMatrix); double[] data = distanceMatrix.getData(); int index = getMinPointIndex(data, k); return Tuple2.of(index, data[index]); }  static FastDistanceMatrixData buildCentroidsMatrix(List<FastDistanceVectorData> vectors,
                                                              FastDistance distance,
                                                              int vectorSize); static int updateSumMatrix(FastDistanceVectorData sample,
                                      long sampleWeight,
                                      FastDistanceMatrixData centroids,
                                      int vectorSize,
                                      double[] sumMatrix,
                                      int k,
                                      FastDistance fastDistance,
                                      DenseMatrix distanceMatrix); static Tuple2<Integer, Double> getClosestClusterIndex(FastDistanceVectorData sample,
                                                                 FastDistanceMatrixData centroids,
                                                                 int k,
                                                                 FastDistance distance,
                                                                 DenseMatrix distanceMatrix); static double[] getClusterDistances(FastDistanceVectorData sample,
                                               FastDistanceMatrixData centroids,
                                               FastDistance distance,
                                               DenseMatrix distanceMatrix); static Tuple2<Integer, Double> getClosestClusterIndex(KMeansTrainModelData trainModelData,
                                                                 Vector sample,
                                                                 ContinuousDistance distance); static double[] getClusterDistances(KMeansTrainModelData trainModelData,
                                               Vector sample,
                                               ContinuousDistance distance); static int getMinPointIndex(double[] data, int endIndex); static int[] getKmeansPredictColIdxs(KMeansTrainModelData.ParamSummary params, String[] dataCols); static Vector getKMeansPredictVector(int[] colIdxs, Row row); static KMeansTrainModelData transformPredictDataToTrainData(KMeansPredictModelData predictModelData); static KMeansPredictModelData transformTrainDataToPredictData(KMeansTrainModelData trainModelData); static double[] getProbArrayFromDistanceArray(double[] distances); static KMeansTrainModelData loadModelForTrain(Params params, Iterable<String> data);  }
@Test public void getClusterFastDistanceTest() { KMeansPredictModelData predictModelData = new KMeansModelDataConverter().load(modelRows); FastDistanceVectorData sample = distance.prepareVectorData(Tuple2.of(VectorUtil.getVector("9 9 9"), null)); DenseMatrix distanceMatrix = new DenseMatrix(predictModelData.params.k, 1); Tuple2<Integer, Double> tuple2 = KMeansUtil.getClosestClusterIndex(sample, predictModelData.centroids, predictModelData.params.k, predictModelData.params.distanceType.getFastDistance(), distanceMatrix); Assert.assertEquals(tuple2.f0.intValue(), 0); Assert.assertEquals(tuple2.f1, 0.173, 0.01); double[] distanceMatrixData = distanceMatrix.getData(); Assert.assertEquals(distanceMatrixData[0], 0.173, 0.01); Assert.assertEquals(distanceMatrixData[1], 15.415, 0.01); }