Weka中贝叶斯网络学习情况小结.docx
《Weka中贝叶斯网络学习情况小结.docx》由会员分享,可在线阅读,更多相关《Weka中贝叶斯网络学习情况小结.docx(31页珍藏版)》请在三一办公上搜索。
1、Weka中贝叶斯网络学习情况小结Weka中贝叶斯网络学习情况小结 Weka中对于贝叶斯网络的学习,仅仅看相关的几个包几乎是不可能的,结果还是一叶障目不见泰山。 后来发现就那些代码死磕根本不行,还得采取灵活的方式方法。一方面采用学习人家博客的总结,逐步梳理weka中那些类的组织和功能函数的情况。一方面下载了一个weka源代码分析的中文翻译包,能够从更加宽的领域理解BN的相关重要函数以及操作。 随便找个类,比如instance和instances,比如evaluation,都是几百行,或者千余行,通读一遍都很费力,而且许多类有继承,许多地方用的是接口,很少有地方用抽象类。各个类之间的关联较多,随便
2、引用一下,在某个函数中出现一下,都是关系,本来以为UML图能够方便的解决问题,但是拖动以后发现,事情越高越多,线条也越来越多,实际上展示效力快速下降,到最后还是不能够说明问题。 刚开始研究原理,其实朴素贝叶斯和贝叶斯网络的基本原理并不复杂,小型网络手算是可以的,有点像矩阵,小矩阵加减乘除都没问题,但是大型矩阵就得想方设法用计算机、写代码来实现了。 数学上涉及的东西,写道计算机上就需要一些辅助的东西了。特别是程序实现上。事实上,学习概论的东西本身也要小心,一不留神还是会犯下各种错误。在软件实现上,读入arff数据,构建ADTree,然后训练分类器,最后进行测试。 An alternating d
3、ecision tree (ADTree) is a machine learning method for classification. It generalizes decision trees and has connections to boosting. An alternating decision tree consists of decision nodes and prediction nodes. Decision nodes specify a predicate condition. Prediction nodes contain a single number.
4、ADTrees always have prediction nodes as both root and leaves. An instance is classified by an ADTree by following all paths for which all decision nodes are true and summing any prediction nodes that are traversed. This is different from binary classification trees such as CART (Classification and r
5、egression tree) or C4.5 in which an instance follows only one path through the tree. http:/en.wikipedia.org/wiki/ADTree这个网站给的例子可以好好理解一下、 weka在进行实例测试的时候也有许多术语和内容需要继续认识,比如recall,precision,confusion matrix,以及其他指标。 评分函数,在软件中归在evaluation中。CPT,也就是conditional probability table也是建立网络的关键。有一个说法:Bays = Bs(DAG)
6、+Bp(CPT) weka 中的Beiyes有两个要求,一个是离散化的数据,另一个是数据的值不能是null 整个学习的过程是先构建DAG在学习出CPT。 记录一点代码分析: 在buildClassifier函数中,重要的几行是: / build the network structure initStructure; / build the network structure buildStructure; / build the set of CPTs estimateCPTs; 函数initStructure为初始化网络结构,buildStructure为构造网络结构,estimateCP
7、Ts为计算条件概率表(conditional probability table)。 /* * Init structure initializes the structure to an empty graph * or a Naive Bayes graph (depending on the -N flag). */ public void initStructure throws Exception / reserve memory m_ParentSets = new ParentSetm_Instances.numAttributes; for (int iAttribute =
8、0; iAttribute m_Instances.numAttributes; iAttribute+) m_ParentSetsiAttribute = new ParentSet(m_Instances .numAttributes); / initStructure m_ParentSets是记录下第i个属性(iAttribute)的父结点,ParentSet初始函数为: public ParentSet(int nMaxNrOfParents) m_nParents = new intnMaxNrOfParents; m_nNrOfParents = 0; m_nCardinalit
9、yOfParents = 1; / ParentSet 不做什么,也就是一个空图。 接下来看buildStructure,它会调用SearchAlgorithm中的buildStructure: /* * buildStructure determines the network structure/graph of the network. * The default behavior is creating a network where all nodes have * the first node as its parent (i.e., a BayesNet that behaves
10、 like * a naive Bayes classifier). This method can be overridden by derived * classes to restrict the class of network structures that are acceptable. */ public void buildStructure(BayesNet bayesNet, Instances instances) throws Exception if (m_bInitAsNaiveBayes) int iClass = instances.classIndex; /
11、initialize parent sets to have arrow from classifier node to / each of the other nodes for (int iAttribute = 0; iAttribute instances.numAttributes; iAttribute+) if (iAttribute != iClass) bayesNet.getParentSet(iAttribute).addParent(iClass, instances); search(bayesNet, instances); if (m_bMarkovBlanket
12、Classifier) doMarkovBlanketCorrection(bayesNet, instances); / buildStructure 这里会判断是不是初始化成朴素贝叶斯,如果不初始化为朴素贝叶斯,那么就还是空图,如果初始为朴素贝叶斯,则对于每个属性将类别属性加为父结点。addParent的代码如下: public void addParent(int nParent, Instances _Instances) if (m_nNrOfParents = 10) / reserve more memory int nParents = new int50; for (int
13、i = 0; i m_nNrOfParents; i+) nParentsi = m_nParentsi; m_nParents = nParents; m_nParentsm_nNrOfParents = nParent; m_nNrOfParents+; m_nCardinalityOfParents *= _Instances.attribute(nParent) .numValues; / AddParent 前面的if是预保留内存的代码,后面的是保存哪个属性是它的父结点,m_NrOfParent是父结点数,CardinalityOfParents是父结点所能取的所有属性值之和。 se
14、arch函数的实现有很多,这里看K2的代码实现: int nOrder = new intinstances.numAttributes; nOrder0 = instances.classIndex; int nAttribute = 0; for (int iOrder = 1; iOrder instances.numAttributes; iOrder+) if (nAttribute = instances.classIndex) nAttribute+; nOrderiOrder = nAttribute+; nOrder中类别属性下标为0,其实它属性顺序还是一样的。 / dete
15、rmine base scores double fBaseScores = new doubleinstances.numAttributes; for (int iOrder = 0; iOrder instances.numAttributes; iOrder+) int iAttribute = nOrderiOrder; fBaseScoresiAttribute = calcNodeScore(iAttribute); 计算base scores,调用calcNodeScore函数: public double calcNodeScore(int nNode) if (m_Baye
16、sNet.getUseADTree & m_BayesNet.getADTree != null) return calcNodeScoreADTree(nNode); else return calcNodeScorePlain(nNode); ADTree就暂时不去理会了,看calcNodeScorePlain函数: / estimate distributions Enumeration enumInsts = instances.enumerateInstances; while (enumInsts.hasMoreElements) Instance instance = (Inst
17、ance) enumInsts.nextElement; / updateClassifier; double iCPT = 0; for (int iParent = 0; iParent oParentSet.getNrOfParents; iParent+) int nParent = oParentSet.getParent(iParent); iCPT = iCPT * instances.attribute(nParent).numValues + instance.value(nParent); nCountsnumValues * (int) iCPT) + (int) ins
18、tance.value(nNode)+; 这里的nCounts是文章Bayesian Network Classifiers in Weka中第4页所提到的Nijk,这里是将i,j,k三维放到了一些,类别值是最后的instance.value(nNode)。 在calcNodeScorePlain函数中最后调用了calcScoreOfCount函数: for (int iParent = 0; iParent nCardinality; iParent+) switch (m_nScoreType) case (Scoreable.BAYES): double nSumOfCounts = 0
19、; for (int iSymbol = 0; iSymbol numValues; iSymbol+) if (m_fAlpha + nCountsiParent * numValues + iSymbol != 0) fLogScore += Statistics.lnGamma(m_fAlpha + nCountsiParent * numValues + iSymbol); nSumOfCounts += m_fAlpha + nCountsiParent * numValues + iSymbol; if (nSumOfCounts != 0) fLogScore -= Statis
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Weka 中贝叶斯 网络 学习 情况 小结
链接地址:https://www.31ppt.com/p-3168930.html