Ejemplo n.º 1
0
        public void Assign_Clusters()
        {
            var o = new ClusterAlgorithmOptions(2);

            ClusterData cd = new ClusterData(new double[3][] { new double[2], new double[2], new double[2] }, new string[2]);

            ClusterResult cr = new ClusterResult();

            cr.ClusterAssignment = new int[3] {
                0, 1, 0
            };
            cr.ClusterMeanValues    = new double[2][];
            cr.ClusterMeanValues[0] = new double[2] {
                100, 1000
            };
            cr.ClusterMeanValues[1] = new double[2] {
                5, 10
            };
            cr.NormalizedData    = new double[3][];
            cr.NormalizedData[0] = new double[2] {
                80, 900
            };
            cr.NormalizedData[1] = new double[2] {
                200, 2000
            };
            cr.NormalizedData[2] = new double[2] {
                7, 20
            };
            KmeansClusterAlgorithm.UpdateClusterAssignment(cr, cd, o);
            Assert.AreEqual(cr.ClusterAssignment[0], 0);
            Assert.AreEqual(cr.ClusterAssignment[1], 0);
            Assert.AreEqual(cr.ClusterAssignment[2], 1);
        }
Ejemplo n.º 2
0
        public void Initialize_Clusters()
        {
            var x = new double[10][];

            for (int i = 0; i < x.Length; i++)
            {
                x[i] = new double[5];
            }
            ClusterData             d       = new ClusterData(x, new string[] { "a1", "a2", "a3", "a4", "a5" });
            ClusterAlgorithmOptions options = new ClusterAlgorithmOptions(3, 123, true, 10);
            //should provide initialized mean values and assignment

            ClusterResult r = KmeansClusterAlgorithm.InitializeClusters(d, options);

            int[] count = new int[3];
            for (int i = 0; i < d.RawData.Length; i++)
            {
                int idx = r.ClusterAssignment[i];
                count[idx] += 1;
            }
            for (int i = 0; i < options.NumberOfClusters; i++)
            {
                //check if every cluster received at least one data point
                Assert.IsTrue(count[i] > 0);
                //check if the column mean values are initialized
                Assert.IsTrue(r.ClusterMeanValues[i].Length == d.RawData[0].Length);
            }
        }
Ejemplo n.º 3
0
        public void Can_Find_Minimum()
        {
            double[] data          = new double[] { -5, 10, 3, 100, -6.01, -6.02, 7, -3 };
            int      expectedIndex = 5;
            int      foundIndex    = KmeansClusterAlgorithm.FindMinimumIndex(data);

            Assert.AreEqual(expectedIndex, foundIndex);
        }
Ejemplo n.º 4
0
        public void TestClusterAlgorithmOnExample()
        {
            ClusterData             data    = new ClusterData(GetTestData(), new string[] { "X", "Y" });
            ClusterAlgorithmOptions options = new ClusterAlgorithmOptions(5);

            var result = KmeansClusterAlgorithm.Analyze(data, options);

            Assert.IsTrue(result.ClusterAssignment.Length > 0);
            Trace.WriteLine($"Iterations: {result.Iterations}");
            Trace.WriteLine(result.PrintCsvResult());
        }
Ejemplo n.º 5
0
        public void Distance_Calculation_Euclidean()
        {
            double[] means   = new double[] { 3, 4, 5 };
            double[] data    = new double[] { 10, 10, 10 };
            double[] weights = new double[] { 1, 1, 1 };
            double   result  = 0;

            for (int i = 0; i < means.Length; i++)
            {
                result = result + weights[i] * Math.Pow(means[i] - data[i], 2);
            }
            result = Math.Sqrt(result);

            Assert.AreEqual(result, KmeansClusterAlgorithm.GetDistance(means, data, weights));
        }
Ejemplo n.º 6
0
        public void TestBigSet()
        {
            int cols = 5;
            int rows = 5000;

            string[] attributes = new string[cols];
            for (int i = 0; i < cols; i++)
            {
                attributes[i] = "Col_" + i.ToString();
            }

            ClusterAlgorithmOptions options = new ClusterAlgorithmOptions(3, true);
            ClusterData             data    = new ClusterData(GetRandomDataSet(rows, cols, 2000), attributes);

            ClusterResult result = KmeansClusterAlgorithm.Analyze(data, options);

            Assert.IsTrue(result.Iterations > 0);
            Trace.WriteLine($"Iterations: {result.Iterations}");
            Trace.WriteLine(result.PrintCsvResult());
        }
Ejemplo n.º 7
0
        public void Calculate_Means()
        {
            double[][] data = new double[5][];

            //1. cluster
            data[0] = new double[] { 2, 7 };
            data[1] = new double[] { 3, 3 };
            //2. cluster
            data[2] = new double[] { 1, 100 };
            data[3] = new double[] { 2, 200 };
            data[4] = new double[] { 3, 300 };


            ClusterData             cd      = new ClusterData(data, new string[] { "a1", "a2" });
            ClusterAlgorithmOptions options = new ClusterAlgorithmOptions(2, false);

            ClusterResult r = new ClusterResult();

            r.ClusterAssignment    = new int[] { 0, 0, 1, 1, 1 };
            r.NormalizedData       = data;
            r.ClusterMeanValues    = new double[2][];
            r.ClusterMeanValues[0] = new double[2] {
                0, 0
            };
            r.ClusterMeanValues[1] = new double[2] {
                0, 0
            };

            KmeansClusterAlgorithm.UpdateClusterMeanValues(r, cd, options);

            double exp_First  = 2.5;
            double exp_Second = 5.0;

            Assert.AreEqual(exp_First, r.ClusterMeanValues[0][0]);
            Assert.AreEqual(exp_Second, r.ClusterMeanValues[0][1]);
            exp_First  = 2d;
            exp_Second = 200d;
            Assert.AreEqual(exp_First, r.ClusterMeanValues[1][0]);
            Assert.AreEqual(exp_Second, r.ClusterMeanValues[1][1]);
        }
Ejemplo n.º 8
0
        private async void ExportButton_Click(object sender, RoutedEventArgs e)
        {
            ClusterButton.IsEnabled = false;
            ClusterButton.IsEnabled = false;
            LoadCsvButton.IsEnabled = false;
            BusyIndication.IsActive = true;

            int clusters = (int)(ClusterCountRange.UpperValue);

            if (MessageBox.Show($"Run with {clusters} Clusters?", "Get Results", MessageBoxButton.YesNo, MessageBoxImage.Question)
                == MessageBoxResult.Yes)
            {
                int  runs      = (int)(RandomRuns.Value.Value);
                bool normalize = NormalizeSwitch.IsChecked.Value;

                var analysis = await Task.Run(() => ClusterSolver.Cluster(clusterData, clusters, clusters, runs, normalize));

                var seed    = analysis[0].BestSeed;
                var options = new ClusterAlgorithmOptions(clusters, seed, normalize);
                var result  = await Task.Run(() => KmeansClusterAlgorithm.Analyze(clusterData, options));


                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Csv files (*.csv)|*.csv|Text files (*.txt)|*.txt";

                if (saveFileDialog.ShowDialog() == true)
                {
                    using (var writer = new StreamWriter(File.Create(saveFileDialog.FileName)))
                    {
                        string csv = result.PrintCsvResult();
                        writer.Write(csv);
                    }
                }
            }
            ClusterButton.IsEnabled = true;
            ClusterButton.IsEnabled = true;
            LoadCsvButton.IsEnabled = true;
            BusyIndication.IsActive = false;
        }