Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Gaussian Naive Bayes

I'm trying to implement Gaussian Naive Bayes in C# for classification of points. I have implemented first part ( http://www.statsoft.com/textbook/naive-bayes-classifier/ ) probability part, but i don't understand how to implement Gaussian Naive Bayes algorithm normal model. This is my code:

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;
        }
    }

In this pdf file (Problem 5) is the description of what i need to do ( http://romanager.ro/s.10-701.hw1.sol.pdf ). My work is to implement Gaussina Naive Bayes and kNN algorithms and compare the result on a set of data. Please teach me where and how to implement Gaussian Naive Bayes algorithm.

Thanks!

like image 748
Urmelinho Avatar asked Mar 25 '12 12:03

Urmelinho


People also ask

How do you use Gaussian Naive Bayes?

Gaussian Naive Bayes supports continuous valued features and models each as conforming to a Gaussian (normal) distribution. An approach to create a simple model is to assume that the data is described by a Gaussian distribution with no co-variance (independent dimensions) between dimensions.

What is Gaussian naive Bayes algorithm?

What is Gaussian Naïve Bayes algorithm? Gaussian Naive Bayes is a probabilistic classification algorithm based on applying Bayes' theorem with strong independence assumptions.

Is Naive Bayes same as Gaussian Naive Bayes?

Naive Bayes is a generative model. (Gaussian) Naive Bayes assumes that each class follow a Gaussian distribution. The difference between QDA and (Gaussian) Naive Bayes is that Naive Bayes assumes independence of the features, which means the covariance matrices are diagonal matrices.


1 Answers

good example here:

look at sex predictor at bottom of this page as an example with a real data set

Good explanation here:

naive bayes breakdown on stackoverflow

like image 50
negEntropy Avatar answered Sep 26 '22 02:09

negEntropy