2012-03-25 5 views
6

Ich versuche, Gaussian Naive Bayes in C# für die Klassifizierung von Punkten zu implementieren. Ich habe implementiert ersten Teil (http://www.statsoft.com/textbook/naive-bayes-classifier/) Wahrscheinlichkeit Teil, aber ich verstehe nicht, wie Gauß Naive Bayes Algorithmus normales Modell zu implementieren. Dies ist mein Code:Implement Gaussian Naive Bayes

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 dieser pdf-Datei (Problem 5) ist die Beschreibung dessen, was ich tun muss (http://romanager.ro/s.10-701.hw1.sol.pdf). Meine Arbeit besteht darin, Gaussina Naive Bayes und kNN Algorithmen zu implementieren und das Ergebnis mit einer Reihe von Daten zu vergleichen. Bitte bringen Sie mir bei, wo und wie Gauß'sche Naive Bayes Algorithmen implementiert werden.

Danke!

+0

niemand kann mir helfen? :( – Urmelinho

+0

Urmelinho: Bieten Sie ein Kopfgeld und jemand könnte helfen :-) –

+0

für einige Ideen glaube ich nicht, dass jemand Bounty von mir wollen ... für diesen Teil des Algorithmus bin ich völlig aus. Sie können denken, dass mein Dank Ihre Belohnung für die Lösung sein wird. Ich werde jeden Rat als eine Lösung betrachten: D – Urmelinho

Antwort

Verwandte Themen