⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classifier.cs

📁 It s the Naive Bayes algorithm with import function dat-files, realized by the tools of Visual Stu
💻 CS
字号:
using System;
using System.Collections;
using System.Text;

namespace NaiveBayes
{
    class Classifier
    {
        private ArrayList priorProbabilities = new ArrayList();
        private ArrayList posteriorProbabilities = new ArrayList();
        private int classAssumed;

        public Classifier(TestSample testSample, TargetAttribute target, ArrayList attributes)
        {
            foreach (int type in target.getDigitizedTypes())
            {
                countPriorProbabilities(type, target);
                countPosteriorProbabilities(type, target, attributes, testSample, countPosNum(type, target));
            }
            classify(target);
        }

        private void classify(TargetAttribute target)
        {
            ArrayList probabilities = new ArrayList();
            for (int i = 0; i < this.posteriorProbabilities.Count; i++)
            {
                probabilities.Add(Convert.ToDouble(this.posteriorProbabilities[i]) * Convert.ToDouble(this.priorProbabilities[i]));
            }
            double maxProb = 0;
          
            for (int i = 0; i < probabilities.Count; i++)
            {
                if (Convert.ToDouble(probabilities[i]) > maxProb)
                {
                    maxProb = Convert.ToDouble(probabilities[i]);
                    this.classAssumed = i;
                }
            }

        }
        private int countPosNum(int type, TargetAttribute target)
        {
            int num = 0;
            for (int i = 0; i < target.getDigitizedValues().Count; i++)
            {
                if (type.Equals(target.getDigitizedValues()[i]))
                    num++;
            }
            return num;
        }
       
        private void countPriorProbabilities(int type, TargetAttribute target)
        {
            double prob = 0;
            for (int i = 0; i < target.getDigitizedValues().Count; i++)
            {
                if (type.Equals(target.getDigitizedValues()[i]))
                    prob++;
            }
            this.priorProbabilities.Add(prob / Convert.ToDouble(target.getDigitizedValues().Count));
        }

        private void countPosteriorProbabilities(int type, TargetAttribute target, ArrayList attributes, TestSample testSample, int num)
        {
            double attrProb = 1;
            for (int i = 0; i < attributes.Count - 1; i++)
            {
                 double prob = 0;
                for (int j = 0; j < (attributes[i] as Attribute).getDigitizedValues().Count; j++)
                {
                    if (Convert.ToInt32((attributes[i] as Attribute).getDigitizedValues()[j]).Equals(Convert.ToInt32(testSample.getValues()[i])))
                    {
                        if (type.Equals(Convert.ToInt32(target.getDigitizedValues()[j])))
                            prob++;
                    }
                }
                attrProb*=(prob/num);
            }
            this.posteriorProbabilities.Add(attrProb);
        }

        public int getClassAssumed()
        {
            return this.classAssumed;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -