6
我試圖在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算法,並將結果與一組數據進行比較。 請教我在何處以及如何實現高斯樸素貝葉斯算法。
謝謝!
沒有人能幫助我嗎? :( – Urmelinho 2012-03-25 16:03:28
Urmelinho:提供賞金,有人可能會幫助:-) – 2012-03-29 05:18:40
對於一些想法,我不認爲有人想從我這裏得到賞金...對於這部分算法我完全沒有。您可能會認爲我的謝意將會是您對解決方案的回報。我會考慮任何建議作爲解決方案:D – Urmelinho 2012-03-29 10:34:28