实现高斯朴素贝叶斯

时间:2012-03-25 12:04:26

标签: c# algorithm machine-learning bayesian

我正在尝试用C#中的高斯朴素贝叶斯来实现点的分类。我有 实现了第一部分(http://www.statsoft.com/textbook/naive-bayes-classifier/)概率部分,但我不明白如何实现高斯朴素贝叶斯算法的正态模型。 这是我的代码:

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

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

        private List<Point> vecinityPoints(Point p, double maxDist)
        {
            List<Point> listVecinityPoints = new List<Point>();
            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<Point> 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<Point> 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算法,并将结果与​​一组数据进行比较。 请教我在何处以及如何实现高斯朴素贝叶斯算法。

谢谢!

1 个答案:

答案 0 :(得分:2)