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

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:

 -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: 15519 $
Author:
Remco Bouckaert (rrb@xm.co.nz)
See Also:
  • Field Details

    • m_Distributions

      public Estimator[][] m_Distributions
      The attribute estimators containing CPTs.
    • m_Instances

      public Instances m_Instances
      The dataset header for the purposes of printing out a semi-intelligible model
  • Constructor Details

    • BayesNet

      public BayesNet()
  • Method Details

    • getCapabilities

      public Capabilities getCapabilities()
      Returns default capabilities of the classifier.
      Specified by:
      getCapabilities in interface CapabilitiesHandler
      Specified by:
      getCapabilities in interface Classifier
      Overrides:
      getCapabilities in class AbstractClassifier
      Returns:
      the capabilities of this classifier
      See Also:
    • buildClassifier

      public void buildClassifier(Instances instances) throws Exception
      Generates the classifier.
      Specified by:
      buildClassifier in interface Classifier
      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

      public void initStructure() throws Exception
      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

      public void buildStructure() throws Exception
      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

      public void estimateCPTs() throws Exception
      estimateCPTs estimates the conditional probability tables for the Bayes Net using the network structure.
      Throws:
      Exception - in case of an error
    • initCPTs

      public void initCPTs() throws Exception
      initializes the conditional probabilities
      Throws:
      Exception - in case of an error
    • updateClassifier

      public void updateClassifier(Instance instance) throws Exception
      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

      public double[] distributionForInstance(Instance instance) throws Exception
      Calculates the class membership probabilities for the given test instance.
      Specified by:
      distributionForInstance in interface Classifier
      Overrides:
      distributionForInstance in class AbstractClassifier
      Parameters:
      instance - the instance to be classified
      Returns:
      predicted class probability distribution
      Throws:
      Exception - if there is a problem generating the prediction
    • countsForInstance

      public double[] countsForInstance(Instance instance) throws 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:
      Exception - if there is a problem generating the prediction
    • listOptions

      public Enumeration<Option> listOptions()
      Returns an enumeration describing the available options
      Specified by:
      listOptions in interface OptionHandler
      Overrides:
      listOptions in class AbstractClassifier
      Returns:
      an enumeration of all the available options
    • setOptions

      public void setOptions(String[] options) throws Exception
      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:
      setOptions in interface OptionHandler
      Overrides:
      setOptions in class AbstractClassifier
      Parameters:
      options - the list of options as an array of strings
      Throws:
      Exception - if an option is not supported
    • partitionOptions

      public static String[] partitionOptions(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 String[] getOptions()
      Gets the current settings of the classifier.
      Specified by:
      getOptions in interface OptionHandler
      Overrides:
      getOptions in class AbstractClassifier
      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(String sBIFFile)
      Set name of network in BIF file to compare with
      Parameters:
      sBIFFile - the name of the BIF file
    • getBIFFile

      public String getBIFFile()
      Get name of network in BIF file to compare with
      Returns:
      BIF file name
    • toString

      public String toString()
      Returns a description of the classifier.
      Overrides:
      toString in class Object
      Returns:
      a description of the classifier as a string.
    • graphType

      public int graphType()
      Returns the type of graph this classifier represents.
      Specified by:
      graphType in interface Drawable
      Returns:
      Drawable.TREE
    • graph

      public String graph() throws Exception
      Returns a BayesNet graph in XMLBIF ver 0.3 format.
      Specified by:
      graph in interface Drawable
      Returns:
      String representing this BayesNet in XMLBIF ver 0.3
      Throws:
      Exception - in case BIF generation fails
    • getBIFHeader

      public String getBIFHeader()
    • toXMLBIF03

      public 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 String useADTreeTipText()
      Returns:
      a string to describe the UseADTreeoption.
    • searchAlgorithmTipText

      public String searchAlgorithmTipText()
      Returns:
      a string to describe the SearchAlgorithm.
    • estimatorTipText

      public String estimatorTipText()
      This will return a string describing the BayesNetEstimator.
      Returns:
      The string.
    • BIFFileTipText

      public String BIFFileTipText()
      Returns:
      a string to describe the BIFFile.
    • globalInfo

      public String globalInfo()
      This will return a string describing the classifier.
      Returns:
      The string.
    • main

      public static void main(String[] argv)
      Main method for testing this class.
      Parameters:
      argv - the options
    • getName

      public 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 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 String getNodeValue(int iNode, int iValue)
      get name of a particular value of a node
      Parameters:
      iNode - index of the node
      iValue - 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 node
      iParent - 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 node
      iParent - 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 Enumeration<String> enumerateMeasures()
      Returns an enumeration of the measure names. Additional measures must follow the naming convention of starting with "measure", eg. double measureBlah()
      Specified by:
      enumerateMeasures in interface AdditionalMeasureProducer
      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(String measureName)
      Returns the value of the named measure
      Specified by:
      getMeasure in interface AdditionalMeasureProducer
      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

      public String getRevision()
      Returns the revision string.
      Specified by:
      getRevision in interface RevisionHandler
      Overrides:
      getRevision in class AbstractClassifier
      Returns:
      the revision