Exemplo n.º 1
0
        public static Dictionary <int, GaussianClusterCollection> GetIndexClustersMap(List <double[]> continuousData, IDictionary <int, int> continuesIndexKMap)
        {
            Dictionary <int, GaussianClusterCollection> indexClusterMap = new Dictionary <int, GaussianClusterCollection>();

            Accord.Math.Random.Generator.Seed = 0;

            foreach (int i in continuesIndexKMap.Keys)
            {
                // Prepare data to be analyzed in the KMeans algorithm
                double[][] data = new double[continuousData.Count][];
                for (int j = 0; j < continuousData.Count; j++)
                {
                    data[j] = new[] { continuousData[j][i] };
                }

                // Grab k from optimum map.
                int    k      = continuesIndexKMap[i];
                KMeans kMeans = new KMeans(k);

                var cluster = kMeans.Learn(data);

                // Learn gaussian  mixture model based on k-model
                GaussianMixtureModel gaussianMixtureModel = new GaussianMixtureModel(kMeans);
                indexClusterMap[i] = gaussianMixtureModel.Learn(data);
            }

            return(indexClusterMap);
        }
Exemplo n.º 2
0
        public void GaussianMixtureModelExample()
        {
            // Test Samples
            double[][] samples =
            {
                new double[] { 0, 1 },
                new double[] { 1, 2 },
                new double[] { 1, 1 },
                new double[] { 0, 7 },
                new double[] { 1, 1 },
                new double[] { 6, 2 },
                new double[] { 6, 5 },
                new double[] { 5, 1 },
                new double[] { 7, 1 },
                new double[] { 5, 1 }
            };

            double[] sample = samples[0];


            // Create a new Gaussian mixture with 2 components
            var gmm = new GaussianMixtureModel(components: 2);

            // Compute the model (estimate)
            var clusters = gmm.Learn(samples);

            double error = gmm.LogLikelihood;

            // Classify a single sample
            int c0 = clusters.Decide(samples[0]);
            int c1 = clusters.Decide(samples[1]);

            int c7 = clusters.Decide(samples[7]);
            int c8 = clusters.Decide(samples[8]);

            Assert.AreEqual(c0, c1);
            Assert.AreEqual(c7, c8);
            Assert.AreNotEqual(c0, c8);


            // Extract the multivariate Normal distribution from it
            MultivariateMixture <MultivariateNormalDistribution> mixture =
                gmm.ToMixtureDistribution();

            Assert.AreEqual(2, mixture.Dimension);
            Assert.AreEqual(2, mixture.Components.Length);
            Assert.AreEqual(2, mixture.Coefficients.Length);
        }
Exemplo n.º 3
0
        private static void gmm(double[][] inputs)
        {
            var gmm = new GaussianMixtureModel(components: 3)
            {
                Tolerance = 1e-6
            };

            var model = gmm.Learn(inputs);

            int[] prediction = model.Decide(inputs);

            double[][] prob = model.Probabilities(inputs);

            // Compute the clustering distortion
            double distortion = model.Distortion(inputs, prediction);

            // Plot the results
            ScatterplotBox.Show("Free GMM's answer", inputs, prediction)
            .Hold();
        }
        /// <summary>
        ///   Estimates Gaussian distributions from the data.
        /// </summary>
        ///
        private void btnCompute_Click(object sender, EventArgs e)
        {
            // Create a new Gaussian Mixture Model
            var gmm = new GaussianMixtureModel(k);

            // If available, initialize with k-means
            if (kmeans != null)
            {
                gmm.Initialize(kmeans);
            }

            // Compute the model
            GaussianClusterCollection clustering = gmm.Learn(observations);

            // Classify all instances in mixture data
            int[] classifications = clustering.Decide(observations);

            // Draw the classifications
            updateGraph(classifications);
        }
Exemplo n.º 5
0
        private static void gmmSharedCovariances(double[][] inputs)
        {
            var gmm = new GaussianMixtureModel(components: 3)
            {
                Tolerance = 1e-6,
                Options   = new NormalOptions()
                {
                    Shared = true
                }
            };

            var model = gmm.Learn(inputs);

            int[] prediction = model.Decide(inputs);

            // Compute the clustering distortion
            double distortion = model.Distortion(inputs, prediction);

            // Plot the results
            ScatterplotBox.Show("Shared GMM's answer", inputs, prediction)
            .Hold();
        }
        public void GaussianMixtureModelTest4()
        {

            // Suppose we have a weighted data set. Those are the input points:
            double[][] points =
            {
                new double[] { 0 }, new double[] { 3 }, new double[] {  1 }, 
                new double[] { 7 }, new double[] { 3 }, new double[] {  5 },
                new double[] { 1 }, new double[] { 2 }, new double[] { -1 },
                new double[] { 2 }, new double[] { 7 }, new double[] {  6 }, 
                new double[] { 8 }, new double[] { 6 } // (14 points)
            };

            // And those are their respective unnormalized weights:
            double[] weights = { 10, 1, 1, 2, 2, 1, 1, 1, 8, 1, 2, 5, 1, 1 }; // (14 weights)

            // with weights
            {
                Accord.Math.Tools.SetupGenerator(0);

                // If we need the GaussianMixtureModel functionality, we can
                // use the estimated mixture to initialize a new model:
                GaussianMixtureModel gmm = new GaussianMixtureModel(2);
                gmm.Initializations = 1;

                var options = new GaussianMixtureModelOptions();
                options.Weights = weights;
                options.ParallelOptions.MaxDegreeOfParallelism = 1;
                gmm.Compute(points, options);


                int a = 1;
                int b = 0;
                double tol = 1e-6;

                if (!gmm.Gaussians[a].Mean[0].IsRelativelyEqual(6.41922, 1e-4))
                {
                    var t = a;
                    a = b;
                    b = t;
                }

                Assert.AreEqual(6.4192285647145395, gmm.Gaussians[a].Mean[0], tol);
                Assert.AreEqual(0.2888226129013588, gmm.Gaussians[b].Mean[0], tol);

                Assert.AreEqual(0.32321638614859777, gmm.Gaussians[a].Proportion, tol);
                Assert.AreEqual(0.67678361385140218, gmm.Gaussians[b].Proportion, tol);
                Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion);
            }

            // with weights, new style
            {
                Accord.Math.Tools.SetupGenerator(0);

                // If we need the GaussianMixtureModel functionality, we can
                // use the estimated mixture to initialize a new model:
                GaussianMixtureModel gmm = new GaussianMixtureModel(2);
                gmm.Initializations = 1;

                gmm.UseLogarithm = false;
                gmm.ParallelOptions.MaxDegreeOfParallelism = 1;
                gmm.Learn(points, weights);


                int a = 1;
                int b = 0;
                double tol = 1e-6;

                if (!gmm.Gaussians[a].Mean[0].IsRelativelyEqual(6.41922, 1e-4))
                {
                    var t = a;
                    a = b;
                    b = t;
                }

                Assert.AreEqual(6.4192285647145395, gmm.Gaussians[a].Mean[0], tol);
                Assert.AreEqual(0.2888226129013588, gmm.Gaussians[b].Mean[0], tol);

                Assert.AreEqual(0.32321638614859777, gmm.Gaussians[a].Proportion, tol);
                Assert.AreEqual(0.67678361385140218, gmm.Gaussians[b].Proportion, tol);
                Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion);
            }

            // without weights
            {
                Accord.Math.Random.Generator.Seed = 0;

                // If we need the GaussianMixtureModel functionality, we can
                // use the estimated mixture to initialize a new model:
                GaussianMixtureModel gmm = new GaussianMixtureModel(2);
                gmm.Initializations = 1;

                var options = new GaussianMixtureModelOptions();
                options.ParallelOptions.MaxDegreeOfParallelism = 1;
                gmm.Compute(points, options);

                int a = 1;
                int b = 0;

                double tol = 1e-2;

                if (!6.5149525060859848.IsRelativelyEqual(gmm.Gaussians[a].Mean[0], tol))
                {
                    var t = a;
                    a = b;
                    b = t;
                }

                Assert.AreEqual(6.5149525060859848, gmm.Gaussians[a].Mean[0], tol);
                Assert.AreEqual(1.4191977895308987, gmm.Gaussians[b].Mean[0], tol);

                Assert.AreEqual(0.4195042394315267, gmm.Gaussians[a].Proportion, tol);
                Assert.AreEqual(0.58049576056847307, gmm.Gaussians[b].Proportion, tol);
                Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion, 1e-8);
            }
        }
Exemplo n.º 7
0
        public void GaussianMixtureModelTest4()
        {
            // Suppose we have a weighted data set. Those are the input points:
            double[][] points =
            {
                new double[] { 0 }, new double[] { 3 }, new double[] {  1 },
                new double[] { 7 }, new double[] { 3 }, new double[] {  5 },
                new double[] { 1 }, new double[] { 2 }, new double[] { -1 },
                new double[] { 2 }, new double[] { 7 }, new double[] {  6 },
                new double[] { 8 }, new double[] { 6 } // (14 points)
            };

            // And those are their respective unnormalized weights:
            double[] weights = { 10, 1, 1, 2, 2, 1, 1, 1, 8, 1, 2, 5, 1, 1 }; // (14 weights)

            // with weights
            {
                Accord.Math.Tools.SetupGenerator(0);

                // If we need the GaussianMixtureModel functionality, we can
                // use the estimated mixture to initialize a new model:
                GaussianMixtureModel gmm = new GaussianMixtureModel(2);
                gmm.Initializations = 1;

                var options = new GaussianMixtureModelOptions();
                options.Weights = weights;
                options.ParallelOptions.MaxDegreeOfParallelism = 1;
                gmm.Compute(points, options);


                int    a   = 1;
                int    b   = 0;
                double tol = 1e-6;

                if (!gmm.Gaussians[a].Mean[0].IsRelativelyEqual(6.41922, 1e-4))
                {
                    var t = a;
                    a = b;
                    b = t;
                }

                Assert.AreEqual(6.4192285647145395, gmm.Gaussians[a].Mean[0], tol);
                Assert.AreEqual(0.2888226129013588, gmm.Gaussians[b].Mean[0], tol);

                Assert.AreEqual(0.32321638614859777, gmm.Gaussians[a].Proportion, tol);
                Assert.AreEqual(0.67678361385140218, gmm.Gaussians[b].Proportion, tol);
                Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion);
            }

            // with weights, new style
            {
                Accord.Math.Tools.SetupGenerator(0);

                // If we need the GaussianMixtureModel functionality, we can
                // use the estimated mixture to initialize a new model:
                GaussianMixtureModel gmm = new GaussianMixtureModel(2);
                gmm.Initializations = 1;

                gmm.UseLogarithm = false;
                gmm.ParallelOptions.MaxDegreeOfParallelism = 1;
                gmm.Learn(points, weights);


                int    a   = 1;
                int    b   = 0;
                double tol = 1e-6;

                if (!gmm.Gaussians[a].Mean[0].IsRelativelyEqual(6.41922, 1e-4))
                {
                    var t = a;
                    a = b;
                    b = t;
                }

                Assert.AreEqual(6.4192285647145395, gmm.Gaussians[a].Mean[0], tol);
                Assert.AreEqual(0.2888226129013588, gmm.Gaussians[b].Mean[0], tol);

                Assert.AreEqual(0.32321638614859777, gmm.Gaussians[a].Proportion, tol);
                Assert.AreEqual(0.67678361385140218, gmm.Gaussians[b].Proportion, tol);
                Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion);
            }

            // without weights
            {
                Accord.Math.Random.Generator.Seed = 0;

                // If we need the GaussianMixtureModel functionality, we can
                // use the estimated mixture to initialize a new model:
                GaussianMixtureModel gmm = new GaussianMixtureModel(2);
                gmm.Initializations = 1;

                var options = new GaussianMixtureModelOptions();
                options.ParallelOptions.MaxDegreeOfParallelism = 1;
                gmm.Compute(points, options);

                int a = 1;
                int b = 0;

                double tol = 1e-2;

                if (!6.5149525060859848.IsRelativelyEqual(gmm.Gaussians[a].Mean[0], tol))
                {
                    var t = a;
                    a = b;
                    b = t;
                }

                Assert.AreEqual(6.5149525060859848, gmm.Gaussians[a].Mean[0], tol);
                Assert.AreEqual(1.4191977895308987, gmm.Gaussians[b].Mean[0], tol);

                Assert.AreEqual(0.4195042394315267, gmm.Gaussians[a].Proportion, tol);
                Assert.AreEqual(0.58049576056847307, gmm.Gaussians[b].Proportion, tol);
                Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion, 1e-8);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Generates both training set consisting of feasible and infeasible examples
        /// </summary>
        public void GenerateTrainingData()
        {
            Feasibles = GenerateFeasibleExamples(GlobalVariables.FeasibleExamplesCount);

            // Fill training data
            TrainingData = Feasibles.AddColumnWithValues(1.0);

            // Fill output List
            var tempArr = new double[TrainingData.Count];

            Output.AddRange(tempArr.Select(x => 1));

            _dal.TrainingFeasibleExamples = Feasibles.ToArray();

            GaussianMixtureModel gmm = new GaussianMixtureModel(GlobalVariables.Components);

            gmm = new GaussianMixtureModel(GlobalVariables.Components)
            {
                Initializations = 100,
                MaxIterations   = 10000,
                ParallelOptions = new System.Threading.Tasks.ParallelOptions()
                {
                    MaxDegreeOfParallelism = 1
                },
                Tolerance = 10E-11,
                Options   = new Accord.Statistics.Distributions.Fitting.NormalOptions()
                {
                    Regularization = double.Epsilon
                }
            };

            // Estimate the Gaussian Mixture
            gmm.Learn(_dal.TrainingFeasibleExamples);
            var iterations   = gmm.Iterations;
            var distribution = gmm.ToMixtureDistribution();

            // Get minimal probability of probability density function from distribution (percentile 0)
            var minimalProbability = _dal.TrainingFeasibleExamples
                                     .Select(item => distribution.ProbabilityDensityFunction(item))
                                     .Min();

            // Rescale data range for infeasible example creation
            NewBoundries = BoundryRescaler.Rescale(Feasibles);

            // Generate infeasible examples
            var infeasibles = new List <double[]>();

            while (infeasibles.Count < GlobalVariables.InfeasibleExamplesCount)
            {
                // Generate points within new boundry
                var x = GenerateLimitedInputs(GlobalVariables.Dimensions, NewBoundries);

                // Calculate probability density function value for given input
                var probability = distribution.ProbabilityDensityFunction(x);

                // Check if the value is smaller than smallest probability of all feasible examples
                if (probability > minimalProbability)
                {
                    continue;
                }

                infeasibles.Add(x);

                TrainingData.Add(x.ExtendArrayWithValue(0.0));

                Output.Add(0);
            }

            _dal.TrainingInfeasibleExamples = infeasibles.ToArray();
            _dal.TrainingData = TrainingData.ToArray();
        }
Exemplo n.º 9
0
        /// <summary>
        ///   Estimates Gaussian distributions from the data.
        /// </summary>
        /// 
        private void btnCompute_Click(object sender, EventArgs e)
        {
            // Create a new Gaussian Mixture Model
            var gmm = new GaussianMixtureModel(k);

            // If available, initialize with k-means
            if (kmeans != null) 
                gmm.Initialize(kmeans);

            // Compute the model
            GaussianClusterCollection clustering = gmm.Learn(observations);

            // Classify all instances in mixture data
            int[] classifications = clustering.Decide(observations);

            // Draw the classifications
            updateGraph(classifications);
        }