Реализовать гауссовский наивный байес

Я пытаюсь реализовать Gaussian Naive Bayes в C# для классификация очков. у меня есть реализована первая часть (http://www.statsoft.com/textbook/naive-bayes-classifier/), вероятностная часть, но я не понимаю, как реализовать нормальную модель алгоритма Gaussian Naive Bayes. Это мой код:

class NaiveBayesClassifier
    {
        private List listTrainPoints = new List();
        private int totalPoints = 0;

        public NaiveBayesClassifier(List listTrainPoints) 
        {
            this.listTrainPoints = listTrainPoints;
            this.totalPoints = this.listTrainPoints.Count;
        }

        private List vecinityPoints(Point p, double maxDist)
        {
            List listVecinityPoints = new List();
            for (int i = 0; i < listTrainPoints.Count; i++)
            {
                if (p.distance(listTrainPoints[i]) <= maxDist)
                {
                    listVecinityPoints.Add(listTrainPoints[i]);
                }
            }
            return listVecinityPoints;
        }

        public double priorProbabilityFor(double currentType)
        {
            double countCurrentType = 0;
            for (int i = 0; i < this.listTrainPoints.Count; i++)
            {
                if (this.listTrainPoints[i].Type == currentType)
                {
                    countCurrentType++;
                }
            }

            return (countCurrentType / this.totalPoints);
        }

        public double likelihoodOfXGiven(double currentType, List listVecinityPoints)
        {
            double countCurrentType = 0;
            for (int i = 0; i < listVecinityPoints.Count; i++)
            {
                if (listVecinityPoints[i].Type == currentType)
                {
                    countCurrentType++;
                }
            }

            return (countCurrentType / this.totalPoints);
        }

        public double posteriorProbabilityXBeing(double priorProbabilityFor, double likelihoodOfXGiven)
        {
            return (priorProbabilityFor * likelihoodOfXGiven);
        }

        public int allegedClass(Point p, double maxDist)
        {
            int type1 = 1, type2 = 2;

            List listVecinityPoints = this.vecinityPoints(p, maxDist);

            double priorProbabilityForType1 = this.priorProbabilityFor(type1);
            double priorProbabilityForType2 = this.priorProbabilityFor(type2);

            double likelihoodOfXGivenType1 = likelihoodOfXGiven(type1, listVecinityPoints);
            double likelihoodOfXGivenType2 = likelihoodOfXGiven(type2, listVecinityPoints);

            double posteriorProbabilityXBeingType1 = posteriorProbabilityXBeing(priorProbabilityForType1, likelihoodOfXGivenType1);
            double posteriorProbabilityXBeingType2 = posteriorProbabilityXBeing(priorProbabilityForType2, likelihoodOfXGivenType2);

            if (posteriorProbabilityXBeingType1 > posteriorProbabilityXBeingType2)
                return type1;
            else
                return type2;
        }
    }

В этом pdf-файле (задача 5) есть описание того, что мне нужно сделать (http://romanager.ro/s.10-701.hw1.sol.pdf). Моя работа заключается в реализации алгоритмов Gaussina Naive Bayes и kNN и сравнении результатов на наборе данных. Пожалуйста, научите меня, где и как реализовать алгоритм Гаусса Наивного Байеса.

Спасибо!

7
задан Sirko 25 March 2012 в 12:09
поделиться