Пример #1
0
        static void Main(string[] args)
        {
            int max = 100;

            for (int i = 2; i < max + 1; i++)
            {
                Parser         parser          = new Parser("./WineData.csv");
                KMeans         kmeans          = new KMeans();
                List <Cluster> clusteredResult = kmeans.mainLoop(i, 10, parser.ParsedContent);

                //foreach (Cluster cluster in clusteredResult){
                //    Console.Write((cluster.Id + 1).ToString() + " - " + cluster.Points.Count.ToString() + " : ");
                //    foreach (var point in cluster.Points){
                //       Console.Write(point.Id.ToString() + ", ");
                //    }
                //    Console.WriteLine();
                //}

                Console.WriteLine("SSE with " + i.ToString() + " centroid(s): " + SSE.CalculateSSE(clusteredResult).ToString());
            }
        }
Пример #2
0
        public List <Cluster> mainLoop(int centroidCount, int iterations, List <Vector> data)
        {
            List <Cluster> clusters       = Statics.InitCluster(centroidCount, Statics.InitCentroidsMark(centroidCount, data));//Statics.InitCentroids(centroidCount, data[0].Coordinates.Count));
            List <Cluster> resultClusters = new List <Cluster>();

            int currentIterations = 0;

            while (currentIterations != iterations)
            {
                List <Cluster> clusters = Statics.InitCluster(centroidCount, Statics.InitCentroids(centroidCount, data[0].Coordinates.Count));//Statics.InitCentroidsMark(centroidCount, data));

                while (true)
                {
                    List <List <double> > oldCoordinates = new List <List <double> >();
                    foreach (Cluster cluster in clusters)
                    {
                        oldCoordinates.Add(cluster.Centroid.Coordinates);
                    }
                    step(data, clusters);
                    if (clusters.Select(c => c.Centroid.Coordinates).ToList().SequenceEqual(oldCoordinates))
                    {
                        if (resultClusters.Count == 0)
                        {
                            resultClusters = clusters;
                        }
                        else
                        {
                            if (SSE.CalculateSSE(resultClusters) > SSE.CalculateSSE(clusters))
                            {
                                resultClusters = clusters;
                            }
                        }
                        break;
                    }
                }
                currentIterations++;
            }

            return(resultClusters);
        }