📄 bayesnet.java
字号:
* for details on XML BIF.
* @return an XML BIF 0.3 description of the classifier as a string.
*/
public String toXMLBIF03() {
if (m_Instances == null) {
return ("<!--No model built yet-->");
}
StringBuffer text = new StringBuffer();
text.append("<?xml version='0.1'?>\n");
text.append("<!-- DTD for the XMLBIF 0.3 format -->\n");
text.append("<!DOCTYPE BIF [\n");
text.append(" <!ELEMENT BIF ( NETWORK )*>\n");
text.append(" <!ATTLIST BIF VERSION CDATA #REQUIRED>\n");
text.append(" <!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFINITION )* )>\n");
text.append(" <!ELEMENT NAME (#PCDATA)>\n");
text.append(" <!ELEMENT VARIABLE ( NAME, ( OUTCOME | PROPERTY )* ) >\n");
text.append(" <!ATTLIST VARIABLE TYPE (nature|decision|utility) \"nature\">\n");
text.append(" <!ELEMENT OUTCOME (#PCDATA)>\n");
text.append(" <!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY )* >\n");
text.append(" <!ELEMENT FOR (#PCDATA)>\n");
text.append(" <!ELEMENT GIVEN (#PCDATA)>\n");
text.append(" <!ELEMENT TABLE (#PCDATA)>\n");
text.append(" <!ELEMENT PROPERTY (#PCDATA)>\n");
text.append("]>\n");
text.append("\n");
text.append("\n");
text.append("<BIF VERSION=\"0.3\">\n");
text.append("<NETWORK>\n");
text.append("<NAME>" + m_Instances.relationName() + "</NAME>\n");
for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) {
text.append("<VARIABLE TYPE='nature'>\n");
text.append("<NAME>" + m_Instances.attribute(iAttribute).name() + "</NAME>\n");
for (int iValue = 0; iValue < m_Instances.attribute(iAttribute).numValues(); iValue++) {
text.append("<OUTCOME>" + m_Instances.attribute(iAttribute).value(iValue) + "</OUTCOME>\n");
}
text.append("</VARIABLE>\n");
}
for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) {
text.append("<DEFINITION>\n");
text.append("<FOR>" + m_Instances.attribute(iAttribute).name() + "</FOR>\n");
for (int iParent = 0; iParent < m_ParentSets[iAttribute].getNrOfParents(); iParent++) {
text.append(
"<GIVEN>"
+ m_Instances.attribute(m_ParentSets[iAttribute].getParent(iParent)).name()
+ "</GIVEN>\n");
}
text.append("<TABLE>\n");
for (int iParent = 0; iParent < m_ParentSets[iAttribute].getCardinalityOfParents(); iParent++) {
for (int iValue = 0; iValue < m_Instances.attribute(iAttribute).numValues(); iValue++) {
text.append(m_Distributions[iAttribute][iParent].getProbability(iValue));
text.append(' ');
}
text.append('\n');
}
text.append("</TABLE>\n");
text.append("</DEFINITION>\n");
}
text.append("</NETWORK>\n");
text.append("</BIF>\n");
return text.toString();
} // toXMLBIF03
/**
* @return a string to describe the UseADTreeoption.
*/
public String useADTreeTipText() {
return "When ADTree (the data structure for increasing speed on counts,"
+ " not to be confused with the classifier under the same name) is used"
+ " learning time goes down typically. However, because ADTrees are memory"
+ " intensive, memory problems may occur. Switching this option off makes"
+ " the structure learning algorithms slower, and run with less memory."
+ " By default, ADTrees are used.";
}
/**
* @return a string to describe the SearchAlgorithm.
*/
public String searchAlgorithmTipText() {
return "Select method used for searching network structures.";
}
/**
* This will return a string describing the BayesNetEstimator.
* @return The string.
*/
public String estimatorTipText() {
return "Select Estimator algorithm for finding the conditional probability tables"
+ " of the Bayes Network.";
}
/**
* @return a string to describe the BIFFile.
*/
public String BIFFileTipText() {
return "Set the name of a file in BIF XML format. A Bayes network learned"
+ " from data can be compared with the Bayes network represented by the BIF file."
+ " Statistics calculated are o.a. the number of missing and extra arcs.";
}
/**
* This will return a string describing the classifier.
* @return The string.
*/
public String globalInfo() {
return "Bayes Network learning using various search algorithms and quality measures.";
}
/**
* Main method for testing this class.
*
* @param argv the options
*/
public static void main(String[] argv) {
try {
System.out.println(Evaluation.evaluateModel(new BayesNet(), argv));
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
} // main
/** get name of the Bayes network
* @return name of the Bayes net
*/
public String getName() {
return m_Instances.relationName();
}
/** get number of nodes in the Bayes network
* @return number of nodes
*/
public int getNrOfNodes() {
return m_Instances.numAttributes();
}
/** get name of a node in the Bayes network
* @param iNode: index of the node
* @return name of the specified node
*/
public String getNodeName(int iNode) {
return m_Instances.attribute(iNode).name();
}
/** get number of values a node can take
* @param iNode: index of the node
* @return cardinality of the specified node
*/
public int getCardinality(int iNode) {
return m_Instances.attribute(iNode).numValues();
}
/** get name of a particular value of a node
* @param iNode: index of the node
* @param iValue: index of the value
* @return cardinality of the specified node
*/
public String getNodeValue(int iNode, int iValue) {
return m_Instances.attribute(iNode).value(iValue);
}
/** get number of parents of a node in the network structure
* @param iNode: index of the node
* @return number of parents of the specified node
*/
public int getNrOfParents(int iNode) {
return m_ParentSets[iNode].getNrOfParents();
}
/** get node index of a parent of a node in the network structure
* @param iNode: index of the node
* @param iParent: index of the parents, e.g., 0 is the first parent, 1 the second parent, etc.
* @return node index of the iParent's parent of the specified node
*/
public int getParent(int iNode, int iParent) {
return m_ParentSets[iNode].getParent(iParent);
}
/** Get full set of parent sets.
* @return parent sets;
*/
public ParentSet[] getParentSets() {
return m_ParentSets;
}
/** Get full set of estimators.
* @return estimators;
*/
public Estimator[][] getDistributions() {
return m_Distributions;
}
/** get number of values the collection of parents of a node can take
* @param iNode: index of the node
* @return cardinality of the parent set of the specified node
*/
public int getParentCardinality(int iNode) {
return m_ParentSets[iNode].getCardinalityOfParents();
}
/** get particular probability of the conditional probability distribtion
* of a node given its parents.
* @param iNode: index of the node
* @param iParent: index of the parent set, 0 <= iParent <= getParentCardinality(iNode)
* @param iValue: index of the value, 0 <= iValue <= getCardinality(iNode)
* @return probability
*/
public double getProbability(int iNode, int iParent, int iValue) {
return m_Distributions[iNode][iParent].getProbability(iValue);
}
/** get the parent set of a node
* @param iNode: index of the node
* @return Parent set of the specified node.
*/
public ParentSet getParentSet(int iNode) {
return m_ParentSets[iNode];
}
/** get ADTree strucrture containing efficient representation of counts.
* @return ADTree strucrture
*/
public ADNode getADTree() { return m_ADTree;}
// implementation of AdditionalMeasureProducer interface
/**
* Returns an enumeration of the measure names. Additional measures
* must follow the naming convention of starting with "measure", eg.
* double measureBlah()
* @return an enumeration of the measure names
*/
public Enumeration<String> emerateMeasures() {
Vector<String> newVector = new Vector<String>(4);
newVector.addElement("measureExtraArcs");
newVector.addElement("measureMissingArcs");
newVector.addElement("measureReversedArcs");
newVector.addElement("measureDivergence");
newVector.addElement("measureBayesScore");
newVector.addElement("measureBDeuScore");
newVector.addElement("measureMDLScore");
newVector.addElement("measureAICScore");
newVector.addElement("measureEntropyScore");
return newVector.elements();
} // emerateMeasures
public double measureExtraArcs() {
if (m_otherBayesNet != null) {
return m_otherBayesNet.extraArcs(this);
}
return 0;
} // measureExtraArcs
public double measureMissingArcs() {
if (m_otherBayesNet != null) {
return m_otherBayesNet.missingArcs(this);
}
return 0;
} // measureMissingArcs
public double measureReversedArcs() {
if (m_otherBayesNet != null) {
return m_otherBayesNet.reversedArcs(this);
}
return 0;
} // measureReversedArcs
public double measureDivergence() {
if (m_otherBayesNet != null) {
return m_otherBayesNet.divergence(this);
}
return 0;
} // measureDivergence
public double measureBayesScore() {
LocalScoreSearchAlgorithm s = new LocalScoreSearchAlgorithm(this, m_Instances);
return s.logScore(Scoreable.BAYES);
} // measureBayesScore
public double measureBDeuScore() {
LocalScoreSearchAlgorithm s = new LocalScoreSearchAlgorithm(this, m_Instances);
return s.logScore(Scoreable.BDeu);
} // measureBDeuScore
public double measureMDLScore() {
LocalScoreSearchAlgorithm s = new LocalScoreSearchAlgorithm(this, m_Instances);
return s.logScore(Scoreable.MDL);
} // measureMDLScore
public double measureAICScore() {
LocalScoreSearchAlgorithm s = new LocalScoreSearchAlgorithm(this, m_Instances);
return s.logScore(Scoreable.AIC);
} // measureAICScore
public double measureEntropyScore() {
LocalScoreSearchAlgorithm s = new LocalScoreSearchAlgorithm(this, m_Instances);
return s.logScore(Scoreable.ENTROPY);
} // measureEntropyScore
/**
* Returns the value of the named measure
* @param measureName the name of the measure to query for its value
* @return the value of the named measure
* @exception IllegalArgumentException if the named measure is not supported
*/
public double getMeasure(String measureName) {
if (measureName.equals("measureExtraArcs")) {
return measureExtraArcs();
}
if (measureName.equals("measureMissingArcs")) {
return measureMissingArcs();
}
if (measureName.equals("measureReversedArcs")) {
return measureReversedArcs();
}
if (measureName.equals("measureDivergence")) {
return measureDivergence();
}
if (measureName.equals("measureBayesScore")) {
return measureBayesScore();
}
if (measureName.equals("measureBDeuScore")) {
return measureBDeuScore();
}
if (measureName.equals("measureMDLScore")) {
return measureMDLScore();
}
if (measureName.equals("measureAICScore")) {
return measureAICScore();
}
if (measureName.equals("measureEntropyScore")) {
return measureEntropyScore();
}
return 0;
} // getMeasure
} // class BayesNet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -