public static KmeansResult nearestCentroid(double[] targetPoint, double[][] centroids)
    {
        int nearestCentroidIndex = 0;
        Double minDistance = Double.MAX_VALUE;
        double[] currentCentroid = null;
        Double currentDistance;
        for (int index = 0; index < centroids.length; index++)
        {
            currentCentroid = centroids[index];
            if (currentCentroid != null)
            {
                currentDistance = MathUtils.distance(targetPoint, currentCentroid);
                if (currentDistance < minDistance)
                {
                    minDistance = currentDistance;
                    nearestCentroidIndex = index;
                }
            }
        }

        currentCentroid = centroids[nearestCentroidIndex];

        KmeansResult result = new KmeansResult();
        result.setDataPoint(targetPoint);
        result.setCentroidIndex(nearestCentroidIndex);
        result.setCentroid(currentCentroid);
        result.setDistance(minDistance);

        return result;
    }
public static KmeansResult nearestCentroid ( double [ ] targetPoint , double [ ] [ ] centroids ) { int nearestCentroidIndex = 0 ; Double minDistance = Double . MAX_VALUE ; double [ ] currentCentroid = null ; Double currentDistance ; for ( int index = 0 ; index < centroids . length ; index ++ ) { currentCentroid = centroids [ index ] ; if ( currentCentroid != null ) { currentDistance = MathUtils . distance ( targetPoint , currentCentroid ) ; if ( currentDistance < minDistance ) { minDistance = currentDistance ; nearestCentroidIndex = index ; } } } currentCentroid = centroids [ nearestCentroidIndex ] ; KmeansResult result = new KmeansResult ( ) ; result . setDataPoint ( targetPoint ) ; result . setCentroidIndex ( nearestCentroidIndex ) ; result . setCentroid ( currentCentroid ) ; result . setDistance ( minDistance ) ; return result ; }
対象点と中心点配列を指定し、対象点に最も近い中心点のインデックスを取得し、結果エンティティを返す

@param targetPoint 判定対象ポイント
@param centroids 中心点配列
@return Kmeans算出結果