This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
GaussianMixtureModel.cs
532 lines (460 loc) · 18.3 KB
/
GaussianMixtureModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Accord Machine Learning Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.MachineLearning
{
using Accord.Math;
using Accord.Statistics.Distributions.Fitting;
using Accord.Statistics.Distributions.Multivariate;
using Accord.Statistics.Distributions.Univariate;
using System;
using Accord.Compat;
using System.Threading.Tasks;
/// <summary>
/// Gaussian mixture model clustering.
/// </summary>
///
/// <remarks>
/// Gaussian Mixture Models are one of most widely used model-based
/// clustering methods. This specialized class provides a wrap-up
/// around the
/// <see cref="Statistics.Distributions.Multivariate.MultivariateMixture{Normal}">
/// Mixture<NormalDistribution></see> distribution and provides
/// mixture initialization using the K-Means clustering algorithm.
/// </remarks>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.MachineLearning\Clustering\GaussianMixtureModelTest.cs" region="doc_learn" />
/// </example>
///
/// <seealso cref="KMeans"/>
/// <seealso cref="MeanShift"/>
///
[Serializable]
public class GaussianMixtureModel : ParallelLearningBase,
IUnsupervisedLearning<GaussianClusterCollection, double[], int>,
#pragma warning disable 0618
IClusteringAlgorithm<double[], double>
#pragma warning restore 0618
{
private GaussianClusterCollection clusters;
/// <summary>
/// Gets or sets the maximum number of iterations to
/// be performed by the method. If set to zero, no
/// iteration limit will be imposed. Default is 0.
/// </summary>
///
public int MaxIterations { get; set; }
/// <summary>
/// Gets or sets the convergence criterion for the
/// Expectation-Maximization algorithm. Default is 1e-3.
/// </summary>
///
/// <value>The convergence threshold.</value>
///
public double Tolerance { get; set; }
/// <summary>
/// Gets or sets whether cluster labels should be computed
/// at the end of the learning iteration. Setting to <c>False</c>
/// might save a few computations in case they are not necessary.
/// </summary>
///
public bool ComputeLabels { get; set; }
/// <summary>
/// Gets or sets whether the log-likelihood should be computed
/// at the end of the learning iteration. Setting to <c>False</c>
/// might save a few computations in case they are not necessary.
/// </summary>
///
public bool ComputeLogLikelihood { get; set; }
/// <summary>
/// Gets the log-likelihood of the model at the last iteration.
/// </summary>
///
public double LogLikelihood { get; private set; }
/// <summary>
/// Gets or sets how many random initializations to try.
/// Default is 3.
/// </summary>
///
public int Initializations { get; set; }
/// <summary>
/// Gets how many iterations have been performed in the last call
/// to <see cref="Compute(double[][])"/>.
/// </summary>
///
public int Iterations { get; private set; }
/// <summary>
/// Gets or sets whether to make computations using the log
/// -domain. This might improve accuracy on large datasets.
/// </summary>
///
public bool UseLogarithm { get; set; }
/// <summary>
/// Gets or sets the fitting options for the component
/// Gaussian distributions of the mixture model.
/// </summary>
///
/// <value>The fitting options for inner Gaussian distributions.</value>
///
public NormalOptions Options { get; set; }
/// <summary>
/// Gets the Gaussian components of the mixture model.
/// </summary>
///
public GaussianClusterCollection Gaussians
{
get { return clusters; }
}
/// <summary>
/// Initializes a new instance of the <see cref="GaussianMixtureModel"/> class.
/// </summary>
///
/// <param name="components">
/// The number of clusters in the clustering problem. This will be
/// used to set the number of components in the mixture model.</param>
///
public GaussianMixtureModel(int components)
{
if (components <= 0)
{
throw new ArgumentOutOfRangeException("components",
"The number of components should be greater than zero.");
}
constructor(components);
}
/// <summary>
/// Initializes a new instance of the <see cref="GaussianMixtureModel"/> class.
/// </summary>
///
/// <param name="kmeans">
/// The initial solution as a K-Means clustering.</param>
///
public GaussianMixtureModel(KMeans kmeans)
{
if (kmeans == null)
throw new ArgumentNullException("kmeans");
constructor(kmeans.K);
clusters.Initialize(kmeans);
}
/// <summary>
/// Initializes a new instance of the <see cref="GaussianMixtureModel"/> class.
/// </summary>
///
/// <param name="mixture">
/// The initial solution as a mixture of normal distributions.</param>
///
public GaussianMixtureModel(Mixture<NormalDistribution> mixture)
{
if (mixture == null)
throw new ArgumentNullException("mixture");
constructor(mixture.Components.Length);
clusters.Initialize(mixture);
}
/// <summary>
/// Initializes a new instance of the <see cref="GaussianMixtureModel"/> class.
/// </summary>
///
/// <param name="mixture">
/// The initial solution as a mixture of normal distributions.</param>
///
public GaussianMixtureModel(MultivariateMixture<MultivariateNormalDistribution> mixture)
{
if (mixture == null)
throw new ArgumentNullException("mixture");
constructor(mixture.Components.Length);
clusters.Initialize(mixture);
}
private void constructor(int components)
{
ComputeLabels = true;
ComputeLogLikelihood = true;
Tolerance = 1e-3;
Initializations = 3;
MaxIterations = 0;
UseLogarithm = true;
Options = new Statistics.Distributions.Fitting.NormalOptions();
ParallelOptions = new ParallelOptions();
// Initialize the model using the created objects.
this.clusters = new GaussianClusterCollection(components);
}
/// <summary>
/// Divides the input data into K clusters modeling each
/// cluster as a multivariate Gaussian distribution.
/// </summary>
///
[Obsolete("Please use Learn(x) instead.")]
public int[] Compute(double[][] data)
{
return Learn(data).Decide(data);
}
/// <summary>
/// Divides the input data into K clusters modeling each
/// cluster as a multivariate Gaussian distribution.
/// </summary>
///
[Obsolete("Please use Learn(x) instead.")]
public int[] Compute(double[][] data, double[] weights)
{
return Learn(data, weights).Decide(data);
}
/// <summary>
/// Learns a model that can map the given inputs to the desired outputs.
/// </summary>
/// <param name="x">The model inputs.</param>
/// <param name="weights">The weight of importance for each input sample.</param>
/// <returns>A model that has learned how to produce suitable outputs
/// given the input data <paramref name="x" />.</returns>
public GaussianClusterCollection Learn(double[][] x, double[] weights = null)
{
if (clusters.Model == null)
Initialize(x, weights);
// Create the mixture options
var mixtureOptions = new MixtureOptions()
{
Threshold = this.Tolerance,
InnerOptions = this.Options,
MaxIterations = this.MaxIterations,
Logarithm = this.UseLogarithm,
ParallelOptions = ParallelOptions,
};
MultivariateMixture<MultivariateNormalDistribution> model = clusters.Model;
// Fit a multivariate Gaussian distribution
model.Fit(x, weights, mixtureOptions);
#pragma warning disable 612, 618
this.Iterations = mixtureOptions.Iterations;
#pragma warning restore 612, 618
for (int i = 0; i < clusters.Model.Components.Length; i++)
clusters.Centroids[i] = new MixtureComponent<MultivariateNormalDistribution>(model, i);
if (ComputeLogLikelihood)
LogLikelihood = model.LogLikelihood(x);
Accord.Diagnostics.Debug.Assert(clusters.NumberOfClasses == clusters.Model.Components.Length);
Accord.Diagnostics.Debug.Assert(clusters.NumberOfOutputs == clusters.Model.Components.Length);
Accord.Diagnostics.Debug.Assert(clusters.NumberOfInputs == x[0].Length);
return clusters;
}
/// <summary>
/// Initializes the model with initial values obtained
/// through a run of the K-Means clustering algorithm.
/// </summary>
///
public void Initialize(double[][] data, double[] weights)
{
var kmeans = new KMeans[Initializations];
double[] errors = new double[Initializations];
Parallel.For(0, kmeans.Length, ParallelOptions, i =>
{
// Create a new K-Means algorithm
kmeans[i] = new KMeans(clusters.Count)
{
ComputeCovariances = true,
ComputeError = true,
UseSeeding = Seeding.KMeansPlusPlus,
Tolerance = Tolerance
};
// Compute the K-Means
kmeans[i].Learn(data, weights);
errors[i] = kmeans[i].Error;
});
int index = errors.ArgMin();
// Initialize the model with K-Means
clusters.Initialize(kmeans[index]);
}
/// <summary>
/// Initializes the model with initial values obtained
/// through a run of the K-Means clustering algorithm.
/// </summary>
///
public void Initialize(KMeans kmeans)
{
clusters.Initialize(kmeans);
}
/// <summary>
/// Initializes the model with initial values.
/// </summary>
///
public void Initialize(MultivariateNormalDistribution[] distributions)
{
clusters.Initialize(distributions);
}
/// <summary>
/// Initializes the model with initial values.
/// </summary>
///
public void Initialize(NormalDistribution[] distributions)
{
clusters.Initialize(distributions);
}
/// <summary>
/// Initializes the model with initial values.
/// </summary>
///
public void Initialize(double[] coefficients, MultivariateNormalDistribution[] distributions)
{
clusters.Initialize(coefficients, distributions);
}
/// <summary>
/// Initializes the model with initial values.
/// </summary>
///
public void Initialize(double[] coefficients, NormalDistribution[] distributions)
{
clusters.Initialize(coefficients, distributions);
}
/// <summary>
/// Initializes the model with initial values.
/// </summary>
///
public void Initialize(MultivariateMixture<MultivariateNormalDistribution> mixture)
{
clusters.Initialize(mixture);
}
/// <summary>
/// Initializes the model with initial values.
/// </summary>
///
public void Initialize(Mixture<NormalDistribution> mixture)
{
clusters.Initialize(mixture);
}
/// <summary>
/// Gets a copy of the mixture distribution modeled by this Gaussian Mixture Model.
/// </summary>
///
public MultivariateMixture<MultivariateNormalDistribution> ToMixtureDistribution()
{
return clusters.ToMixtureDistribution();
}
# region Deprecated
/// <summary>
/// Divides the input data into K clusters modeling each
/// cluster as a multivariate Gaussian distribution.
/// </summary>
///
[Obsolete("Please set the properties of this class directly and use Learn(double[]) instead.")]
public double Compute(double[][] data, GaussianMixtureModelOptions options)
{
this.Options = options.NormalOptions;
this.MaxIterations = options.Iterations;
this.UseLogarithm = options.Logarithm;
this.Tolerance = options.Threshold;
this.ParallelOptions = options.ParallelOptions;
Compute(data, options.Weights);
return LogLikelihood;
}
/// <summary>
/// Divides the input data into K clusters modeling each
/// cluster as a multivariate Gaussian distribution.
/// </summary>
///
[Obsolete("Please set the tolerance threshold using the Tolerance property and use Compute(double[]) instead.")]
public double Compute(double[][] data, double threshold)
{
this.Tolerance = threshold;
Learn(data);
return LogLikelihood;
}
#endregion
#pragma warning disable 0618
/// <summary>
/// Gets the collection of clusters currently modeled by the
/// clustering algorithm.
/// </summary>
///
[Obsolete("Please use Gaussians instead")]
public IClusterCollection<double[]> Clusters
{
get { return (IClusterCollection<double[]>)Gaussians; }
}
IClusterCollection<double[]> IUnsupervisedLearning<IClusterCollection<double[]>, double[], int>.Learn(double[][] x, double[] weights)
{
return Learn(x);
}
#pragma warning restore 0618
}
/// <summary>
/// Options for Gaussian Mixture Model fitting.
/// </summary>
///
/// <remarks>
/// This class provides different options that can be passed to a
/// <see cref="GaussianMixtureModel"/> object when calling its
/// <see cref="GaussianMixtureModel.Compute(double[][], GaussianMixtureModelOptions)"/>
/// method.
/// </remarks>
///
/// <seealso cref="GaussianMixtureModel"/>
///
[Obsolete("This class will be removed.")]
public class GaussianMixtureModelOptions
{
/// <summary>
/// Gets or sets the convergence criterion for the
/// Expectation-Maximization algorithm. Default is 1e-3.
/// </summary>
///
/// <value>The convergence threshold.</value>
///
public double Threshold { get; set; }
/// <summary>
/// Gets or sets the maximum number of iterations
/// to be performed by the Expectation-Maximization
/// algorithm. Default is zero (iterate until convergence).
/// </summary>
///
public int Iterations { get; set; }
/// <summary>
/// Gets or sets whether to make computations using the log
/// -domain. This might improve accuracy on large datasets.
/// </summary>
///
public bool Logarithm { get; set; }
/// <summary>
/// Gets or sets the sample weights. If set to null,
/// the data will be assumed equal weights. Default
/// is null.
/// </summary>
///
public double[] Weights { get; set; }
/// <summary>
/// Gets or sets the fitting options for the component
/// Gaussian distributions of the mixture model.
/// </summary>
///
/// <value>The fitting options for inner Gaussian distributions.</value>
///
public NormalOptions NormalOptions { get; set; }
/// <summary>
/// Gets or sets parallelization options.
/// </summary>
///
public ParallelOptions ParallelOptions { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="GaussianMixtureModelOptions"/> class.
/// </summary>
///
public GaussianMixtureModelOptions()
{
Threshold = 1e-3;
ParallelOptions = new ParallelOptions();
}
}
}