Package weka.classifiers.bayes
Class BayesNet
java.lang.Object
weka.classifiers.AbstractClassifier
weka.classifiers.bayes.BayesNet
- All Implemented Interfaces:
Serializable,Cloneable,Classifier,AdditionalMeasureProducer,BatchPredictor,CapabilitiesHandler,CapabilitiesIgnorer,CommandlineRunnable,Drawable,OptionHandler,RevisionHandler,WeightedInstancesHandler
- Direct Known Subclasses:
BIFReader,EditableBayesNet
public class BayesNet
extends AbstractClassifier
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-7-0.pdf /download Valid options are:
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-7-0.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: 15520 $
- Author:
- Remco Bouckaert (rrb@xm.co.nz)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionEstimator[][]The attribute estimators containing CPTs.The dataset header for the purposes of printing out a semi-intelligible modelFields inherited from class weka.classifiers.AbstractClassifier
BATCH_SIZE_DEFAULT, NUM_DECIMAL_PLACES_DEFAULTFields inherited from interface weka.core.Drawable
BayesNet, Newick, NOT_DRAWABLE, TREE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbuildClassifier(Instances instances) Generates the classifier.voidbuildStructure 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.Returns an enumeration of the measure names.voidestimateCPTs estimates the conditional probability tables for the Bayes Net using the network structure.This will return a string describing the BayesNetEstimator.get ADTree strucrture containing efficient representation of counts.Get name of network in BIF file to compare withReturns default capabilities of the classifier.intgetCardinality(int iNode) get number of values a node can takeEstimator[][]Get full set of estimators.Get the BayesNetEstimator used for calculating the CPTsdoublegetMeasure(String measureName) Returns the value of the named measuregetName()get name of the Bayes networkgetNodeName(int iNode) get name of a node in the Bayes networkgetNodeValue(int iNode, int iValue) get name of a particular value of a nodeintget number of nodes in the Bayes networkintgetNrOfParents(int iNode) get number of parents of a node in the network structureintReturns the number of instances the model was built from.String[]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 takegetParentSet(int iNode) get the parent set of a nodeGet 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.Returns the revision string.Get the SearchAlgorithm used as the search algorithmbooleanMethod declarationThis will return a string describing the classifier.graph()Returns a BayesNet graph in XMLBIF ver 0.3 format.intReturns the type of graph this classifier represents.voidinitCPTs()initializes the conditional probabilitiesvoidInit structure initializes the structure to an empty graph or a Naive Bayes graph (depending on the -N flag).Returns an enumeration describing the available optionsstatic voidMain method for testing this class.doubledoubledoubledoubledoubledoubledoubledoubledoublestatic String[]partitionOptions(String[] options) Returns the secondary set of options (if any) contained in the supplied options array.voidsetBIFFile(String sBIFFile) Set name of network in BIF file to compare withvoidsetEstimator(BayesNetEstimator newBayesNetEstimator) Set the Estimator Algorithm used in calculating the CPTsvoidsetOptions(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 nottoString()Returns a description of the classifier.Returns a description of the classifier in XML BIF 0.3 format.voidupdateClassifier(Instance instance) Updates the classifier with the given instance.Methods inherited from class weka.classifiers.AbstractClassifier
batchSizeTipText, classifyInstance, debugTipText, distributionsForInstances, doNotCheckCapabilitiesTipText, forName, getBatchSize, getDebug, getDoNotCheckCapabilities, getNumDecimalPlaces, implementsMoreEfficientBatchPrediction, makeCopies, makeCopy, numDecimalPlacesTipText, postExecution, preExecution, run, runClassifier, setBatchSize, setDebug, setDoNotCheckCapabilities, setNumDecimalPlaces
-
Field Details
-
m_Distributions
The attribute estimators containing CPTs. -
m_Instances
The dataset header for the purposes of printing out a semi-intelligible model
-
-
Constructor Details
-
BayesNet
public BayesNet()
-
-
Method Details
-
getCapabilities
Returns default capabilities of the classifier.- Specified by:
getCapabilitiesin interfaceCapabilitiesHandler- Specified by:
getCapabilitiesin interfaceClassifier- Overrides:
getCapabilitiesin classAbstractClassifier- Returns:
- the capabilities of this classifier
- See Also:
-
buildClassifier
Generates the classifier.- Specified by:
buildClassifierin interfaceClassifier- Parameters:
instances- set of instances serving as training data- Throws:
Exception- if the classifier has not been generated successfully
-
getNumInstances
public int getNumInstances()Returns the number of instances the model was built from. -
initStructure
Init structure initializes the structure to an empty graph or a Naive Bayes graph (depending on the -N flag).- Throws:
Exception- in case of an error
-
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 like a naive Bayes classifier). This method can be overridden by derived classes to restrict the class of network structures that are acceptable.- Throws:
Exception- in case of an error
-
estimateCPTs
estimateCPTs estimates the conditional probability tables for the Bayes Net using the network structure.- Throws:
Exception- in case of an error
-
initCPTs
initializes the conditional probabilities- Throws:
Exception- in case of an error
-
updateClassifier
Updates the classifier with the given instance.- Parameters:
instance- the new training instance to include in the model- Throws:
Exception- if the instance could not be incorporated in the model.
-
distributionForInstance
Calculates the class membership probabilities for the given test instance.- Specified by:
distributionForInstancein interfaceClassifier- Overrides:
distributionForInstancein classAbstractClassifier- Parameters:
instance- the instance to be classified- Returns:
- predicted class probability distribution
- Throws:
Exception- if there is a problem generating the prediction
-
countsForInstance
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:
Exception- if there is a problem generating the prediction
-
listOptions
Returns an enumeration describing the available options- Specified by:
listOptionsin interfaceOptionHandler- Overrides:
listOptionsin classAbstractClassifier- Returns:
- an enumeration of all the available options
-
setOptions
Parses 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 classAbstractClassifier- Parameters:
options- the list of options as an array of strings- Throws:
Exception- if an option is not supported
-
partitionOptions
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
Gets the current settings of the classifier.- Specified by:
getOptionsin interfaceOptionHandler- Overrides:
getOptionsin classAbstractClassifier- Returns:
- an array of strings suitable for passing to setOptions
-
setSearchAlgorithm
Set the SearchAlgorithm used in searching for network structures.- Parameters:
newSearchAlgorithm- the SearchAlgorithm to use.
-
getSearchAlgorithm
Get the SearchAlgorithm used as the search algorithm- Returns:
- the SearchAlgorithm used as the search algorithm
-
setEstimator
Set the Estimator Algorithm used in calculating the CPTs- Parameters:
newBayesNetEstimator- the Estimator to use.
-
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
Set name of network in BIF file to compare with- Parameters:
sBIFFile- the name of the BIF file
-
getBIFFile
Get name of network in BIF file to compare with- Returns:
- BIF file name
-
toString
Returns a description of the classifier. -
graphType
public int graphType()Returns the type of graph this classifier represents. -
graph
Returns a BayesNet graph in XMLBIF ver 0.3 format. -
getBIFHeader
-
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
- Returns:
- a string to describe the UseADTreeoption.
-
searchAlgorithmTipText
- Returns:
- a string to describe the SearchAlgorithm.
-
estimatorTipText
This will return a string describing the BayesNetEstimator.- Returns:
- The string.
-
BIFFileTipText
- Returns:
- a string to describe the BIFFile.
-
globalInfo
This will return a string describing the classifier.- Returns:
- The string.
-
main
Main method for testing this class.- Parameters:
argv- the options
-
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
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
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
Get full set of parent sets.- Returns:
- parent sets;
-
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
get the parent set of a node- Parameters:
iNode- index of the node- Returns:
- Parent set of the specified node.
-
getADTree
get ADTree strucrture containing efficient representation of counts.- Returns:
- ADTree strucrture
-
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
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:
IllegalArgumentException- if the named measure is not supported
-
getRevision
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Overrides:
getRevisionin classAbstractClassifier- Returns:
- the revision
-