Package weka.classifiers.bayes
Class BayesNet
- java.lang.Object
-
- weka.classifiers.Classifier
-
- weka.classifiers.bayes.BayesNet
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,AdditionalMeasureProducer,CapabilitiesHandler,Drawable,OptionHandler,RevisionHandler,WeightedInstancesHandler
- Direct Known Subclasses:
BIFReader,EditableBayesNet
public class BayesNet extends Classifier implements OptionHandler, WeightedInstancesHandler, Drawable, AdditionalMeasureProducer
Bayes Network learning using various search algorithms and quality measures.
Base class for a Bayes Network classifier. Provides datastructures (network structure, conditional probability distributions, etc.) and facilities common to Bayes Network learning algorithms like K2 and B.
For more information see:
http://sourceforge.net/projects/weka/files/documentation/WekaManual-3-6-1.pdf/download Valid options are:-D Do not use ADTree data structure
-B <BIF file> BIF file to compare with
-Q weka.classifiers.bayes.net.search.SearchAlgorithm Search algorithm
-E weka.classifiers.bayes.net.estimate.SimpleEstimator Estimator algorithm
- Version:
- $Revision: 5725 $
- Author:
- Remco Bouckaert (rrb@xm.co.nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description Estimator[][]m_DistributionsThe attribute estimators containing CPTs.Instancesm_InstancesThe dataset header for the purposes of printing out a semi-intelligible model-
Fields inherited from interface weka.core.Drawable
BayesNet, Newick, NOT_DRAWABLE, TREE
-
-
Constructor Summary
Constructors Constructor Description BayesNet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringBIFFileTipText()voidbuildClassifier(Instances instances)Generates the classifier.voidbuildStructure()buildStructure determines the network structure/graph of the network.double[]countsForInstance(Instance instance)Calculates the counts for Dirichlet distribution for the class membership probabilities for the given test instance.double[]distributionForInstance(Instance instance)Calculates the class membership probabilities for the given test instance.java.util.EnumerationenumerateMeasures()Returns an enumeration of the measure names.voidestimateCPTs()estimateCPTs estimates the conditional probability tables for the Bayes Net using the network structure.java.lang.StringestimatorTipText()This will return a string describing the BayesNetEstimator.ADNodegetADTree()get ADTree strucrture containing efficient representation of counts.java.lang.StringgetBIFFile()Get name of network in BIF file to compare withjava.lang.StringgetBIFHeader()CapabilitiesgetCapabilities()Returns default capabilities of the classifier.intgetCardinality(int iNode)get number of values a node can takeEstimator[][]getDistributions()Get full set of estimators.BayesNetEstimatorgetEstimator()Get the BayesNetEstimator used for calculating the CPTsdoublegetMeasure(java.lang.String measureName)Returns the value of the named measurejava.lang.StringgetName()get name of the Bayes networkjava.lang.StringgetNodeName(int iNode)get name of a node in the Bayes networkjava.lang.StringgetNodeValue(int iNode, int iValue)get name of a particular value of a nodeintgetNrOfNodes()get number of nodes in the Bayes networkintgetNrOfParents(int iNode)get number of parents of a node in the network structurejava.lang.String[]getOptions()Gets the current settings of the classifier.intgetParent(int iNode, int iParent)get node index of a parent of a node in the network structureintgetParentCardinality(int iNode)get number of values the collection of parents of a node can takeParentSetgetParentSet(int iNode)get the parent set of a nodeParentSet[]getParentSets()Get full set of parent sets.doublegetProbability(int iNode, int iParent, int iValue)get particular probability of the conditional probability distribtion of a node given its parents.java.lang.StringgetRevision()Returns the revision string.SearchAlgorithmgetSearchAlgorithm()Get the SearchAlgorithm used as the search algorithmbooleangetUseADTree()Method declarationjava.lang.StringglobalInfo()This will return a string describing the classifier.java.lang.Stringgraph()Returns a BayesNet graph in XMLBIF ver 0.3 format.intgraphType()Returns the type of graph this classifier represents.voidinitCPTs()initializes the conditional probabilitiesvoidinitStructure()Init structure initializes the structure to an empty graph or a Naive Bayes graph (depending on the -N flag).java.util.EnumerationlistOptions()Returns an enumeration describing the available optionsstatic voidmain(java.lang.String[] argv)Main method for testing this class.doublemeasureAICScore()doublemeasureBayesScore()doublemeasureBDeuScore()doublemeasureDivergence()doublemeasureEntropyScore()doublemeasureExtraArcs()doublemeasureMDLScore()doublemeasureMissingArcs()doublemeasureReversedArcs()static java.lang.String[]partitionOptions(java.lang.String[] options)Returns the secondary set of options (if any) contained in the supplied options array.java.lang.StringsearchAlgorithmTipText()voidsetBIFFile(java.lang.String sBIFFile)Set name of network in BIF file to compare withvoidsetEstimator(BayesNetEstimator newBayesNetEstimator)Set the Estimator Algorithm used in calculating the CPTsvoidsetOptions(java.lang.String[] options)Parses a given list of options.voidsetSearchAlgorithm(SearchAlgorithm newSearchAlgorithm)Set the SearchAlgorithm used in searching for network structures.voidsetUseADTree(boolean bUseADTree)Set whether ADTree structure is used or notjava.lang.StringtoString()Returns a description of the classifier.java.lang.StringtoXMLBIF03()Returns a description of the classifier in XML BIF 0.3 format.voidupdateClassifier(Instance instance)Updates the classifier with the given instance.java.lang.StringuseADTreeTipText()-
Methods inherited from class weka.classifiers.Classifier
classifyInstance, debugTipText, forName, getDebug, makeCopies, makeCopy, setDebug
-
-
-
-
Method Detail
-
getCapabilities
public Capabilities getCapabilities()
Returns default capabilities of the classifier.- Specified by:
getCapabilitiesin interfaceCapabilitiesHandler- Overrides:
getCapabilitiesin classClassifier- Returns:
- the capabilities of this classifier
- See Also:
Capabilities
-
buildClassifier
public void buildClassifier(Instances instances) throws java.lang.Exception
Generates the classifier.- Specified by:
buildClassifierin classClassifier- Parameters:
instances- set of instances serving as training data- Throws:
java.lang.Exception- if the classifier has not been generated successfully
-
initStructure
public void initStructure() throws java.lang.ExceptionInit structure initializes the structure to an empty graph or a Naive Bayes graph (depending on the -N flag).- Throws:
java.lang.Exception- in case of an error
-
buildStructure
public void buildStructure() throws java.lang.ExceptionbuildStructure 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 like a naive Bayes classifier). This method can be overridden by derived classes to restrict the class of network structures that are acceptable.- Throws:
java.lang.Exception- in case of an error
-
estimateCPTs
public void estimateCPTs() throws java.lang.ExceptionestimateCPTs estimates the conditional probability tables for the Bayes Net using the network structure.- Throws:
java.lang.Exception- in case of an error
-
initCPTs
public void initCPTs() throws java.lang.Exceptioninitializes the conditional probabilities- Throws:
java.lang.Exception- in case of an error
-
updateClassifier
public void updateClassifier(Instance instance) throws java.lang.Exception
Updates the classifier with the given instance.- Parameters:
instance- the new training instance to include in the model- Throws:
java.lang.Exception- if the instance could not be incorporated in the model.
-
distributionForInstance
public double[] distributionForInstance(Instance instance) throws java.lang.Exception
Calculates the class membership probabilities for the given test instance.- Overrides:
distributionForInstancein classClassifier- Parameters:
instance- the instance to be classified- Returns:
- predicted class probability distribution
- Throws:
java.lang.Exception- if there is a problem generating the prediction
-
countsForInstance
public double[] countsForInstance(Instance instance) throws java.lang.Exception
Calculates the counts for Dirichlet distribution for the class membership probabilities for the given test instance.- Parameters:
instance- the instance to be classified- Returns:
- counts for Dirichlet distribution for class probability
- Throws:
java.lang.Exception- if there is a problem generating the prediction
-
listOptions
public java.util.Enumeration listOptions()
Returns an enumeration describing the available options- Specified by:
listOptionsin interfaceOptionHandler- Overrides:
listOptionsin classClassifier- Returns:
- an enumeration of all the available options
-
setOptions
public void setOptions(java.lang.String[] options) throws java.lang.ExceptionParses a given list of options.Valid options are:
-D Do not use ADTree data structure
-B <BIF file> BIF file to compare with
-Q weka.classifiers.bayes.net.search.SearchAlgorithm Search algorithm
-E weka.classifiers.bayes.net.estimate.SimpleEstimator Estimator algorithm
- Specified by:
setOptionsin interfaceOptionHandler- Overrides:
setOptionsin classClassifier- Parameters:
options- the list of options as an array of strings- Throws:
java.lang.Exception- if an option is not supported
-
partitionOptions
public static java.lang.String[] partitionOptions(java.lang.String[] options)
Returns the secondary set of options (if any) contained in the supplied options array. The secondary set is defined to be any options after the first "--" but before the "-E". These options are removed from the original options array.- Parameters:
options- the input array of options- Returns:
- the array of secondary options
-
getOptions
public java.lang.String[] getOptions()
Gets the current settings of the classifier.- Specified by:
getOptionsin interfaceOptionHandler- Overrides:
getOptionsin classClassifier- Returns:
- an array of strings suitable for passing to setOptions
-
setSearchAlgorithm
public void setSearchAlgorithm(SearchAlgorithm newSearchAlgorithm)
Set the SearchAlgorithm used in searching for network structures.- Parameters:
newSearchAlgorithm- the SearchAlgorithm to use.
-
getSearchAlgorithm
public SearchAlgorithm getSearchAlgorithm()
Get the SearchAlgorithm used as the search algorithm- Returns:
- the SearchAlgorithm used as the search algorithm
-
setEstimator
public void setEstimator(BayesNetEstimator newBayesNetEstimator)
Set the Estimator Algorithm used in calculating the CPTs- Parameters:
newBayesNetEstimator- the Estimator to use.
-
getEstimator
public BayesNetEstimator getEstimator()
Get the BayesNetEstimator used for calculating the CPTs- Returns:
- the BayesNetEstimator used.
-
setUseADTree
public void setUseADTree(boolean bUseADTree)
Set whether ADTree structure is used or not- Parameters:
bUseADTree- true if an ADTree structure is used
-
getUseADTree
public boolean getUseADTree()
Method declaration- Returns:
- whether ADTree structure is used or not
-
setBIFFile
public void setBIFFile(java.lang.String sBIFFile)
Set name of network in BIF file to compare with- Parameters:
sBIFFile- the name of the BIF file
-
getBIFFile
public java.lang.String getBIFFile()
Get name of network in BIF file to compare with- Returns:
- BIF file name
-
toString
public java.lang.String toString()
Returns a description of the classifier.- Overrides:
toStringin classjava.lang.Object- Returns:
- a description of the classifier as a string.
-
graphType
public int graphType()
Returns the type of graph this classifier represents.
-
graph
public java.lang.String graph() throws java.lang.ExceptionReturns a BayesNet graph in XMLBIF ver 0.3 format.
-
getBIFHeader
public java.lang.String getBIFHeader()
-
toXMLBIF03
public java.lang.String toXMLBIF03()
Returns a description of the classifier in XML BIF 0.3 format. See http://www-2.cs.cmu.edu/~fgcozman/Research/InterchangeFormat/ for details on XML BIF.- Returns:
- an XML BIF 0.3 description of the classifier as a string.
-
useADTreeTipText
public java.lang.String useADTreeTipText()
- Returns:
- a string to describe the UseADTreeoption.
-
searchAlgorithmTipText
public java.lang.String searchAlgorithmTipText()
- Returns:
- a string to describe the SearchAlgorithm.
-
estimatorTipText
public java.lang.String estimatorTipText()
This will return a string describing the BayesNetEstimator.- Returns:
- The string.
-
BIFFileTipText
public java.lang.String BIFFileTipText()
- Returns:
- a string to describe the BIFFile.
-
globalInfo
public java.lang.String globalInfo()
This will return a string describing the classifier.- Returns:
- The string.
-
main
public static void main(java.lang.String[] argv)
Main method for testing this class.- Parameters:
argv- the options
-
getName
public java.lang.String getName()
get name of the Bayes network- Returns:
- name of the Bayes net
-
getNrOfNodes
public int getNrOfNodes()
get number of nodes in the Bayes network- Returns:
- number of nodes
-
getNodeName
public java.lang.String getNodeName(int iNode)
get name of a node in the Bayes network- Parameters:
iNode- index of the node- Returns:
- name of the specified node
-
getCardinality
public int getCardinality(int iNode)
get number of values a node can take- Parameters:
iNode- index of the node- Returns:
- cardinality of the specified node
-
getNodeValue
public java.lang.String getNodeValue(int iNode, int iValue)get name of a particular value of a node- Parameters:
iNode- index of the nodeiValue- index of the value- Returns:
- cardinality of the specified node
-
getNrOfParents
public int getNrOfParents(int iNode)
get number of parents of a node in the network structure- Parameters:
iNode- index of the node- Returns:
- number of parents of the specified node
-
getParent
public int getParent(int iNode, int iParent)get node index of a parent of a node in the network structure- Parameters:
iNode- index of the nodeiParent- index of the parents, e.g., 0 is the first parent, 1 the second parent, etc.- Returns:
- node index of the iParent's parent of the specified node
-
getParentSets
public ParentSet[] getParentSets()
Get full set of parent sets.- Returns:
- parent sets;
-
getDistributions
public Estimator[][] getDistributions()
Get full set of estimators.- Returns:
- estimators;
-
getParentCardinality
public int getParentCardinality(int iNode)
get number of values the collection of parents of a node can take- Parameters:
iNode- index of the node- Returns:
- cardinality of the parent set of the specified node
-
getProbability
public double getProbability(int iNode, int iParent, int iValue)get particular probability of the conditional probability distribtion of a node given its parents.- Parameters:
iNode- index of the nodeiParent- index of the parent set, 0 <= iParent <= getParentCardinality(iNode)iValue- index of the value, 0 <= iValue <= getCardinality(iNode)- Returns:
- probability
-
getParentSet
public ParentSet getParentSet(int iNode)
get the parent set of a node- Parameters:
iNode- index of the node- Returns:
- Parent set of the specified node.
-
getADTree
public ADNode getADTree()
get ADTree strucrture containing efficient representation of counts.- Returns:
- ADTree strucrture
-
enumerateMeasures
public java.util.Enumeration enumerateMeasures()
Returns an enumeration of the measure names. Additional measures must follow the naming convention of starting with "measure", eg. double measureBlah()- Specified by:
enumerateMeasuresin interfaceAdditionalMeasureProducer- Returns:
- an enumeration of the measure names
-
measureExtraArcs
public double measureExtraArcs()
-
measureMissingArcs
public double measureMissingArcs()
-
measureReversedArcs
public double measureReversedArcs()
-
measureDivergence
public double measureDivergence()
-
measureBayesScore
public double measureBayesScore()
-
measureBDeuScore
public double measureBDeuScore()
-
measureMDLScore
public double measureMDLScore()
-
measureAICScore
public double measureAICScore()
-
measureEntropyScore
public double measureEntropyScore()
-
getMeasure
public double getMeasure(java.lang.String measureName)
Returns the value of the named measure- Specified by:
getMeasurein interfaceAdditionalMeasureProducer- Parameters:
measureName- the name of the measure to query for its value- Returns:
- the value of the named measure
- Throws:
java.lang.IllegalArgumentException- if the named measure is not supported
-
getRevision
public java.lang.String getRevision()
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Overrides:
getRevisionin classClassifier- Returns:
- the revision
-
-