Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            List <Vector> vectors = new List <Vector>();

            //Read WineData.csv containing 32 rows and 100 columns
            var lines = File.ReadAllLines("WineData.csv").Select(
                line => line.Split(',').Select(float.Parse).ToList())
                        .ToList();

            //Loop through all rows and columns
            for (var i = 0; i < lines.Count; i++)
            {
                for (var j = 0; j < lines[i].Count; j++)
                {
                    if (vectors.ElementAtOrDefault(j) == null)
                    {
                        vectors.Add(new Vector());
                    }
                    //Transpose the dataset, into a list of 100 vectors containing 32 points
                    vectors[j].AddPoint(lines[i][j]);
                }
            }

            //Get the users' input
            Console.Write("How many iterations would you like to make: ");
            var iterations = int.Parse(Console.ReadLine());

            Console.Write("How many clusters would you like to create: ");
            var clusters = int.Parse(Console.ReadLine());

            while (clusters > vectors.Count())
            {
                Console.Write("You can't create more clusters than datapoints, please select a value lower than " + vectors.Count() + ": ");
                clusters = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("*******************************************");

            //Get the best value for clusters (amount) by minimizing the SSE
            double[] SSEGroup = new double[10];
            for (var i = 1; i < 11; i++)
            {
                var KMeansAlgorithmDebug = new KMeansAlgorithm(iterations, i, vectors, true);
                KMeansAlgorithmDebug.MainLoop();
                SSEGroup[i - 1] = KMeansAlgorithmDebug.SSE1;
            }
            Console.WriteLine("Best value for k (amount of clusters) after minimizing the SSE for k = 1 through 10:");
            Console.WriteLine("k = " + Convert.ToInt32(Array.IndexOf(SSEGroup, SSEGroup.Min()) + 1) + ": " + SSEGroup.Min());
            Console.WriteLine("Increasing k will almost always minimize the SSE, so it makes sense that k is high");
            Console.WriteLine("*******************************************");

            //Run the main K-Means Algorithm Loop
            var KMeansAlgorithm = new KMeansAlgorithm(iterations, clusters, vectors, false);

            KMeansAlgorithm.MainLoop();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //const int MAX = 1000;
            int numClusters = 0;
            //int[] clustering = new int[MAX];
            string fileName = "..\\..\\..\\..\\Datasets\\normal\\normal.txt";

            double[][] normalData = DataSetManager.ParseDataToDouble(fileName, 2, "\t");

            Console.WriteLine("========= Data Clustering of file normal.txt");
            //KMeansAlgorithm.ShowData(normalData, 1, true, true);
            Console.WriteLine("Enter number of clusters: ");
            numClusters = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("\nSetting numClusters to " + numClusters);

            int[] clustering = KMeansAlgorithm.Cluster(normalData, numClusters); // this is it

            Console.WriteLine("\nK-means clustering complete\n");

            Console.WriteLine("Raw data by cluster:\n");
            KMeansAlgorithm.ShowClustered(normalData, clustering, numClusters, 1);

            //==================== the second file ===============================

            fileName = "..\\..\\..\\..\\Datasets\\unbalance\\unbalance.txt";
            double[][] unbalanceData = DataSetManager.ParseDataToDouble(fileName, 2, " ");

            Console.WriteLine("========= Data Clustering of file unbalance.txt");
            //KMeansAlgorithm.ShowData(normalData, 1, true, true);
            Console.WriteLine("Enter number of clusters: ");
            numClusters = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("\nSetting numClusters to " + numClusters);

            clustering = KMeansAlgorithm.Cluster(unbalanceData, numClusters); // this is it

            Console.WriteLine("\nK-means clustering complete\n");

            Console.WriteLine("Raw data by cluster:\n");
            KMeansAlgorithm.ShowClustered(unbalanceData, clustering, numClusters, 1);

            Console.ReadLine();
        }