Example #1
0
        public void binary_split_new_method()
        {
            #region doc_sample1
            // Use a fixed seed for reproducibility
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some data to be clustered
            double[][] input = 
            {
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            // Create a new binary split with 3 clusters 
            BinarySplit binarySplit = new BinarySplit(3);

            // Learn a data partitioning using the Binary Split algorithm
            KMeansClusterCollection clustering = binarySplit.Learn(input);

            // Predict group labels for each point
            int[] output = clustering.Decide(input);

            // As a result, the first two observations should belong to the
            //  same cluster (thus having the same label). The same should
            //  happen to the next four observations and to the last three.
            #endregion

            Assert.AreEqual(output[0], output[1]);

            Assert.AreEqual(output[2], output[3]);
            Assert.AreEqual(output[2], output[4]);
            Assert.AreEqual(output[2], output[5]);

            Assert.AreEqual(output[6], output[7]);
            Assert.AreEqual(output[6], output[8]);

            Assert.AreNotEqual(output[0], output[2]);
            Assert.AreNotEqual(output[2], output[6]);
            Assert.AreNotEqual(output[0], output[6]);

            int[] labels2 = binarySplit.Clusters.Nearest(input);

            Assert.IsTrue(output.IsEqual(labels2));
        }
Example #2
0
        private static void binarySplit(double[][] inputs)
        {
            // Create a binary-split algorithm
            var binarySplit = new BinarySplit(k: 3)
            {
                Distance      = new SquareEuclidean(),
                MaxIterations = 1000
            };

            // Use it to learn a data model
            var model = binarySplit.Learn(inputs);

            // Use the model to group new instances
            int[] prediction = model.Decide(inputs);

            // Plot the results
            ScatterplotBox.Show("Binary Split's answer", inputs, prediction).Hold();
        }
        public void binary_split_information_test()
        {
            // Use a fixed seed for reproducibility
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some data to be clustered
            double[][] input =
            {
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            // Create a new binary split with 3 clusters
            BinarySplit binarySplit = new BinarySplit(3)
            {
                ComputeProportions = true,
                ComputeCovariances = true,
                ComputeError       = true,
            };

            // Learn a data partitioning using the Binary Split algorithm
            KMeansClusterCollection clustering = binarySplit.Learn(input);

            string str = clustering.Proportions.ToCSharp();

            double[] expectedProportions = new double[] { 0.333333333333333, 0.444444444444444, 0.222222222222222 };
            Assert.IsTrue(expectedProportions.IsEqual(clustering.Proportions, 1e-10));

            var strs = clustering.Covariances.Apply(x => x.ToCSharp());

            double[][][] expectedCovar =
            {
                new double[][]
                {
                    new double[] {                   7,                  0,                   1 },
                    new double[] {                   0,                  0,                   0 },
                    new double[] {                   1,                  0,    1.33333333333333 }
                },
                new double[][]
                {
                    new double[] {   0.916666666666667,              -0.25, -0.0833333333333333 },
                    new double[] {               -0.25,               0.25,  0.0833333333333333 },
                    new double[] { -0.0833333333333333, 0.0833333333333333,                0.25 }
                },
                new double[][]
                {
                    new double[] {                   0,                  0,                   0 },
                    new double[] {                   0,                4.5,                 7.5 },
                    new double[] {                   0,                7.5,                12.5 }
                }
            };

            Assert.IsTrue(expectedCovar.IsEqual(clustering.Covariances, 1e-10));
        }