Package weka.classifiers.bayes.net
Class EditableBayesNet
- java.lang.Object
-
- weka.classifiers.Classifier
-
- weka.classifiers.bayes.BayesNet
-
- weka.classifiers.bayes.net.EditableBayesNet
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,AdditionalMeasureProducer
,CapabilitiesHandler
,Drawable
,OptionHandler
,RevisionHandler
,WeightedInstancesHandler
- Direct Known Subclasses:
BayesNetGenerator
public class EditableBayesNet extends BayesNet
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://www.cs.waikato.ac.nz/~remco/weka.pdf 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: 7836 $
- Author:
- Remco Bouckaert (rrb@xm.co.nz)
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class weka.classifiers.bayes.BayesNet
m_Distributions, m_Instances
-
Fields inherited from interface weka.core.Drawable
BayesNet, Newick, NOT_DRAWABLE, TREE
-
-
Constructor Summary
Constructors Constructor Description EditableBayesNet()
standard constructor *EditableBayesNet(boolean bSetInstances)
constructor that potentially initializes instances as wellEditableBayesNet(BIFReader other)
constructor, copies Bayesian network structure from a Bayesian network encapsulated in a BIFReaderEditableBayesNet(Instances instances)
constructor, creates empty network with nodes based on the attributes in a data set
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addArc(int nParent, int nChild)
Add arc between two nodes Distributions are updated by duplication for every value of the parent node.void
addArc(java.lang.String sParent, java.lang.String sChild)
Add arc between two nodes Distributions are updated by duplication for every value of the parent node.void
addArc(java.lang.String sParent, FastVector nodes)
Add arc between parent node and each of the nodes in a given list.void
addNode(java.lang.String sName, int nCardinality)
Add new node to the network, initializing instances, parentsets, distributions.void
addNode(java.lang.String sName, int nCardinality, int nPosX, int nPosY)
Add node to network at a given position, initializing instances, parentsets, distributions.void
addNodeValue(int nTargetNode, java.lang.String sNewValue)
Add node value to a node.void
alignBottom(FastVector nodes)
align set of nodes with the bottom most node in the listvoid
alignLeft(FastVector nodes)
align set of nodes with the left most node in the listvoid
alignRight(FastVector nodes)
align set of nodes with the right most node in the listvoid
alignTop(FastVector nodes)
align set of nodes with the top most node in the listboolean
canRedo()
return whether there is something on the undo stack that can be performedboolean
canUndo()
return whether there is something on the undo stack that can be performedvoid
centerHorizontal(FastVector nodes)
center set of nodes half way between left and right most node in the listvoid
centerVertical(FastVector nodes)
center set of nodes half way between top and bottom most node in the listvoid
clearUndoStack()
remove all actions from the undo stackvoid
deleteArc(int nParent, int nChild)
Delete arc between two nodes.void
deleteArc(java.lang.String sParent, java.lang.String sChild)
Delete arc between two nodes.void
deleteNode(int nTargetNode)
Delete node from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value.void
deleteNode(java.lang.String sName)
Delete node from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value.void
deleteSelection(FastVector nodes)
Delete nodes with indexes in selection from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value.void
delNodeValue(int nTargetNode, java.lang.String sValue)
Delete node value from a node.FastVector
getChildren(int nTargetNode)
return list of children of a nodejava.lang.String
getContent(org.w3c.dom.Element node)
XML helper function.double[][]
getDistribution(int nTargetNode)
returns distribution of a node in matrix form with matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)double[][]
getDistribution(java.lang.String sName)
returns distribution of a node in matrix form with matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)int
getEvidence(int iNode)
get evidence state of a node.double[]
getMargin(int iNode)
return marginal distibution for a nodeint
getNode(java.lang.String sNodeName)
returns index of node with given name.int
getNode2(java.lang.String sNodeName)
returns index of node with given name, or -1 if no such node existsint
getPositionX(int iNode)
get x position of a nodeint
getPositionY(int iNode)
get y position of a nodejava.lang.String
getRevision()
Returns the revision string.java.lang.String
getValueName(int nTargetNode, int iValue)
returns value of a nodejava.lang.String[]
getValues(int nTargetNode)
returns array of values of a nodejava.lang.String[]
getValues(java.lang.String sName)
returns array of values of a nodeboolean
isChanged()
return true when current state differs from the state the network was last savedvoid
isSaved()
indicate the network state was savedjava.lang.String
lastActionMsg()
get message representing the last action performed on the networkvoid
layoutGraph(FastVector nPosX, FastVector nPosY)
set positions of all nodesstatic void
main(java.lang.String[] args)
void
paste(java.lang.String sXML)
Apply paste operation with XMLBIF fragment.java.lang.String
redo()
redo the last edit action performed on the network.void
renameNodeValue(int nTargetNode, java.lang.String sValue, java.lang.String sNewValue)
change the name of a value of a nodevoid
setData(Instances instances)
Assuming a network structure is defined and we want to learn from data, the data set must be put if correct order first and possibly discretized/missing values filled in before proceeding to CPT learning.void
setDistribution(int nTargetNode, double[][] P)
specify distribution of a nodevoid
setDistribution(java.lang.String sName, double[][] P)
specify distribution of a nodevoid
setEvidence(int iNode, int iValue)
set evidence state of a node.void
setMargin(int iNode, double[] fMarginP)
set marginal distibution for a nodevoid
setNodeName(int nTargetNode, java.lang.String sName)
change the name of a nodevoid
setPosition(int iNode, int nX, int nY)
set position of nodevoid
setPosition(int nNode, int nX, int nY, FastVector nodes)
Set position of node.void
spaceHorizontal(FastVector nodes)
space out set of nodes evenly between left and right most node in the listvoid
spaceVertical(FastVector nodes)
space out set of nodes evenly between top and bottom most node in the listjava.lang.String
toXMLBIF03()
returns network in XMLBIF formatjava.lang.String
toXMLBIF03(FastVector nodes)
return fragment of network in XMLBIF formatjava.lang.String
undo()
undo the last edit action performed on the network.-
Methods inherited from class weka.classifiers.bayes.BayesNet
BIFFileTipText, buildClassifier, buildStructure, countsForInstance, distributionForInstance, enumerateMeasures, estimateCPTs, estimatorTipText, getADTree, getBIFFile, getBIFHeader, getCapabilities, getCardinality, getDistributions, getEstimator, getMeasure, getName, getNodeName, getNodeValue, getNrOfNodes, getNrOfParents, getOptions, getParent, getParentCardinality, getParentSet, getParentSets, getProbability, getSearchAlgorithm, getUseADTree, globalInfo, graph, graphType, initCPTs, initStructure, listOptions, measureAICScore, measureBayesScore, measureBDeuScore, measureDivergence, measureEntropyScore, measureExtraArcs, measureMDLScore, measureMissingArcs, measureReversedArcs, partitionOptions, searchAlgorithmTipText, setBIFFile, setEstimator, setOptions, setSearchAlgorithm, setUseADTree, toString, updateClassifier, useADTreeTipText
-
Methods inherited from class weka.classifiers.Classifier
classifyInstance, debugTipText, forName, getDebug, makeCopies, makeCopy, setDebug
-
-
-
-
Constructor Detail
-
EditableBayesNet
public EditableBayesNet()
standard constructor *
-
EditableBayesNet
public EditableBayesNet(Instances instances)
constructor, creates empty network with nodes based on the attributes in a data set
-
EditableBayesNet
public EditableBayesNet(BIFReader other)
constructor, copies Bayesian network structure from a Bayesian network encapsulated in a BIFReader
-
EditableBayesNet
public EditableBayesNet(boolean bSetInstances)
constructor that potentially initializes instances as well- Parameters:
bSetInstances
- flag indicating whether to initialize instances or not
-
-
Method Detail
-
setData
public void setData(Instances instances) throws java.lang.Exception
Assuming a network structure is defined and we want to learn from data, the data set must be put if correct order first and possibly discretized/missing values filled in before proceeding to CPT learning.- Parameters:
instances
- data set to learn from- Throws:
java.lang.Exception
- when data sets are not compatible, e.g., a variable is missing or a variable has different nr of values.
-
getNode2
public int getNode2(java.lang.String sNodeName)
returns index of node with given name, or -1 if no such node exists- Parameters:
sNodeName
- name of the node to get index for
-
getNode
public int getNode(java.lang.String sNodeName) throws java.lang.Exception
returns index of node with given name. Throws exception if no such node exists- Parameters:
sNodeName
- name of the node to get index for- Throws:
java.lang.Exception
-
addNode
public void addNode(java.lang.String sName, int nCardinality) throws java.lang.Exception
Add new node to the network, initializing instances, parentsets, distributions. Used for manual manipulation of the Bayesian network.- Parameters:
sName
- name of the node. If the name already exists, an x is appended to the namenCardinality
- number of values for this node- Throws:
java.lang.Exception
-
addNode
public void addNode(java.lang.String sName, int nCardinality, int nPosX, int nPosY) throws java.lang.Exception
Add node to network at a given position, initializing instances, parentsets, distributions. Used for manual manipulation of the Bayesian network.- Parameters:
sName
- name of the node. If the name already exists, an x is appended to the namenCardinality
- number of values for this nodenPosX
- x-coordiate of the position to place this nodenPosY
- y-coordiate of the position to place this node- Throws:
java.lang.Exception
-
deleteNode
public void deleteNode(java.lang.String sName) throws java.lang.Exception
Delete node from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value. Used for manual manipulation of the Bayesian network.- Parameters:
sName
- name of the node. If the name does not exists an exception is thrown- Throws:
java.lang.Exception
-
deleteNode
public void deleteNode(int nTargetNode) throws java.lang.Exception
Delete node from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value. Used for manual manipulation of the Bayesian network.- Parameters:
nTargetNode
- index of the node to delete.- Throws:
java.lang.Exception
-
deleteSelection
public void deleteSelection(FastVector nodes)
Delete nodes with indexes in selection from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value. Used for manual manipulation of the Bayesian network.- Parameters:
nodes
- array of indexes of nodes to delete.- Throws:
java.lang.Exception
-
getContent
public java.lang.String getContent(org.w3c.dom.Element node)
XML helper function. Returns all TEXT children of the given node in one string. Between the node values new lines are inserted.- Parameters:
node
- the node to return the content for- Returns:
- the content of the node
-
paste
public void paste(java.lang.String sXML) throws java.lang.Exception
Apply paste operation with XMLBIF fragment. This adds nodes in the XMLBIF fragment to the network, together with its parents. First, paste in test mode to verify no problems occur, then execute paste operation. If a problem occurs (e.g. parent does not exist) then a exception is thrown.- Parameters:
sXML
- XMLBIF fragment to paste into the network- Throws:
java.lang.Exception
-
addArc
public void addArc(java.lang.String sParent, java.lang.String sChild) throws java.lang.Exception
Add arc between two nodes Distributions are updated by duplication for every value of the parent node.- Parameters:
sParent
- name of the parent nodesChild
- name of the child node- Throws:
java.lang.Exception
- if parent or child cannot be found in network
-
addArc
public void addArc(int nParent, int nChild) throws java.lang.Exception
Add arc between two nodes Distributions are updated by duplication for every value of the parent node.- Parameters:
nParent
- index of the parent nodenChild
- index of the child node- Throws:
java.lang.Exception
-
addArc
public void addArc(java.lang.String sParent, FastVector nodes) throws java.lang.Exception
Add arc between parent node and each of the nodes in a given list. Distributions are updated as above.- Parameters:
sParent
- name of the parent nodenodes
- array of indexes of child nodes- Throws:
java.lang.Exception
-
deleteArc
public void deleteArc(java.lang.String sParent, java.lang.String sChild) throws java.lang.Exception
Delete arc between two nodes. Distributions are updated by condensing for the parent node taking its first value.- Parameters:
sParent
- name of the parent nodesChild
- name of the child node- Throws:
java.lang.Exception
- if parent or child cannot be found in network
-
deleteArc
public void deleteArc(int nParent, int nChild) throws java.lang.Exception
Delete arc between two nodes. Distributions are updated by condensing for the parent node taking its first value.- Parameters:
nParent
- index of the parent nodenChild
- index of the child node- Throws:
java.lang.Exception
-
setDistribution
public void setDistribution(java.lang.String sName, double[][] P) throws java.lang.Exception
specify distribution of a node- Parameters:
sName
- name of the node to specify distribution forP
- matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)- Throws:
java.lang.Exception
- if parent or child cannot be found in network
-
setDistribution
public void setDistribution(int nTargetNode, double[][] P) throws java.lang.Exception
specify distribution of a node- Parameters:
nTargetNode
- index of the node to specify distribution forP
- matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)- Throws:
java.lang.Exception
- if parent or child cannot be found in network
-
getDistribution
public double[][] getDistribution(java.lang.String sName)
returns distribution of a node in matrix form with matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)- Parameters:
sName
- name of the node to get distribution from
-
getDistribution
public double[][] getDistribution(int nTargetNode)
returns distribution of a node in matrix form with matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)- Parameters:
nTargetNode
- index of the node to get distribution from
-
getValues
public java.lang.String[] getValues(java.lang.String sName)
returns array of values of a node- Parameters:
sName
- name of the node to get values from
-
getValues
public java.lang.String[] getValues(int nTargetNode)
returns array of values of a node- Parameters:
nTargetNode
- index of the node to get values from
-
getValueName
public java.lang.String getValueName(int nTargetNode, int iValue)
returns value of a node- Parameters:
nTargetNode
- index of the node to get values fromiValue
- index of the value
-
setNodeName
public void setNodeName(int nTargetNode, java.lang.String sName)
change the name of a node- Parameters:
nTargetNode
- index of the node to set name forsName
- new name to assign
-
renameNodeValue
public void renameNodeValue(int nTargetNode, java.lang.String sValue, java.lang.String sNewValue)
change the name of a value of a node- Parameters:
nTargetNode
- index of the node to set name forsValue
- current name of the valuesNewValue
- new name of the value
-
addNodeValue
public void addNodeValue(int nTargetNode, java.lang.String sNewValue)
Add node value to a node. Distributions for the node assign zero probability to the new value. Child nodes duplicate CPT conditioned on the new value.- Parameters:
nTargetNode
- index of the node to add value forsNewValue
- name of the value
-
delNodeValue
public void delNodeValue(int nTargetNode, java.lang.String sValue) throws java.lang.Exception
Delete node value from a node. Distributions for the node are scaled up proportional to existing distribution (or made uniform if zero probability is assigned to remainder of values). .* Child nodes delete CPTs conditioned on the new value.- Parameters:
nTargetNode
- index of the node to delete value fromsValue
- name of the value to delete- Throws:
java.lang.Exception
-
setPosition
public void setPosition(int iNode, int nX, int nY)
set position of node- Parameters:
iNode
- index of node to set position fornX
- x position of new positionnY
- y position of new position
-
setPosition
public void setPosition(int nNode, int nX, int nY, FastVector nodes)
Set position of node. Move set of nodes with the same displacement as a specified node.- Parameters:
iNode
- index of node to set position fornX
- x position of new positionnY
- y position of new positionnodes
- array of indexes of nodes to move
-
layoutGraph
public void layoutGraph(FastVector nPosX, FastVector nPosY)
set positions of all nodes- Parameters:
nPosX
- new x positions for all nodesnPosY
- new y positions for all nodes
-
getPositionX
public int getPositionX(int iNode)
get x position of a node- Parameters:
iNode
- index of node of interest
-
getPositionY
public int getPositionY(int iNode)
get y position of a node- Parameters:
iNode
- index of node of interest
-
alignLeft
public void alignLeft(FastVector nodes)
align set of nodes with the left most node in the list- Parameters:
nodes
- list of indexes of nodes to align
-
alignRight
public void alignRight(FastVector nodes)
align set of nodes with the right most node in the list- Parameters:
nodes
- list of indexes of nodes to align
-
alignTop
public void alignTop(FastVector nodes)
align set of nodes with the top most node in the list- Parameters:
nodes
- list of indexes of nodes to align
-
alignBottom
public void alignBottom(FastVector nodes)
align set of nodes with the bottom most node in the list- Parameters:
nodes
- list of indexes of nodes to align
-
centerHorizontal
public void centerHorizontal(FastVector nodes)
center set of nodes half way between left and right most node in the list- Parameters:
nodes
- list of indexes of nodes to center
-
centerVertical
public void centerVertical(FastVector nodes)
center set of nodes half way between top and bottom most node in the list- Parameters:
nodes
- list of indexes of nodes to center
-
spaceHorizontal
public void spaceHorizontal(FastVector nodes)
space out set of nodes evenly between left and right most node in the list- Parameters:
nodes
- list of indexes of nodes to space out
-
spaceVertical
public void spaceVertical(FastVector nodes)
space out set of nodes evenly between top and bottom most node in the list- Parameters:
nodes
- list of indexes of nodes to space out
-
getMargin
public double[] getMargin(int iNode)
return marginal distibution for a node- Parameters:
iNode
- index of node of interest
-
setMargin
public void setMargin(int iNode, double[] fMarginP)
set marginal distibution for a node- Parameters:
iNode
- index of node to set marginal distribution forfMarginP
- marginal distribution
-
getEvidence
public int getEvidence(int iNode)
get evidence state of a node. -1 represents no evidence set, otherwise the index of a value of the node- Parameters:
iNode
- index of node of interest
-
setEvidence
public void setEvidence(int iNode, int iValue)
set evidence state of a node. -1 represents no evidence set, otherwise the index of a value of the node- Parameters:
iNode
- index of node of interestiValue
- evidence value to set
-
getChildren
public FastVector getChildren(int nTargetNode)
return list of children of a node- Parameters:
iNode
- index of node of interest
-
toXMLBIF03
public java.lang.String toXMLBIF03()
returns network in XMLBIF format- Overrides:
toXMLBIF03
in classBayesNet
- Returns:
- an XML BIF 0.3 description of the classifier as a string.
-
toXMLBIF03
public java.lang.String toXMLBIF03(FastVector nodes)
return fragment of network in XMLBIF format- Parameters:
nodes
- array of indexes of nodes that should be in the fragment
-
canUndo
public boolean canUndo()
return whether there is something on the undo stack that can be performed
-
canRedo
public boolean canRedo()
return whether there is something on the undo stack that can be performed
-
isChanged
public boolean isChanged()
return true when current state differs from the state the network was last saved
-
isSaved
public void isSaved()
indicate the network state was saved
-
lastActionMsg
public java.lang.String lastActionMsg()
get message representing the last action performed on the network
-
undo
public java.lang.String undo()
undo the last edit action performed on the network. returns message representing the action performed.
-
redo
public java.lang.String redo()
redo the last edit action performed on the network. returns message representing the action performed.
-
clearUndoStack
public void clearUndoStack()
remove all actions from the undo stack
-
getRevision
public java.lang.String getRevision()
Returns the revision string.- Specified by:
getRevision
in interfaceRevisionHandler
- Overrides:
getRevision
in classBayesNet
- Returns:
- the revision
-
main
public static void main(java.lang.String[] args)
- Parameters:
args
-
-
-