📄 bayesnet.java
字号:
iParent < m_ParentSets[iAttribute].GetNrOfParents(); iParent++) { int nParent = m_ParentSets[iAttribute].GetParent(iParent); if (nParent == m_Instances.classIndex()) { iCPT = iCPT * m_NumClasses + iClass; } else { iCPT = iCPT * m_Instances.attribute(nParent).numValues() + instance.value(nParent); } } if (iAttribute == m_Instances.classIndex()) { fCount += ((DiscreteEstimatorBayes)m_Distributions[iAttribute][(int) iCPT]).getCount(iClass); } else { fCount += ((DiscreteEstimatorBayes)m_Distributions[iAttribute][(int) iCPT]).getCount(instance.value(iAttribute)); } } fCounts[iClass] += fCount; } return fCounts; } // countsForInstance /** * Returns an enumeration describing the available options * * @return an enumeration of all the available options */ public Enumeration listOptions() { Vector newVector = new Vector(5); newVector .addElement(new Option("\tScore type (BAYES, MDL, ENTROPY, or AIC)\n", "S", 1, "-S [BAYES|MDL|ENTROPY|AIC]")); newVector.addElement(new Option("\tInitial count (alpha)\n", "A", 1, "-A <alpha>")); newVector .addElement(new Option("\tInitial structure is empty (instead of Naive Bayes)\n", "N", 0, "-N")); newVector .addElement(new Option("\tUse ADTree data structure\n", "D", 0, "-D")); newVector.addElement(new Option("\tMaximum number of parents\n", "P", 1, "-P <nr of parents>")); return newVector.elements(); } // listOptions /** * Parses a given list of options. Valid options are:<p> * * @param options the list of options as an array of strings * @exception Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { m_bInitAsNaiveBayes = !(Utils.getFlag('N', options)); m_bUseADTree = !(Utils.getFlag('D', options)); String sScore = Utils.getOption('S', options); if (sScore.compareTo("BAYES") == 0) { // m_nScoreType = Scoreable.BAYES; setScoreType(new SelectedTag(Scoreable.BAYES, TAGS_SCORE_TYPE)); } if (sScore.compareTo("MDL") == 0) { // m_nScoreType = Scoreable.MDL; setScoreType(new SelectedTag(Scoreable.MDL, TAGS_SCORE_TYPE)); } if (sScore.compareTo("ENTROPY") == 0) { // m_nScoreType = Scoreable.ENTROPY; setScoreType(new SelectedTag(Scoreable.ENTROPY, TAGS_SCORE_TYPE)); } if (sScore.compareTo("AIC") == 0) { // m_nScoreType = Scoreable.AIC; setScoreType(new SelectedTag(Scoreable.AIC, TAGS_SCORE_TYPE)); } String sAlpha = Utils.getOption('A', options); if (sAlpha.length() != 0) { m_fAlpha = (new Float(sAlpha)).floatValue(); } else { m_fAlpha = 0.5f; } String sMaxNrOfParents = Utils.getOption('P', options); if (sMaxNrOfParents.length() != 0) { setMaxNrOfParents(Integer.parseInt(sMaxNrOfParents)); } else { setMaxNrOfParents(100000); } Utils.checkForRemainingOptions(options); } // setOptions /** * Method declaration * * @param scoreType * */ public void setScoreType(SelectedTag newScoreType) { if (newScoreType.getTags() == TAGS_SCORE_TYPE) { m_nScoreType = newScoreType.getSelectedTag().getID(); } } /** * Method declaration * * @return * */ public SelectedTag getScoreType() { return new SelectedTag(m_nScoreType, TAGS_SCORE_TYPE); } /** * Method declaration * * @param fAlpha * */ public void setAlpha(double fAlpha) { m_fAlpha = fAlpha; } /** * Method declaration * * @return * */ public double getAlpha() { return m_fAlpha; } /** * Method declaration * * @param bInitAsNaiveBayes * */ public void setInitAsNaiveBayes(boolean bInitAsNaiveBayes) { m_bInitAsNaiveBayes = bInitAsNaiveBayes; } /** * Method declaration * * @return * */ public boolean getInitAsNaiveBayes() { return m_bInitAsNaiveBayes; } /** * Method declaration * * @param bUseADTree * */ public void setUseADTree(boolean bUseADTree) { m_bUseADTree = bUseADTree; } /** * Method declaration * * @return * */ public boolean getUseADTree() { return m_bUseADTree; } /** * Method declaration * * @param nMaxNrOfParents * */ public void setMaxNrOfParents(int nMaxNrOfParents) { m_nMaxNrOfParents = nMaxNrOfParents; } /** * Method declaration * * @return * */ public int getMaxNrOfParents() { return m_nMaxNrOfParents; } /** * Gets the current settings of the classifier. * * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { String[] options = new String[8]; int current = 0; options[current++] = "-S"; switch (m_nScoreType) { case (Scoreable.BAYES): options[current++] = "BAYES"; break; case (Scoreable.MDL): options[current++] = "MDL"; break; case (Scoreable.ENTROPY): options[current++] = "ENTROPY"; break; case (Scoreable.AIC): options[current++] = "AIC"; break; } options[current++] = "-A"; options[current++] = "" + m_fAlpha; if (!m_bInitAsNaiveBayes) { options[current++] = "-N"; } if (!m_bUseADTree) { options[current++] = "-D"; } if (m_nMaxNrOfParents != 10000) { options[current++] = "-P"; options[current++] = "" + m_nMaxNrOfParents; } // Fill up rest with empty strings, not nulls! while (current < options.length) { options[current++] = ""; } return options; } // getOptions /** * logScore returns the log of the quality of a network * (e.g. the posterior probability of the network, or the MDL * value). * @param nType score type (Bayes, MDL, etc) to calculate score with * @return log score. */ public double logScore(int nType) { if (nType < 0) { nType = m_nScoreType; } double fLogScore = 0.0; for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) { for (int iParent = 0; iParent < m_ParentSets[iAttribute].GetCardinalityOfParents(); iParent++) { fLogScore += ((Scoreable) m_Distributions[iAttribute][iParent]).logScore(nType); } switch (nType) { case (Scoreable.MDL): { fLogScore -= 0.5 * m_ParentSets[iAttribute].GetCardinalityOfParents() * (m_Instances.attribute(iAttribute).numValues() - 1) * Math.log(m_Instances.numInstances()); } break; case (Scoreable.AIC): { fLogScore -= m_ParentSets[iAttribute].GetCardinalityOfParents() * (m_Instances.attribute(iAttribute).numValues() - 1); } break; } } return fLogScore; } // logScore /** * Returns a description of the classifier. * * @return a description of the classifier as a string. */ public String toString() { StringBuffer text = new StringBuffer(); text.append("Bayes Network Classifier"); text.append("\n" + (m_bUseADTree ? "Using " : "not using ") + "ADTree"); if (m_Instances == null) { text.append(": No model built yet."); } else { // TODO: flatten BayesNet down to text text.append("\n#attributes="); text.append(m_Instances.numAttributes()); text.append(" #classindex="); text.append(m_Instances.classIndex()); text.append("\nNetwork structure (nodes followed by parents)\n"); for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) { text.append(m_Instances.attribute(iAttribute).name() + "(" + m_Instances.attribute(iAttribute).numValues() + "): "); for (int iParent = 0; iParent < m_ParentSets[iAttribute].GetNrOfParents(); iParent++) { text .append(m_Instances.attribute(m_ParentSets[iAttribute].GetParent(iParent)).name() + " "); } text.append("\n"); // Description of distributions tends to be too much detail, so it is commented out here // for (int iParent = 0; iParent < m_ParentSets[iAttribute].GetCardinalityOfParents(); iParent++) { // text.append('(' + m_Distributions[iAttribute][iParent].toString() + ')'); // } // text.append("\n"); } text.append("LogScore Bayes: " + logScore(Scoreable.BAYES) + "\n"); text.append("LogScore MDL: " + logScore(Scoreable.MDL) + "\n"); text.append("LogScore ENTROPY: " + logScore(Scoreable.ENTROPY) + "\n"); text.append("LogScore AIC: " + logScore(Scoreable.AIC) + "\n"); String[] options = getOptions(); for (int iOption = 0; iOption < 1; iOption++) { text.append(options[iOption]); } } return text.toString(); } // toString /** * Calc Node Score With AddedParent * * @param nNode node for which the score is calculate * @param nCandidateParent candidate parent to add to the existing parent set * @return log score */ protected double CalcScoreWithExtraParent(int nNode, int nCandidateParent) { // sanity check: nCandidateParent should not be in parent set already for (int iParent = 0; iParent < m_ParentSets[nNode].GetNrOfParents(); iParent++) { if (m_ParentSets[nNode].GetParent(iParent) == nCandidateParent) { return -1e100;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -