KMeansUtil implements Serializable { public static FastDistanceMatrixData buildCentroidsMatrix(List<FastDistanceVectorData> vectors, FastDistance distance, int vectorSize) { DenseMatrix matrix = new DenseMatrix(vectorSize, vectors.size()); for (int i = 0; i < vectors.size(); i++) { MatVecOp.appendVectorToMatrix(matrix, false, i, vectors.get(i).getVector()); } FastDistanceMatrixData centroid = new FastDistanceMatrixData(matrix); distance.updateLabel(centroid); return centroid; }  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 buildCentroidsMatrixTest() { List<FastDistanceVectorData> samples = new ArrayList<>(); List<Vector> vectorList = new ArrayList<>(); for (int i = 0; i < length; i++) { vectorList.add(DenseVector.ones(vectorSize).scale(i)); } vectorList.forEach(vec -> samples.add(distance.prepareVectorData(Tuple2.of(vec, null)))); FastDistanceMatrixData matrixData = KMeansUtil.buildCentroidsMatrix(samples, distance, vectorSize); DenseMatrix matrix = matrixData.getVectors(); Assert.assertEquals(matrix.numCols(), length); Assert.assertEquals(matrix.numRows(), vectorSize); double[] label = matrixData.getLabel().getData(); for (int i = 0; i < vectorList.size(); i++) { Assert.assertEquals(label[i], vectorList.get(i).normL2Square(), 0.001); } }