The Naive Bayes Classifer – Machine Learning, Engine Implementation Part 1


Target Object Component

  • Tests of component (NUnit)
    using NaiveBayes.Category;
    using NaiveBayes.Variables;
    using NUnit.Framework;
    
    namespace TestNaiveBayes
    {
        [TestFixture]
        public class TestTargetObject
        {
            private ITargetObject _targetObject;
    
            private ICategory _category;
    
            [SetUp]
            public void SetUp()
            {
                CategoryFactory.AddCategoryType(TestCategoryFactory.CategoryName, TestCategoryFactory.CategoryTypes,
                                                TestCategoryFactory.CategoryAttributes);
    
                this._category = CategoryFactory.GetCategory(TestCategoryFactory.CategoryName);
    
                this._targetObject = new TargetObject(this._category, this._category.CategoryTypes[TestCategoryFactory.BeginnerCategory]);
            }
    
            [TearDown]
            public void TearDown()
            {
                CategoryFactory.RemoveCategory(TestCategoryFactory.CategoryName);
            }
    
            [Test]
            public void TestAfterInit()
            {
                Assert.AreEqual(this._targetObject.CategoryType, TestCategoryFactory.CategoryTypes[TestCategoryFactory.BeginnerCategory]);
    
                foreach (string variable in this._targetObject.AllAttributes.Keys)
                {
                    Assert.IsFalse(this._targetObject.AllAttributes[variable]);
                }
    
                Assert.AreEqual(this._targetObject.AllAttributes.Count, TestCategoryFactory.CategoryAttributes.Length);
                Assert.AreEqual(this._targetObject.ExistsAttributes.Count, 0);
            }
    
            [Test]
            public void TestSetUpSomeAttribute()
            {
                this._targetObject.SetAttributeExist(TestCategoryFactory.CategoryAttributes[TestCategoryFactory.Cheap]);
                this._targetObject.SetAttributeExist(TestCategoryFactory.CategoryAttributes[TestCategoryFactory.Freestyle]);
    
                Assert.AreEqual(this._targetObject.ExistsAttributes.Count, 2);
    
                this._targetObject.SetAttributeNotExist(TestCategoryFactory.CategoryAttributes[TestCategoryFactory.Cheap]);
    
                Assert.AreEqual(this._targetObject.ExistsAttributes.Count, 1);
            }
        }
    }
    
  • Interface – ITargetObject
    namespace NaiveBayes.Variables
    {
        public interface ITargetObject
        {
            IAttributesCollection AllAttributes { get; }
            IAttributesCollection ExistsAttributes { get; }
    
            string CategoryType { get; }
    
            void SetAttributeExist(string name);
            void SetAttributeNotExist(string name);
        }
    }
    
  • Implementation – TargetObject
    using NaiveBayes.Category;
    
    namespace NaiveBayes.Variables
    {
        public class TargetObject : ITargetObject
        {
            private readonly IAttributesCollection _variablesProbabilityMap;
    
            public TargetObject(ICategory category, string categoryType)
            {
                this.CategoryType = categoryType;
                this._variablesProbabilityMap = new AttributesCollection();
    
                foreach (string variableName in category.Attributes)
                {
                    this._variablesProbabilityMap.Add(variableName);
                }
            }
    
            #region ITargetObject Members
    
            public string CategoryType { get; private set; }
    
            public IAttributesCollection AllAttributes
            {
                get { return this._variablesProbabilityMap; }
            }
    
            public IAttributesCollection ExistsAttributes
            {
                get
                {
                    IAttributesCollection existAttributes = new AttributesCollection();
    
                    foreach (string key in this._variablesProbabilityMap.Keys)
                    {
                        if (this._variablesProbabilityMap[key])
                        {
                            existAttributes.Add(key, this._variablesProbabilityMap[key]);
                        }
                    }
    
                    return existAttributes;
                }
            }
    
            public void SetAttributeExist(string name)
            {
                this.ChangeExistingOfAttribute(name, true);
            }
    
            public void SetAttributeNotExist(string name)
            {
                this.ChangeExistingOfAttribute(name, false);
            }
    
            #endregion
    
            private void ChangeExistingOfAttribute(string name, bool result)
            {
                if (this._variablesProbabilityMap.ContainsKey(name))
                {
                    this._variablesProbabilityMap[name] = result;
                }
            }
        }
    }
    
  • Interface – IAttributesCollection
    using System.Collections.Generic;
    
    namespace NaiveBayes.Variables
    {
        public interface IAttributesCollection
        {
            bool this[string key] { get; set; }
    
            IEnumerable<string> Keys { get; }
            int Count { get; }
    
            void Add(string name);
            void Add(string name, bool exist);
            bool ContainsKey(string name);
        }
    }
    
  • Implementation – AttributesCollection
    using System.Collections.Generic;
    
    namespace NaiveBayes.Variables
    {
        public class AttributesCollection : IAttributesCollection
        {
            private readonly Dictionary<string, bool> _attributes;
    
            public AttributesCollection()
            {
                this._attributes = new Dictionary<string, bool>();
            }
    
            #region IAttributesCollection Members
    
            public void Add(string name)
            {
                this.Add(name, false);
            }
    
            public void Add(string name, bool exist)
            {
                this._attributes.Add(name, exist);
            }
    
            public bool this[string key]
            {
                get { return this._attributes[key]; }
                set { this._attributes[key] = value; }
            }
    
            public IEnumerable<string> Keys
            {
                get { return this._attributes.Keys; }
            }
    
            public int Count
            {
                get { return this._attributes.Count; }
            }
    
            public bool ContainsKey(string name)
            {
                return this._attributes.ContainsKey(name);
            }
    
            #endregion
        }
    }
    
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s