【发布时间】:2012-04-09 05:44:30
【问题描述】:
我正在尝试在 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:提供赏金,有人可能会提供帮助 :-)
-
对于一些想法,我不认为有人想从我这里得到赏金......对于这部分算法,我完全出局了。您可能会认为我的感谢将是您对解决方案的奖励。我会考虑任何建议作为解决方案:D
标签: c# algorithm machine-learning bayesian