public void GaussianMixtureModelTest7() { double[][] values = { new double[] { 0 }, new double[] { 1 }, new double[] { 2 }, new double[] { 3 }, new double[] { 4 }, new double[] { 5 }, new double[] { 6 }, new double[] { 7 }, new double[] { 8 }, new double[] { 9 }, new double[] { 10 }, new double[] { 11 }, new double[] { 12 } }; double[] weights = { 1, 3, 5, 4, 3, 5, 10, 17, 12, 6, 3, 1, 0 }; GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Compute(values, new GaussianMixtureModelOptions() { Weights = weights }); int[] classifications = gmm.Gaussians.Nearest(values); }
public void GaussianMixtureModelTest5() { Accord.Math.Tools.SetupGenerator(0); MemoryStream stream = new MemoryStream(Resources.CircleWithWeights); ExcelReader reader = new ExcelReader(stream, xlsx: false); DataTable table = reader.GetWorksheet("Sheet1"); double[,] matrix = table.ToMatrix(); double[][] points = matrix.Submatrix(null, 0, 1).ToArray(); double[] weights = matrix.GetColumn(2); GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Compute(points, new GaussianMixtureModelOptions() { Weights = weights }); Assert.AreEqual(-0.010550720353814949, gmm.Gaussians[0].Mean[0]); Assert.AreEqual(0.40799698773355553, gmm.Gaussians[0].Mean[1]); Assert.AreEqual(0.011896812071918696, gmm.Gaussians[1].Mean[0]); Assert.AreEqual(-0.40400708592859663, gmm.Gaussians[1].Mean[1]); }
public void GaussianMixtureModelConstructorTest() { Accord.Math.Tools.SetupGenerator(0); // 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 Model with 2 components GaussianMixtureModel gmm = new GaussianMixtureModel(2); // Compute the model (estimate) gmm.Compute(samples, 0.0001); // Classify a single sample int c = gmm.Gaussians.Nearest(sample); Assert.AreEqual(2, gmm.Gaussians.Count); int first = 0; for (int i = 0; i < samples.Length; i++) { sample = samples[i]; c = gmm.Gaussians.Nearest(sample); if (i == 0) { first = c; } if (i < 5) { Assert.AreEqual(c, first); } else { Assert.AreNotEqual(c, first); } } }
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 unormalized 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.Compute(points, new GaussianMixtureModelOptions() { Weights = weights }); Assert.AreEqual(6.420790676635443, gmm.Gaussians[0].Mean[0]); Assert.AreEqual(0.290536871335858, gmm.Gaussians[1].Mean[0]); Assert.AreEqual(0.32294476897888613, gmm.Gaussians[0].Proportion); Assert.AreEqual(0.67705523102111387, gmm.Gaussians[1].Proportion); } // without 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.Compute(points); Assert.AreEqual(6.5149525060859865, gmm.Gaussians[0].Mean[0]); Assert.AreEqual(1.4191977895308987, gmm.Gaussians[1].Mean[0]); Assert.AreEqual(0.42235760973845654, gmm.Gaussians[0].Proportion); Assert.AreEqual(0.57764239026154351, gmm.Gaussians[1].Proportion); } }
public void GaussianMixtureModelConstructorTest() { Accord.Math.Tools.SetupGenerator(0); // 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 Model with 2 components GaussianMixtureModel gmm = new GaussianMixtureModel(2); // Compute the model (estimate) gmm.Compute(samples, 0.0001); // Classify a single sample int c = gmm.Gaussians.Nearest(sample); Assert.AreEqual(2, gmm.Gaussians.Count); int first = 0; for (int i = 0; i < samples.Length; i++) { sample = samples[i]; c = gmm.Gaussians.Nearest(sample); if (i == 0) first = c; if (i < 5) { Assert.AreEqual(c, first); } else { Assert.AreNotEqual(c, first); } } }
public void GaussianMixtureModelTest2() { Accord.Math.Tools.SetupGenerator(0); int height = 16; int width = 16; var gmm = new GaussianMixtureModel(3); // gmm.Regularization = 0; Assert.AreEqual(3, gmm.Gaussians.Count); Assert.IsNull(gmm.Gaussians[0].Covariance); Assert.IsNull(gmm.Gaussians[0].Mean); double[][][] A = new double[3][][]; A[0] = new double[height][]; A[1] = new double[height][]; A[2] = new double[height][]; for (int j = 0; j < height; j++) { A[0][j] = new double[width]; A[1][j] = new double[width]; A[2][j] = new double[width]; for (int k = 0; k < width; k++) { A[0][j][k] = 102; A[1][j][k] = 57; A[2][j][k] = 200; } } double[][] B = Matrix.Stack(A); bool thrown = false; try { var result = gmm.Compute(B); } catch (NonPositiveDefiniteMatrixException) { thrown = true; } Assert.IsTrue(thrown); }
public void GaussianMixtureModelConstructorTest() { Accord.Math.Tools.SetupGenerator(0); // 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 Model with 2 components GaussianMixtureModel gmm = new GaussianMixtureModel(2); // Compute the model (estimate) double ll = gmm.Compute(samples, 0.0001); Assert.AreEqual(-35.930732550698494, ll, 1e-10); Assert.AreEqual(2, gmm.Gaussians.Count); Assert.IsTrue(gmm.Gaussians.Means[0].IsEqual(new[] { 5.8, 2.0 }, 1e-3)); Assert.IsTrue(gmm.Gaussians.Means[1].IsEqual(new[] { 0.6, 2.4 }, 1e-3)); int[] c = samples.Apply(gmm.Clusters.Decide); for (int i = 0; i < samples.Length; i++) { double[] responses; int e; responses = gmm.Gaussians.Probabilities(samples[i], out e); int a = responses.ArgMax(); Assert.AreEqual(a, e); Assert.AreEqual(c[i], (i < 5) ? 1 : 0); } }
public void GaussianMixtureModelTest3() { Accord.Math.Tools.SetupGenerator(0); var gmm = new GaussianMixtureModel(3); Assert.AreEqual(3, gmm.Gaussians.Count); Assert.IsNull(gmm.Gaussians[0].Covariance); Assert.IsNull(gmm.Gaussians[0].Mean); double[][] B = Matrix.Random(56, 12).ToArray(); double result = gmm.Compute(B); }
internal void Train() { logLike = new double[maxK]; // Log-likelihood estimate for a given model and training set rissanen = new double[maxK]; // Rissanen estimate for a given model and training set mdl = new double[maxK]; // Minimum description length for a given model and training set kVar = new bool[maxK]; for (int i = 0; i < maxK; ++i) { // Step 1: Make a GMM with K subclasses gmm = new GaussianMixtureModel(kVals[i]); // Step 2: fit the gmm to the projection data logLike[i] = gmm.Compute(currentProjection, 1e-3, 1.0); // Step 3: perform a classification to detect spurious classification kVar[i] = (kVals[i] == gmm.Classify(currentProjection).Distinct().ToArray().Length); // Step 4: Calculate the MDL for this K double L = (double)(kVals[i] * 3 - 1); rissanen[i] = 0.5 * L * Math.Log(currentProjection.Length); mdl[i] = -logLike[i] + rissanen[i]; } // Which value of K supported the MDL: int ind = Array.IndexOf(mdl, mdl.Min()); // Find the value of K that supports the lowest mdl and is verified K = kVals[ind]; while (!kVar[ind]) { K = kVals[ind]; ++ind; } // Recreate the gmm with the trained value of K gmm = new GaussianMixtureModel(K); double LL = gmm.Compute(currentProjection, 1e-3, 1.0); // Is this a functional classifier? if (gmm != null) { trained = true; } // Set the STD ellipsoid gmm.SetPValue(pValue); }
public void GaussianMixtureModelConstructorTest() { Accord.Math.Tools.SetupGenerator(0); // 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 Model with 2 components GaussianMixtureModel gmm = new GaussianMixtureModel(2); // Compute the model (estimate) double ll = gmm.Compute(samples, 0.0001); Assert.AreEqual(-35.930732550698494, ll, 1e-10); Assert.AreEqual(2, gmm.Gaussians.Count); Assert.IsTrue(gmm.Gaussians.Means[0].IsEqual(new[] { 5.8, 2.0 }, 1e-3)); Assert.IsTrue(gmm.Gaussians.Means[1].IsEqual(new[] { 0.6, 2.4 }, 1e-3)); int[] c = samples.Apply(gmm.Clusters.Nearest); for (int i = 0; i < samples.Length; i++) { double[] responses; int e = gmm.Gaussians.Nearest(samples[i], out responses); int a = responses.ArgMax(); Assert.AreEqual(a, e); Assert.AreEqual(c[i], (i < 5) ? 1 : 0); } }
public void GaussianMixtureModelTest5() { Accord.Math.Tools.SetupGenerator(0); string path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Resources", "CircleWithWeights.xls"); ExcelReader reader = new ExcelReader(path); DataTable table = reader.GetWorksheet("Sheet1"); double[,] matrix = table.ToMatrix(); double[][] points = matrix.Submatrix(null, 0, 1).ToJagged(); double[] weights = matrix.GetColumn(2); GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Initializations = 1; gmm.ParallelOptions.MaxDegreeOfParallelism = 1; gmm.Compute(points, new GaussianMixtureModelOptions() { Weights = weights }); int a = 0; int b = 1; double tol = 1e-3; if ((-0.407859903454185).IsRelativelyEqual(gmm.Gaussians[1].Mean[0], tol)) { a = 1; b = 0; } Assert.AreEqual(-0.407859903454185, gmm.Gaussians[a].Mean[0], tol); Assert.AreEqual(-0.053911705279706859, gmm.Gaussians[a].Mean[1], tol); Assert.AreEqual(0.39380877640250328, gmm.Gaussians[b].Mean[0], tol); Assert.AreEqual(0.047186154880776772, gmm.Gaussians[b].Mean[1], tol); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion, 1e-15); Assert.IsFalse(gmm.Gaussians[0].Mean.HasNaN()); Assert.IsFalse(gmm.Gaussians[1].Mean.HasNaN()); }
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) double error = gmm.Compute(samples); // Classify a single sample int c0 = gmm.Gaussians.Nearest(samples[0]); int c1 = gmm.Gaussians.Nearest(samples[1]); int c7 = gmm.Gaussians.Nearest(samples[7]); int c8 = gmm.Gaussians.Nearest(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); }
private void btnCompute_Click(object sender, EventArgs e) { // Create a new Gaussian Mixture Model GaussianMixtureModel gmm = new GaussianMixtureModel(k); // If available, initialize with k-means if (kmeans != null) { gmm.Initialize(kmeans); } // Compute the model gmm.Compute(mixture); // Classify all instances in mixture data int[] classifications = gmm.Classify(mixture); // Draw the classifications updateGraph(classifications); }
private void DoFitGMM() { // Create a new Gaussian Mixture Model var gmm = new GaussianMixtureModel(this.k); // If available, initialize with k-means if (this.kmeans != null) { gmm.Initialize(this.kmeans); } // Compute the model gmm.Compute(this.mixture); // Classify all instances in mixture data var classifications = gmm.Gaussians.Nearest(this.mixture); // Draw the classifications this.UpdateGraph(classifications); }
public void buildModel() { if (inputMatrix == null) { getMatrix(); } switch (cType) { case clusterType.KMEANS: KMeans kmeans = new KMeans(k); kmeans.Compute(inputMatrix, precision); clusterCollection = kmeans.Clusters; model = kmeans; break; case clusterType.BINARY: BinarySplit bSplit = new BinarySplit(k); bSplit.Compute(inputMatrix, precision); clusterCollection = bSplit.Clusters; model = bSplit; //Console.WriteLine("BinarySplit"); break; case clusterType.GAUSSIANMIXTURE: GaussianMixtureModel gModel = new GaussianMixtureModel(k); gModel.Compute(inputMatrix, precision); clusterCollection = gModel.Gaussians; model = gModel; break; default: break; } lbl = new List <string>(); for (int i = 0; i < k; i++) { lbl.Add(i.ToString()); } }
public void GaussianMixtureModelTest3() { Accord.Math.Tools.SetupGenerator(0); var gmm = new GaussianMixtureModel(3); Assert.AreEqual(3, gmm.Gaussians.Count); Assert.IsNull(gmm.Gaussians[0].Covariance); Assert.IsNull(gmm.Gaussians[0].Mean); double[][] B = Matrix.Random(56, 12).ToJagged(); Accord.Math.Tools.SetupGenerator(0); gmm.Options.Robust = true; var result = gmm.Compute(B, new GaussianMixtureModelOptions() { NormalOptions = new NormalOptions { Robust = true } }); }
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); } }
public void GaussianMixtureModelTest7() { double[][] values = { new double[] {0}, new double[] {1}, new double[] {2}, new double[] {3}, new double[] {4}, new double[] {5}, new double[] {6}, new double[] {7}, new double[] {8}, new double[] {9}, new double[] {10}, new double[] {11}, new double[] {12} }; double[] weights = { 1, 3, 5, 4, 3, 5, 10, 17, 12, 6, 3, 1, 0 }; GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Compute(values, new GaussianMixtureModelOptions() { Weights = weights }); int[] classifications = gmm.Gaussians.Nearest(values); }
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) double error = gmm.Compute(samples); // Classify a single sample int c0 = gmm.Gaussians.Nearest(samples[0]); int c1 = gmm.Gaussians.Nearest(samples[1]); int c7 = gmm.Gaussians.Nearest(samples[7]); int c8 = gmm.Gaussians.Nearest(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); }
public CalPAC(UISettings ui, string grouping, int n, List <double> AreaPool) { try { //Stopwatch sw = new Stopwatch(); //sw.Start(); Grouping = grouping; N = n; AreaPool.Sort(); // for weight sampling //double px2um2=ui.PixelScale*ui.PixelScale/ui.ResizeRatio/ui.ResizeRatio; var CountDis = new List <double[]>(); double AreaSum = 0.0d; for (int i = 0; i < AreaPool.Count; i++) { double[] data = new double[1]; data[0] = Math.Log10(AreaPool[i] + 1); //data[1] = (double)(mFull[i]/100.0d); CountDis.Add(data); AreaSum += AreaPool[i]; } ///1-Dist fitted with 1-Population GaussianMixtureModel gmm1 = new GaussianMixtureModel(1); gmm1Fit = gmm1.Compute(CountDis.ToArray()); p1Mean = gmm1.Gaussians[0].Mean[0]; p1Covariance = gmm1.Gaussians[0].Covariance[0, 0]; p1Proportion = gmm1.Gaussians[0].Proportion; ///1-Dist fitted with 2-Populations / take the larger proportion //GaussianMixtureModel gmm1=new GaussianMixtureModel(2); //gmm1Fit=gmm1.Compute(countDis.ToArray()); //int m=(gmm1.Gaussians[0].Proportion>gmm1.Gaussians[1].Proportion) ? 0 : 1; //p1Mean=gmm1.Gaussians[m].Mean[0]; //p1Covariance=gmm1.Gaussians[m].Covariance[0, 0]; //p1Proportion=gmm1.Gaussians[m].Proportion; ///List<double[]> areaDis = WeightExpansion(AreaPool, divider=10.0d); //var AreaDis = new List<double[]>(); //double divider = 10.0d; //for (int i = 0; i<AreaPool.Count; i++) { // double[] data = new double[1]; // data[0]=Math.Log10(AreaPool[i]+1); // for (int r = 0; r<Math.Round(AreaPool[i]/divider); r++) { AreaDis.Add(data); } //} ///List<double[]> areaDis = WeightSampling(AreaPool, AreaSum, Math.Max((int)Math.Round(AreaSum/5000.0d), 1)); // sample every divided by 5000 evenly or every one if not too large var AreaDis = new List <double[]>(); int askip = (int)Math.Round(Math.Max(AreaSum / 5000.0d, 1)); for (int sn = 0; sn < (int)Math.Floor(AreaSum); sn += askip) // sample number { double s = 0.0d; for (int bi = 0; bi < AreaPool.Count; bi++) { s += AreaPool[bi]; if (s > sn) { double[] data = new double[1]; data[0] = Math.Log10(AreaPool[bi] + 1); AreaDis.Add(data); break; } } } ///2-Dist fitted with 2-Populations / GaussianMixtureModel gmm2 = new GaussianMixtureModel(2); gmm2Fit = gmm2.Compute(AreaDis.ToArray()); int a = (gmm2.Gaussians[0].Mean[0] < gmm2.Gaussians[1].Mean[0]) ? 0 : 1; p2Mean = gmm2.Gaussians[a].Mean[0]; p2Covariance = gmm2.Gaussians[a].Covariance[0, 0]; p2Proportion = gmm2.Gaussians[a].Proportion; p3Mean = gmm2.Gaussians[1 - a].Mean[0]; p3Covariance = gmm2.Gaussians[1 - a].Covariance[0, 0]; p3Proportion = gmm2.Gaussians[1 - a].Proportion; DistCount = Bucketize(CountDis); DistArea = Bucketize(AreaDis); //sw.Stop(); //Debug.Write(sw.Elapsed); } catch { throw new Exception("Error Occured During Airspace Categorization"); } }
public void GaussianMixtureModelTest5() { Accord.Math.Tools.SetupGenerator(0); MemoryStream stream = new MemoryStream(Resources.CircleWithWeights); ExcelReader reader = new ExcelReader(stream, xlsx: false); DataTable table = reader.GetWorksheet("Sheet1"); double[,] matrix = table.ToMatrix(); double[][] points = matrix.Submatrix(null, 0, 1).ToArray(); double[] weights = matrix.GetColumn(2); GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Compute(points, new GaussianMixtureModelOptions() { Weights = weights }); int a = 0; int b = 1; if ((-0.010550720353814949).IsRelativelyEqual(gmm.Gaussians[1].Mean[0], 1e-4)) { a = 1; b = 0; } Assert.AreEqual(-0.010550720353814949, gmm.Gaussians[a].Mean[0], 1e-4); Assert.AreEqual(0.40799698773355553, gmm.Gaussians[a].Mean[1], 1e-3); Assert.AreEqual(0.011896812071918696, gmm.Gaussians[b].Mean[0], 1e-4); Assert.AreEqual(-0.40400708592859663, gmm.Gaussians[b].Mean[1], 1e-4); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion, 1e-15); Assert.IsFalse(gmm.Gaussians[0].Mean.HasNaN()); Assert.IsFalse(gmm.Gaussians[1].Mean.HasNaN()); }
/// <summary> /// Estimates Gaussian distributions from the data. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Create a new Gaussian Mixture Model GaussianMixtureModel gmm = new GaussianMixtureModel(k); // If available, initialize with k-means if (kmeans != null) gmm.Initialize(kmeans); // Compute the model gmm.Compute(mixture); // Classify all instances in mixture data int[] classifications = gmm.Gaussians.Nearest(mixture); // Draw the classifications updateGraph(classifications); }
internal void Train() { logLike = new double[maxK]; // Log-likelihood estimate for a given model and training set rissanen = new double[maxK]; // Rissanen estimate for a given model and training set mdl = new double[maxK]; // Minimum description length for a given model and training set kVar = new bool[maxK]; for (int i = 0; i < maxK; ++i) { // Step 1: Make a GMM with K subclasses gmm = new GaussianMixtureModel(kVals[i]); // Step 2: fit the gmm to the projection data logLike[i] = gmm.Compute(currentProjection, 1e-3, 1.0); // Step 3: perform a classification to detect spurious classification kVar[i] = (kVals[i] == gmm.Classify(currentProjection).Distinct().ToArray().Length); // Step 4: Calculate the MDL for this K double L = (double)(kVals[i] * 3 - 1); rissanen[i] = 0.5 * L * Math.Log(currentProjection.Length); mdl[i] = -logLike[i] + rissanen[i]; } // Which value of K supported the MDL: int ind = Array.IndexOf(mdl, mdl.Min()); // Find the value of K that supports the lowest mdl and is verified K = kVals[ind]; while (!kVar[ind]) { K = kVals[ind]; ++ind; } // Recreate the gmm with the trained value of K gmm = new GaussianMixtureModel(K); double LL = gmm.Compute(currentProjection, 1e-3, 1.0); // Is this a functional classifier? if (gmm != null) trained = true; // Set the STD ellipsoid gmm.SetPValue(pValue); }
public void GenerateGMM() { double[][] mixture = new double[DeltaScores.Count][]; for (int i = 0; i < DeltaScores.Count; i++) { mixture[i] = new double[] { DeltaScores[i] }; } gmm = new GaussianMixtureModel(2); // If available, initialize with k-means //if (kmeans != null) gmm.Initialize(kmeans); // Compute the model GaussianMixtureModelOptions gmmo = new GaussianMixtureModelOptions(); gmmo.Logarithm = true; gmm.Compute(mixture, 0.000000000000000000000001); //Find out who is the big median gaussian and small media gaussian double[] meanTmp1 = gmm.Gaussians[0].Mean; double[] meanTmp2 = gmm.Gaussians[1].Mean; ProportionSmallGaussian = -1; ProportionBigGaussian = -1; if (meanTmp1[0] < meanTmp2[0]) { GaussianSmallMean = gmm.Gaussians[0].ToDistribution(); ProportionSmallGaussian = gmm.Gaussians[0].Proportion; GaussianBigMean = gmm.Gaussians[1].ToDistribution(); ProportionBigGaussian = gmm.Gaussians[1].Proportion; } else { GaussianSmallMean = gmm.Gaussians[1].ToDistribution(); ProportionSmallGaussian = gmm.Gaussians[1].Proportion; GaussianBigMean = gmm.Gaussians[0].ToDistribution(); ProportionBigGaussian = gmm.Gaussians[0].Proportion; } //And now find GMM intersect and the cumulative intersect double min = DeltaScores.Min(); double max = DeltaScores.Max(); List <double> smallMeanValues = new List <double>(); List <double> largeMeanValues = new List <double>(); //Determine intersect of the two Gaussians double GMintersect = double.PositiveInfinity; double GMdistance2 = double.PositiveInfinity; double Cintersect = double.PositiveInfinity; double Cdistance2 = double.PositiveInfinity; for (double i = GaussianSmallMean.Median[0]; i <= GaussianBigMean.Median[0]; i += 0.02) { //Gaussian Intersect //double GMredG = GaussianSmallMean.ProbabilityDensityFunction(i) * ProportionSmallGaussian; //double GMblueG = GaussianBigMean.ProbabilityDensityFunction(i) * ProportionBigGaussian; double GMredG = GaussianSmallMean.ProbabilityDensityFunction(i); double GMblueG = GaussianBigMean.ProbabilityDensityFunction(i); double GMdistanceThis = Math.Abs(GMredG - GMblueG); if (GMdistanceThis < Math.Abs(GMdistance2)) { GMdistance2 = GMdistanceThis; GMintersect = i; } //Cumulative Intersect //double CredG = GaussianSmallMean.ComplementaryDistributionFunction(i) * ProportionSmallGaussian; //double CblueG = GaussianBigMean.DistributionFunction(i) * ProportionBigGaussian; double CredG = GaussianSmallMean.ComplementaryDistributionFunction(i); double CblueG = GaussianBigMean.DistributionFunction(i); double CdistanceThis = Math.Abs(CredG - CblueG); if (CdistanceThis < Math.Abs(Cdistance2)) { Cdistance2 = CdistanceThis; Cintersect = i; } } Console.WriteLine("Gaussian Meeting point : " + GMintersect); Console.WriteLine("Cumulative Meeting point : " + Cintersect); GaussianIntersect = GMintersect; CumulativeIntersect = Cintersect; }
// Method to rate mode goodness and then sort (use first mode as base) private void ComputeModeGoodnessRatio() { // Use no more than Max_N (which is 5 I believe) int n = lstCentroids.Count; // find as many Gaussians as modes. // Don't do too many Gaussians because it will take a long time and perform poorly if (n > GCount) { n = GCount; } // Multiple dist map and diff map if necessary //DateTime startTime = DateTime.Now; ComputeRealMap(); //DateTime stopTime = DateTime.Now; //TimeSpan duration = stopTime - startTime; //double RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("ComputeRealMap Run time " + RunTime + " seconds!"); // Allocate memory for output matrices Array arrModes = new double[n]; Array arrProportions = new double[n]; Array arrMUs = new double[n, 2]; Array arrSigmaXSigmaY = new double[n]; Array junkModes = new double[n]; Array junkMUs = new double[n, 2]; Array junkSigmaXSigmaY = new double[n]; #region Using MATLAB for GMM ////startTime = DateTime.Now; //double[,] arrSamplesR; //double[,] arrSamplesI; //// Generate samples from map to get ready to perform mixed Gaussian fitting //PrepareSamples(out arrSamplesR, out arrSamplesI); ////stopTime = DateTime.Now; ////duration = stopTime - startTime; ////RunTime = duration.TotalSeconds; ////System.Windows.Forms.MessageBox.Show("PrepareSamples Run time " + RunTime + " seconds!"); //// Perform mixed Gaussian fitting and get parameters ////startTime = DateTime.Now; //// Using MATLAB to do GMM //GaussianFitting(n, arrSamplesR, arrSamplesI, ref arrModes, ref arrMUs, ref arrSigmaXSigmaY, ref junkModes, ref junkMUs, ref junkSigmaXSigmaY); ////stopTime = DateTime.Now; ////duration = stopTime - startTime; ////RunTime = duration.TotalSeconds; ////System.Windows.Forms.MessageBox.Show("GaussianFitting Run time " + RunTime + " seconds!"); //// Debug //// curRequest.SetLog("\nMATLAB GMM results\n"); #endregion #region Using C# for GMM // Prepare samples into the format needed double[][] arrSamples; // Generate samples from map to get ready to perform mixed Gaussian fitting PrepareSamplesAccord(out arrSamples); // Using Accord.net library to do GMM GaussianMixtureModel gmm = new GaussianMixtureModel(n); List<MapMode> lstGaussians = new List<MapMode>(); // Results stored in a list of MapModes for sorting // If Accord.net library fails, try it again up to 3 times for (int ii = 0; ii < ProjectConstants.MaxAccordRun; ii++) { try { gmm.Compute(arrSamples, 10); //// Debug code //// Print out means and covariances and proportions so we can plot //Console.WriteLine("Accord.net run number " + ii); //for (int i = 0; i < n; i++) //{ // Console.WriteLine("Gaussian number " + i); // // Means // Console.Write("Mean: (" + gmm.Gaussians[i].Mean[0] + " " + gmm.Gaussians[i].Mean[1] + ") "); // // Area // Console.Write("Covariance Matrix [" + gmm.Gaussians[i].Covariance[0, 0] + " " // + gmm.Gaussians[i].Covariance[0, 1] + "; " // + gmm.Gaussians[i].Covariance[1, 0] + " " // + gmm.Gaussians[i].Covariance[1, 1] + "] "); // Console.Write("Proportion: " + gmm.Gaussians[i].Proportion + "\n"); //} // Getting arrays ready for (int i = 0; i < n; i++) { // Means arrMUs.SetValue(gmm.Gaussians[i].Mean[0], i, 0); arrMUs.SetValue(gmm.Gaussians[i].Mean[1], i, 1); // Area DenseMatrix m = new DenseMatrix(gmm.Gaussians[i].Covariance); System.Numerics.Complex[] d = m.Evd().EigenValues().ToArray(); double SigmaXSigmaY = Math.Sqrt(d[0].Real) * Math.Sqrt(d[1].Real); arrSigmaXSigmaY.SetValue(SigmaXSigmaY, i); // Modes arrModes.SetValue(gmm.Gaussians[i].GetDistribution().ProbabilityDensityFunction(gmm.Gaussians[i].Mean), i); // Scales arrProportions.SetValue(gmm.Gaussians[i].Proportion, i); } // Debug // curRequest.SetLog("\nAccord GMM results\n"); ////Debug code //for (int i = 0; i < arrMUs.Length / 2; i++) //{ // curRequest.SetLog(arrMUs.GetValue(i, 0) + "," + arrMUs.GetValue(i, 1) + " "); //} //curRequest.SetLog("\n"); //for (int i = 0; i < arrSigmaXSigmaY.Length; i++) //{ // curRequest.SetLog(arrSigmaXSigmaY.GetValue(i) + " "); //} //curRequest.SetLog("\n"); //for (int i=0; i< arrModes.Length; i++) //{ // curRequest.SetLog(arrModes.GetValue(i) + " "); //} //curRequest.SetLog("\n"); //startTime = DateTime.Now; // Match centroids to Gaussians MatchCentroidsToGaussians(arrMUs, lstGaussians); //stopTime = DateTime.Now; //duration = stopTime - startTime; //RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("MatchCentroidsToGaussians Run time " + RunTime + " seconds!"); ii = ProjectConstants.MaxAccordRun; } catch { Console.WriteLine("Something went wrong with Accord.net library."); // System.Windows.Forms.MessageBox.Show("Something went wrong with Accord.net library."); } } #endregion //startTime = DateTime.Now; // Evaluate Goodness Rating EvaluateGoodnessRatings(arrModes, arrSigmaXSigmaY, lstGaussians, arrProportions); //stopTime = DateTime.Now; //duration = stopTime - startTime; //RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("EvaluateGoodnessRatings Run time " + RunTime + " seconds!"); //// Debug code //// Print out all Gaussians found //Console.WriteLine("Goodness Ratio:"); //for (int ii = 0; ii < lstGaussians.Count; ii++) //{ // Console.Write("Gaussian number " + ii + ": " + lstGaussians[ii].GoodnessRating + ", "); //} //Console.Write("\n"); // Find top N modes lstGaussians.Sort(); lstGaussians.Reverse(); ////Debug //for (int i = 0; i < n; i++) //{ // curRequest.SetLog("Mode (x,y): " + lstGaussians[i].Mode.X + "," + lstGaussians[i].Mode.Y + " MGR:" + lstGaussians[i].GoodnessRating + "\n"); //} lstGaussians.RemoveRange(N, lstGaussians.Count - N); // Rebuild lstCentroids lstCentroids.Clear(); foreach (MapMode mm in lstGaussians) { lstCentroids.Add(mm.Mode); } }
public void buildModel() { if (inputMatrix == null) getMatrix(); switch (cType) { case clusterType.KMEANS: KMeans kmeans = new KMeans(k); kmeans.Compute(inputMatrix, precision); clusterCollection = kmeans.Clusters; model = kmeans; break; case clusterType.BINARY: BinarySplit bSplit = new BinarySplit(k); bSplit.Compute(inputMatrix, precision); clusterCollection = bSplit.Clusters; model = bSplit; //Console.WriteLine("BinarySplit"); break; case clusterType.GAUSSIANMIXTURE: GaussianMixtureModel gModel = new GaussianMixtureModel(k); gModel.Compute(inputMatrix, precision); clusterCollection = gModel.Gaussians; model = gModel; break; default: break; } lbl = new List<string>(); for (int i = 0; i < k; i++) { lbl.Add(i.ToString()); } }
public void GaussianMixtureModelTest5() { Accord.Math.Tools.SetupGenerator(0); MemoryStream stream = new MemoryStream(Resources.CircleWithWeights); ExcelReader reader = new ExcelReader(stream, xlsx: false); DataTable table = reader.GetWorksheet("Sheet1"); double[,] matrix = table.ToMatrix(); double[][] points = matrix.Submatrix(null, 0, 1).ToJagged(); double[] weights = matrix.GetColumn(2); GaussianMixtureModel gmm = new GaussianMixtureModel(2); gmm.Initializations = 1; gmm.Compute(points, new GaussianMixtureModelOptions() { Weights = weights }); int a = 0; int b = 1; double tol = 1e-3; if ((-0.407859903454185).IsRelativelyEqual(gmm.Gaussians[1].Mean[0], tol)) { a = 1; b = 0; } Assert.AreEqual(-0.407859903454185, gmm.Gaussians[a].Mean[0], tol); Assert.AreEqual(-0.053911705279706859, gmm.Gaussians[a].Mean[1], tol); Assert.AreEqual(0.39380877640250328, gmm.Gaussians[b].Mean[0], tol); Assert.AreEqual(0.047186154880776772, gmm.Gaussians[b].Mean[1], tol); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion, 1e-15); Assert.IsFalse(gmm.Gaussians[0].Mean.HasNaN()); Assert.IsFalse(gmm.Gaussians[1].Mean.HasNaN()); }
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.Compute(points, new GaussianMixtureModelOptions() { Weights = weights }); int a = 0; int b = 1; if (gmm.Gaussians[1].Mean[0].IsRelativelyEqual(6.420790676635443, 1e-10)) { a = 1; b = 0; } Assert.AreEqual(6.420790676635443, gmm.Gaussians[a].Mean[0], 1e-10); Assert.AreEqual(0.290536871335858, gmm.Gaussians[b].Mean[0], 1e-10); Assert.AreEqual(0.32294476897888613, gmm.Gaussians[a].Proportion, 1e-6); Assert.AreEqual(0.67705523102111387, gmm.Gaussians[b].Proportion, 1e-6); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion); } // without 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.Compute(points); int a = 0; int b = 1; if (6.5149525060859865.IsRelativelyEqual(gmm.Gaussians[1].Mean[0], 1e-10)) { a = 1; b = 0; } Assert.AreEqual(6.5149525060859865, gmm.Gaussians[a].Mean[0], 1e-10); Assert.AreEqual(1.4191977895308987, gmm.Gaussians[b].Mean[0], 1e-6); Assert.AreEqual(0.42235760973845654, gmm.Gaussians[a].Proportion, 1e-6); Assert.AreEqual(0.57764239026154351, gmm.Gaussians[b].Proportion, 1e-6); Assert.AreEqual(1, gmm.Gaussians[0].Proportion + gmm.Gaussians[1].Proportion); } }
public void LargeSampleTest() { Accord.Math.Tools.SetupGenerator(0); Func<double> r = () => Tools.Random.NextDouble(); Func<double> b = () => Tools.Random.NextDouble() > 0.3 ? 1 : -1; // Test Samples int thousand = 1000; int million = thousand * thousand; double[][] samples = new double[5 * million][]; Func<int, double[]> expand = (int label) => { var d = new double[10]; d[label] = 1; return d; }; for (int j = 0; j < samples.Length; j++) { if (j % 10 > 8) samples[j] = new double[] { r() }.Concatenate(expand(Tools.Random.Next() % 10)); else samples[j] = new double[] { r() * j }.Concatenate(expand(j % 10)); } // Create a new Gaussian Mixture Model with 2 components GaussianMixtureModel gmm = new GaussianMixtureModel(5); // Compute the model double result = gmm.Compute(samples, new GaussianMixtureModelOptions() { NormalOptions = new NormalOptions() { Regularization = 1e-5, } }); for (int i = 0; i < samples.Length; i++) { var sample = samples[i]; int c = gmm.Gaussians.Nearest(sample); Assert.AreEqual(c, (i % 10) >= 5 ? 1 : 0); } }
public void GaussianMixtureModelTest2() { Accord.Math.Random.Generator.Seed = 0; int height = 16; int width = 16; var gmm = new GaussianMixtureModel(3); // gmm.Regularization = 0; Assert.AreEqual(3, gmm.Gaussians.Count); Assert.IsNull(gmm.Gaussians[0].Covariance); Assert.IsNull(gmm.Gaussians[0].Mean); double[][][] A = new double[3][][]; A[0] = new double[height][]; A[1] = new double[height][]; A[2] = new double[height][]; for (int j = 0; j < height; j++) { A[0][j] = new double[width]; A[1][j] = new double[width]; A[2][j] = new double[width]; for (int k = 0; k < width; k++) { A[0][j][k] = 102; A[1][j][k] = 57; A[2][j][k] = 200; } } double[][] B = Matrix.Stack(A); bool thrown = false; try { var result = gmm.Compute(B); } catch (NonPositiveDefiniteMatrixException) { thrown = true; } Assert.IsTrue(thrown); }
public void GaussianMixtureModelTest3() { Accord.Math.Tools.SetupGenerator(0); var gmm = new GaussianMixtureModel(3); Assert.AreEqual(3, gmm.Gaussians.Count); Assert.IsNull(gmm.Gaussians[0].Covariance); Assert.IsNull(gmm.Gaussians[0].Mean); double[][] B = Matrix.Random(56, 12).ToArray(); var result = gmm.Compute(B); }