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);
        }
コード例 #2
0
        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);
        }