Example #1
0
File: Form1.cs Project: Shock69/FCM
        private void button1_Click(object sender, EventArgs e)
        {
            List<ClusterPoint> points = new List<ClusterPoint>
            {
                new ClusterPoint(new List<double>(){0, 4}),
                new ClusterPoint(new List<double>(){0, 2}),
                new ClusterPoint(new List<double>(){0, 0}),
                new ClusterPoint(new List<double>(){1, 3}),
                new ClusterPoint(new List<double>(){1, 2}),
                new ClusterPoint(new List<double>(){1, 1}),
                new ClusterPoint(new List<double>(){2, 2}),
                new ClusterPoint(new List<double>(){3, 2}),
                new ClusterPoint(new List<double>(){4, 2}),
                new ClusterPoint(new List<double>(){5, 3}),
                new ClusterPoint(new List<double>(){5, 2}),
                new ClusterPoint(new List<double>(){5, 1}),
                new ClusterPoint(new List<double>(){6, 4}),
                new ClusterPoint(new List<double>(){6, 2}),
                new ClusterPoint(new List<double>(){6, 0})
            };                                          


            List<ClusterCentroid> centroids = new List<ClusterCentroid>
            {
                new ClusterCentroid(new List<double>(){1, 2}),
                new ClusterCentroid(new List<double>(){6, 2})
            };


            CMeansAlgorithm alg = new CMeansAlgorithm(points, centroids, 2);
            int iterations = alg.Run(Math.Pow(10, -5));

            double[,] matrix = alg.U;

            for (int j = 0; j < points.Count; j++)
            {
                for (int i = 0; i < centroids.Count; i++)
                {
                    ClusterPoint p = points[j];
                    Console.WriteLine("{0:00} Point: ({1};{2}) ClusterIndex: {3} Value: {4:0.000}", j + 1, p.Data[0], p.Data[1], p.ClusterIndex, matrix[j, i]);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Iteration count: {0}", iterations);
            Console.WriteLine();
            Console.WriteLine("Please press any key to exit...");
            Console.ReadLine();
        }
Example #2
0
        private void cToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<ClusterPoint> points = new List<ClusterPoint>();
            List<ClusterCentroid> centroids = new List<ClusterCentroid>();

            foreach (DataRow row in m_LearningSample.Rows)
            {
                string tag = "";
                List<double> data = new List<double>();
                for(int colNum = 0; colNum<m_LearningSample.Columns.Count; colNum++)
                {
                    if (!m_Descriptions[colNum].IsNotUse)
                    {
                        data.Add(double.Parse(row.Field<string>(colNum), CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        tag = row.Field<string>(colNum);
                    }
                }
                points.Add(new ClusterPoint(data, tag));
            }

            Random rnd = new Random();
            for (int centroidNum = 0; centroidNum < 10; centroidNum++)
            {
                List<double> data = new List<double>();
                for (int colNum = 0; colNum < m_LearningSample.Columns.Count; colNum++)
                {
                    if (!m_Descriptions[colNum].IsNotUse)
                    {
                        data.Add(0.0/*rnd.NextDouble()*/);
                    }
                }
                centroids.Add(new ClusterCentroid(data));
            }

            CMeansAlgorithm alg = new CMeansAlgorithm(points, centroids, 2);
            int iterations = alg.Run(Math.Pow(10, -5));

            double[,] matrix = alg.U;

            for (int j = 0; j < points.Count; j++)
            {
                double max = -1.0;
                string output = string.Empty;
                for (int i = 0; i < centroids.Count; i++)
                {
                    if(max < matrix[j, i])
                    {
                        ClusterPoint p = points[j];
                        StringBuilder sb = new StringBuilder();
                        for (int k = 0; k < p.Data.Count; k++)
                        {
                            sb.AppendFormat("{0}; ", p.Data[k]);
                        }
                        max = matrix[j, i];
                        output = string.Format("{0:00} Point: ({1}) ClusterIndex: {2} Value: {3} Class {4}", j + 1, sb, p.ClusterIndex, matrix[j, i], p.Tag);                        
                    }
                }
                Console.WriteLine(output);
            }

            Console.WriteLine();
            Console.WriteLine("Iteration count: {0}", iterations);
        }