示例#1
0
        public static double[][] clustering(double[][] x, int k, kmeansType initType, out double error)
        {
            double[][] m = null;
            Console.WriteLine("Initialization...");
            switch (initType)
            {
            case kmeansType.Forgy: m = ForgyInit(x, k); break;

            case kmeansType.kmeanspp: m = kmeansppinit(x, k); break;

            case kmeansType.RandomPartition: m = RandomPartion(x, k); break;
            }
            int reassigned = 0;
            int iters      = 0;

            int[] a = new int[x.Length];
            do
            {
                reassigned = Assignment(x, m, ref a);
                if (reassigned != 0)
                {
                    m = Update(x, a, k);
                }
                Console.Write("\rIteration {0}, reassigned {1}            ", iters++, reassigned);
            }while (reassigned != 0);
            error = Error(x, m, a);
            return(m);
        }
示例#2
0
        public static double[][] clustering(double[][] x, int k, kmeansType initType, out double error)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            double[][] m = null;
            //Console.WriteLine("Initialization...");
            switch (initType)
            {
            case kmeansType.Forgy: m = ForgyInit(x, k); break;

            case kmeansType.kmeanspp: m = kmeansppinit(x, k); break;

            case kmeansType.RandomPartition: m = RandomPartion(x, k); break;
            }
            int confirmations = 0;

            int[] a          = new int[x.Length];
            int   reassigned = 0;
            int   iters      = 0;

            bool[]   ntu   = new bool[k];
            DateTime start = DateTime.Now;

            do
            {
                for (int i = 0; i < k; i++)
                {
                    ntu[i] = true;
                }
                do
                {
                    sw.Restart();
                    reassigned = Assignment(x, m, ref a, ref ntu);
                    if (reassigned != 0)
                    {
                        m = Update(x, m, a, ntu, k);
                    }
                    int unmoved = ntu.Count(z => !z);

                    _log.Info($"[{(DateTime.Now - start).TotalMilliseconds} ms] Iteration {iters++}, reassigned {reassigned} unmoved {unmoved} Time {sw.ElapsedMilliseconds} ms     ");

                    if (reassigned != 0)
                    {
                        confirmations = 0;
                    }
                }while (reassigned != 0);
                confirmations++;
                _log.Info($"CONFIRMATION {confirmations}");
            } while (confirmations < 2);

            error = Error(x, m, a);
            return(m);
        }
示例#3
0
 public static double[][] clustering(double[,] x, int k, int restarts, kmeansType initType)
 {
     double[][] tx = new double[x.GetLength(0)][];
     for (int row = 0; row < x.GetLength(0); row++)
     {
         tx[row] = new double[x.GetLength(1)];
         for (int i = 0; i < tx[row].Length; i++)
         {
             tx[row][i] = x[row, i];
         }
     }
     return(clustering(tx, k, restarts, initType));
 }
示例#4
0
        public static double[][] clustering(double[][] x, int k, int restarts, kmeansType initType)
        {
            double[][][] pool  = new double[restarts][][];
            double[]     error = new double[restarts];
            for (int r = 0; r < restarts; r++)
            {
                _log.Info($"Restart {r}");
                double er = 0.0;
                pool[r]  = clustering(x, k, initType, out er);
                error[r] = er;
                _log.Info($"Error {er}");
            }
            double min = error.Min();
            int    opt = error.FindIndex(z => z == min);

            return(pool[opt]);
        }
示例#5
0
        public static double[][] clustering(double[][] x, int k, int restarts, kmeansType initType)
        {
            double[][][] pool  = new double[restarts][][];
            double[]     error = new double[restarts];
            for (int r = 0; r < restarts; r++)
            {
                Console.WriteLine("Restart {0}", r);
                double er = 0.0;
                pool[r]  = clustering(x, k, initType, out er);
                error[r] = er;
                Console.WriteLine("Error {0}", er);
            }
            double min = error.Min();
            int    opt = error.FindIndex(z => z == min);

            return(pool[opt]);
        }