Click here to Skip to main content
11,576,963 members (52,564 online)
Click here to Skip to main content
Add your own
alternative version

Sequence Classifiers in C# - Part I: Hidden Markov Models

, 3 Dec 2014 CPOL 70.5K 7.4K 128
Let's understand hidden Markov models before taking a step into hidden conditional random fields.
gestures-bin.zip
Resources
Accord.dll
Accord.MachineLearning.dll
Accord.Math.dll
Accord.Statistics.dll
AForge.dll
AForge.Math.dll
Gestures.exe
Gestures.vshost.exe
Gestures.vshost.exe.manifest
gestures-src.zip
Externals
Accord.Controls.dll
Accord.dll
Accord.MachineLearning.dll
Accord.Math.dll
Accord.Statistics.dll
AForge.dll
AForge.Math.dll
Native
Properties
Settings.settings
Gestures.csproj.user
Gestures.pidb
Gestures.suo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
using System.Drawing;

namespace Gestures
{
    public class Database
    {
        public BindingList<string> Classes { get; private set; }
        public BindingList<Sequence> Samples { get; private set; }


        public Database()
        {
            Classes = new BindingList<string>();
            Samples = new BindingList<Sequence>();
        }

        public void Save(Stream stream)
        {
            var serializer = new XmlSerializer(typeof(BindingList<Sequence>));
            serializer.Serialize(stream, Samples);
        }

        public void Load(Stream stream)
        {
            var serializer = new XmlSerializer(typeof(BindingList<Sequence>));
            var samples = (BindingList<Sequence>)serializer.Deserialize(stream);

            Classes.Clear();
            foreach (string label in samples.First().Classes)
                Classes.Add(label);

            Samples.Clear();
            foreach (Sequence sample in samples)
            {
                sample.Classes = Classes;
                Samples.Add(sample);
            }
        }

        public Sequence Add(Point[] sequence, string classLabel)
        {
            if (sequence == null || sequence.Length == 0)
                return null;

            if (!Classes.Contains(classLabel))
                Classes.Add(classLabel);

            int classIndex = Classes.IndexOf(classLabel);

            Sequence sample = new Sequence()
            {
                Classes = Classes,
                SourcePath = sequence,
                Output = classIndex
            };

            Samples.Add(sample);

            return sample;
        }

        public void Clear()
        {
            Classes.Clear();
            Samples.Clear();
        }

        public int SamplesPerClass()
        {
            int min = 0;
            foreach (string label in Classes)
            {
                int c = Samples.Count(p => p.OutputName == label);

                if (min == 0) 
                    min = c;

                else if (c < min)
                    min = c;
            }

            return min;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

César de Souza
Engineer Xerox Research Center Europe
Brazil Brazil
Computer and technology enthusiast, interested in artificial intelligence and image processing. Has a Master's degree on Computer Science specialized on Image and Signal Processing, with expertise on Machine Learning, Computer Vision, Pattern Recognition and Data Mining systems. Author of the Accord.NET Framework for developing scientific computing applications.

If you would like to hire good developers to build your dream application, please check out DaitanGroup, one of the top outsourcing companies in Brazil. This company, located in Brazil's Sillicon Valley but with US-based offices, has huge experience developing telecommunications software for large and small companies worldwide.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 3 Dec 2014
Article Copyright 2013 by César de Souza
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid