Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Accord.Statistics.Distributions.Univariate;
- using Accord.Statistics.Models.Markov;
- using Accord.Statistics.Models.Markov.Learning;
- using Accord.Statistics.Models.Markov.Topology;
- using Multivariate = Accord.Statistics.Distributions.Multivariate;
- namespace Test
- {
- //...
- // Create a Continuous density Hidden Markov Model Sequence Classifier
- // to detect a multivariate sequence and the same sequence backwards.
- double[][][] sequences = new double[][][]
- {
- new double[][]
- {
- // This is the first sequence with label = 0
- new double[] { 0 },
- new double[] { 1 },
- new double[] { 2 },
- new double[] { 3 },
- new double[] { 4 },
- },
- new double[][]
- {
- // This is the second sequence with label = 1
- new double[] { 4 },
- new double[] { 3 },
- new double[] { 2 },
- new double[] { 1 },
- new double[] { 0 },
- }
- };
- // Labels for the sequences
- int[] labels = { 0, 1 };
- // Create a mixture of two 1-dimensional normal distributions (by default,
- // initialized with zero mean and unit covariance matrices).
- var density = new Multivariate.Mixture<Multivariate.NormalDistribution>(
- new Multivariate.NormalDistribution(1),
- new Multivariate.NormalDistribution(1));
- // Creates a sequence classifier containing 2 hidden Markov Models with 2 states
- // and an underlying multivariate mixture of Normal distributions as density.
- var classifier = new ContinuousSequenceClassifier(2, new Ergodic(2), density);
- // Configure the learning algorithms to train the sequence classifier
- var teacher = new ContinuousSequenceClassifierLearning(classifier,
- // Train each model until the log-likelihood changes less than 0.0001
- modelIndex => new ContinuousBaumWelchLearning(classifier.Models[modelIndex])
- {
- Tolerance = 0.0001,
- Iterations = 0,
- }
- );
- // Train the sequence classifier using the algorithm
- double logLikelihood = teacher.Run(sequences, labels);
- // Calculate the probability that the given
- // sequences originated from the model
- double likelihood1, likelihood2;
- // Try to classify the 1st sequence (output should be 0)
- int c1 = classifier.Compute(sequences[0], out likelihood1);
- // Try to classify the 2nd sequence (output should be 1)
- int c2 = classifier.Compute(sequences[1], out likelihood2);
- // ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement