实施高斯朴素贝叶斯 [英] Implement Gaussian Naive Bayes

查看:661
本文介绍了实施高斯朴素贝叶斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现高斯朴素贝叶斯在C#中的点分类。我有 实施第一部分( http://www.statsoft.com/textbook/naive-bayes-分类/ )概率的一部分,但我不知道如何实现高斯朴素贝叶斯算法的正常模式。 这是我的code:

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

在此PDF文件(问题5)是什么,我需要做的(的 http://romanager.ro/s.10-701.hw1.sol.pdf )。我的工作是贯彻落实Gaussina朴素贝叶斯与最邻近算法和一组数据的结果进行比较。 请告诉我在哪里以及如何实现高斯朴素贝叶斯算法。

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.

谢谢!

推荐答案

很好的例子:

看色情predictor在本页面底部有一个真实的数据集的例子

好解释一下:

<一个href="http://stackoverflow.com/questions/10059594/a-simple-explanation-of-naive-bayes-classification">naive在计算器

这篇关于实施高斯朴素贝叶斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆