/*
         * public double ComputeVariance(double[] data)
         * {
         *  double avg = data.Average();
         *  double sum = 0;
         *  foreach (double num in data)
         *  {
         *      sum += (num - avg)*(num - avg);
         *  }
         *  return sum / data.Length;
         * }
         *
         * public double Gaussian(double num, double mean, double stddev)  // 1-D gaussian
         * {
         *  double res = (1 / (stddev * Math.Sqrt(2.0 * Math.PI))) *
         *      Math.Exp( (-1 * (num - mean) * (num - mean)) / (2 * stddev * stddev));
         *  return res;
         * }
         */



        private void btnInitialize_Click(object sender, EventArgs e)
        {
            PList.Clear();
            Random rand = new Random();

            double meanx0 = 100;
            double meanx1 = 200;
            double meanx2 = 325;
            double meanx3 = 425;

            double meany0 = 75;
            double meany1 = 150;
            double meany2 = 250;
            double meany3 = 350;

            double stddevx0 = 40;
            double stddevx1 = 70;
            double stddevx2 = 80;
            double stddevx3 = 60;
            double stddevy0 = 60;
            double stddevy1 = 80;
            double stddevy2 = 80;
            double stddevy3 = 85;

            int index = 0;

            for (int i = 0; i < dataLength / 4; i++)
            {
                MyPoint pt   = new MyPoint();
                double  rnum = rand.NextDouble();
                if (rnum < 0.5)
                {
                    pt.X = (int)(rand.NextDouble() * stddevx0 / 2 + meanx0);
                }
                else
                {
                    pt.X = (int)(-1 * rand.NextDouble() * stddevx0 / 2 + meanx0);
                }
                if (rnum < 0.5)
                {
                    pt.Y = (int)(rand.NextDouble() * stddevy0 / 2 + meany0);
                }
                else
                {
                    pt.Y = (int)(-1 * rand.NextDouble() * stddevy0 / 2 + meany0);
                }
                index++;
                PList.Add(pt);
            }

            for (int i = 0; i < dataLength / 4; i++)
            {
                MyPoint pt   = new MyPoint();
                double  rnum = rand.NextDouble();
                if (rnum < 0.5)
                {
                    pt.X = (int)(rand.NextDouble() * stddevx1 / 2 + meanx1);
                }
                else
                {
                    pt.X = (int)(-1 * rand.NextDouble() * stddevx1 / 2 + meanx1);
                }
                if (rnum < 0.5)
                {
                    pt.Y = (int)(rand.NextDouble() * stddevy1 / 2 + meany1);
                }
                else
                {
                    pt.Y = (int)(-1 * rand.NextDouble() * stddevy1 / 2 + meany1);
                }
                index++;
                PList.Add(pt);
            }

            for (int i = 0; i < dataLength / 4; i++)
            {
                double  rnum = rand.NextDouble();
                MyPoint pt   = new MyPoint();
                if (rnum < 0.5)
                {
                    pt.X = (int)(rand.NextDouble() * stddevx2 / 2 + meanx2);
                }
                else
                {
                    pt.X = (int)(-1 * rand.NextDouble() * stddevx2 / 2 + meanx2);
                }
                if (rnum < 0.5)
                {
                    pt.Y = (int)(rand.NextDouble() * stddevy2 / 2 + meany2);
                }
                else
                {
                    pt.Y = (int)(-1 * rand.NextDouble() * stddevy2 / 2 + meany2);
                }
                PList.Add(pt);
                index++;
            }

            for (int i = 0; i < dataLength / 4; i++)
            {
                double  rnum = rand.NextDouble();
                MyPoint pt   = new MyPoint();
                if (rnum < 0.5)
                {
                    pt.X = (int)(rand.NextDouble() * stddevx3 / 2 + meanx3);
                }
                else
                {
                    pt.X = (int)(-1 * rand.NextDouble() * stddevx3 / 2 + meanx3);
                }
                if (rnum < 0.5)
                {
                    pt.Y = (int)(rand.NextDouble() * stddevy3 / 2 + meany3);
                }
                else
                {
                    pt.Y = (int)(-1 * rand.NextDouble() * stddevy3 / 2 + meany3);
                }
                index++;
                PList.Add(pt);
            }
            Visualization.DrawPoints(pic1, PList, 0.7);
            MessageBox.Show("Data initialized for " + PList.Count + " points");
        }
Exemple #2
0
        public double MInertia(Bitmap aoBmp)
        {
            double kdRes = 0.0;

            double[] kdD = new double[this.k];           // Diameters for each cluster
            double[ , ]        kdP = new double[this.k, 4];
            List <MyPoint>[] koList   = new List <MyPoint> [k];
            MyPoint[]        koCenter = new MyPoint[k];
            int     kiC;
            int     x = 0, y = 0;
            double  kdPm;
            MyPoint koPt;

            for (int i = 0; i < k; i++)
            {
                kdD[i]        = 0.0;
                koList[i]     = new List <MyPoint>( );
                koCenter[i]   = new MyPoint( );
                koCenter[i].X = 0.0;
                koCenter[i].Y = 0.0;
            }

            // Find all points in each cluster
            for (int i = 0; i < X.Rows; i++)
            {
                // Gamma matrix has the probabilities for a data point for its membership in each cluster
                kiC  = 0;
                kdPm = Gamma[i, 0];
                for (int m = 0; m < k; m++)
                {
                    if (Gamma[i, m] > kdPm)
                    {
                        kiC  = m; // data i belongs to cluster m
                        kdPm = Gamma[i, m];
                    }
                }
                koPt = new MyPoint {
                    ClusterId = kiC, X = x, Y = y
                };
                koCenter[kiC].X += x;
                koCenter[kiC].Y += y;
                koList[kiC].Add(koPt);
                x++;
                if (x >= aoBmp.Width)
                {
                    x = 0;
                    y++;
                }
            }

            for (int i = 0; i < k; i++)
            {
                // Calculate the center of the cluster
                koCenter[i].X /= koList[i].Count;
                koCenter[i].Y /= koList[i].Count;

                // Calculate the diameter of each cluster
                for (int j = 0; j < koList[i].Count; j++)
                {
                    kdD[i] += Math.Sqrt((koList[i][j].X - koCenter[i].X) * (koList[i][j].X - koCenter[i].X) +
                                        (koList[i][j].Y - koCenter[i].Y) * (koList[i][j].Y - koCenter[i].Y));
                }

                // Normalize each diameter
                kdD[i] /= koList[i].Count;

                kdRes += kdD[i];
            }

            return(kdRes / this.ViK);
        }
        private void btnGMMND_Click(object sender, EventArgs e)
        {
            int k   = int.Parse(txtNumClusters.Text); // number of clusters
            int dim = 2;                              // number of dimensions for data
            //int datSize = 20;

            // read 2-D data
            //FileInfo fi = new FileInfo("d:\\csharp2016\\clusterdata\\data2D.txt");
            //FileInfo fi = new FileInfo("d:\\csharp2016\\clusterdata\\cdata1.txt");
            //StreamReader sr = new StreamReader(fi.Open(FileMode.Open, FileAccess.Read));
            //string sline = sr.ReadLine();
            //int datSize = 1;
            //while (sline != null)
            //{
            //    datSize++;
            //    sline = sr.ReadLine();
            //}
            //sr.Close();
            //sr = new StreamReader(fi.Open(FileMode.Open, FileAccess.Read));
            //sline = sr.ReadLine();
            //Matrix X = new Matrix(datSize, dim);
            //int count = 0;
            //while (sline != null)
            //{
            //    string[] parts = sline.Split(new char[] { '\t', ' ' });
            //    X[count, 0] = double.Parse(parts[1].Trim());
            //    X[count, 1] = double.Parse(parts[2].Trim());
            //    count++;
            //    sline = sr.ReadLine();
            //}
            //sr.Close();

            int            datSize = 1000;
            List <MyPoint> PList   = InitializeData(datSize);

            Mapack.Matrix X = new Matrix(datSize, dim);
            for (int i = 0; i < PList.Count; i++)
            {
                X[i, 0] = PList[i].X;
                X[i, 1] = PList[i].Y;
            }


            GMM_NDim gmmnd = new GMM_NDim(k, dim, X, dataSize);

            gmmnd.ComputeGMM_ND();

            // determine class membership i.e., which point belongs to which cluster
            PList = new List <MyPoint>();
            for (int i = 0; i < X.Rows; i++)
            {
                // Gamma matrix has the probabilities for a data point for its membership in each cluster
                double[] probabs   = new double[k];
                int      cnum      = 0;
                double   maxprobab = gmmnd.Gamma[i, 0];
                for (int m = 0; m < k; m++)
                {
                    if (gmmnd.Gamma[i, m] > maxprobab)
                    {
                        cnum      = m; // data i belongs to cluster m
                        maxprobab = gmmnd.Gamma[i, m];
                    }
                }
                MyPoint pt = new MyPoint {
                    ClusterId = cnum, X = X[i, 0], Y = X[i, 1]
                };
                PList.Add(pt);
            }
            MyImageProc.DrawClusters(pic1, PList, 1, k);
        }
        List <MyPoint> InitializeData(int datSize)
        {
            List <MyPoint> PList = new List <MyPoint>();

            PList.Clear();
            Random rand       = new Random();
            int    dataLength = datSize; // number of data points
            // create 4 distributions with different means and std devs
            double meanx0 = 150;
            double meanx1 = 180;
            double meanx2 = 425;
            double meanx3 = 475;
            double meany0 = 175;
            double meany1 = 250;
            double meany2 = 300;
            double meany3 = 320;

            double stddevx0 = 100;
            double stddevx1 = 90;
            double stddevx2 = 220;
            double stddevx3 = 260;
            double stddevy0 = 50;
            double stddevy1 = 80;
            double stddevy2 = 180;
            double stddevy3 = 195;
            int    index    = 0;

            for (int i = 0; i < dataLength / 4; i++)
            {
                MyPoint pt   = new MyPoint();
                double  rnum = rand.NextDouble();
                if (rnum < 0.5)
                {
                    pt.X = rand.NextDouble() * stddevx0 / 2 + meanx0;
                }
                else
                {
                    pt.X = -1 * rand.NextDouble() * stddevx0 / 2 + meanx0;
                }
                if (rnum < 0.5)
                {
                    pt.Y = rand.NextDouble() * stddevy0 / 2 + meany0;
                }
                else
                {
                    pt.Y = -1 * rand.NextDouble() * stddevy0 / 2 + meany0;
                }
                index++;
                PList.Add(pt);
            }

            for (int i = 0; i < dataLength / 4; i++)
            {
                MyPoint pt   = new MyPoint();
                double  rnum = rand.NextDouble();
                if (rnum < 0.5)
                {
                    pt.X = rand.NextDouble() * stddevx1 / 2 + meanx1;
                }
                else
                {
                    pt.X = -1 * rand.NextDouble() * stddevx1 / 2 + meanx1;
                }
                if (rnum < 0.5)
                {
                    pt.Y = rand.NextDouble() * stddevy1 / 2 + meany1;
                }
                else
                {
                    pt.Y = -1 * rand.NextDouble() * stddevy1 / 2 + meany1;
                }
                index++;
                PList.Add(pt);
            }

            for (int i = 0; i < dataLength / 4; i++)
            {
                double  rnum = rand.NextDouble();
                MyPoint pt   = new MyPoint();
                if (rnum < 0.5)
                {
                    pt.X = rand.NextDouble() * stddevx2 / 2 + meanx2;
                }
                else
                {
                    pt.X = -1 * rand.NextDouble() * stddevx2 / 2 + meanx2;
                }
                if (rnum < 0.5)
                {
                    pt.Y = rand.NextDouble() * stddevy2 / 2 + meany2;
                }
                else
                {
                    pt.Y = -1 * rand.NextDouble() * stddevy2 / 2 + meany2;
                }
                PList.Add(pt);
                index++;
            }

            for (int i = 0; i < dataLength / 4; i++)
            {
                double  rnum = rand.NextDouble();
                MyPoint pt   = new MyPoint();
                if (rnum < 0.5)
                {
                    pt.X = rand.NextDouble() * stddevx3 / 2 + meanx3;
                }
                else
                {
                    pt.X = -1 * rand.NextDouble() * stddevx3 / 2 + meanx3;
                }
                if (rnum < 0.5)
                {
                    pt.Y = rand.NextDouble() * stddevy3 / 2 + meany3;
                }
                else
                {
                    pt.Y = -1 * rand.NextDouble() * stddevy3 / 2 + meany3;
                }
                PList.Add(pt);
                index++;
            }
            MyImageProc.DrawClusters(pic1, PList, 1.0, 1);
            return(PList);
        }
        //CREATING CLUSTERS AND SHOWING MEANS FOR EACH CLUSTER
        private void btnCompute_Click(object sender, EventArgs e)
        {
            if (uploaded == true)
            {
                Stopwatch sw = new Stopwatch();

                try
                {
                    k = int.Parse(txtClusters.Text); //getting number of clusters
                }
                catch
                {
                    MessageBox.Show("Wrong input.");
                }


                //sw.Start();
                data  = new DataPoints(picture, picture.Width, picture.Height);
                PList = new List <MyPoint>(data.CreatePoints());
                X     = new Matrix(data_Size, dimenssions);
                for (int i = 0; i < PList.Count; i++)
                {
                    X[i, 0] = PList[i].Red;
                    X[i, 1] = PList[i].Green;
                    X[i, 2] = PList[i].Blue;
                }

                myGmm = new GMM_NDim(k, dimenssions, X);
                sw.Start();
                myGmm.ComputeGMM_ND();
                sw.Stop();
                MessageBox.Show("Time elapsed in ms: " + sw.ElapsedMilliseconds);

                // determine class membership i.e., which point belongs to which cluster
                PList = new List <MyPoint>();
                for (int i = 0; i < X.Rows; i++) //looping throught all the rows = #points of Gamma -- each col = probability for each cluster
                {
                    // Gamma matrix has the probabilities for a data point for its membership in each cluster
                    double[] probabs   = new double[k];
                    int      cnum      = 0;
                    double   maxprobab = myGmm.Gamma[i, 0];
                    for (int m = 0; m < k; m++)            // Going throught the cols --- each col has probability for one cluster
                    {
                        if (myGmm.Gamma[i, m] > maxprobab) //checking for the cluster with more probability
                        {
                            cnum      = m;                 // data i belongs to cluster m
                            maxprobab = myGmm.Gamma[i, m];
                        }
                    }
                    MyPoint pt = new MyPoint(X[i, 0], X[i, 1], X[i, 2], cnum); //X[i,0] = RED X[i,1] = GREEN X[i,2] = BLUE
                    PList.Add(pt);
                }

                computed = true;
                showMeans();
            }
            else
            {
                MessageBox.Show("You need to upload an image first.");
            }
        }