コード例 #1
0
        public static int[] Clusterize(float[][] records, int k, float error = 1e-5f, int maxIter = 10)
        {
            var distances = new float[k, records.Length];
            var labels    = new int[records.Length];

            var centroidsPrev = KMeansCentroid.RandomPartitioning(records, k);
            var centroidsNext = JaggedArray.InitAndCopy(centroidsPrev, k);
            var iteration     = 0;

            float e;

            do
            {
                CalculateDistancesBetweenRecordsAndCentroids(distances, records, centroidsNext);
                ComputeLabels(labels, distances, k);
                KMeansCentroid.Compute(centroidsNext, records, labels);

                e = ComputeError(centroidsPrev, centroidsNext);
                JaggedArray.Copy(centroidsNext, centroidsPrev, k);
            } while (iteration++ < maxIter && e > error);

            return(labels);
        }