Models

Bayesian Model

class pgmpy.models.BayesianModel.BayesianModel(ebunch=None)[source]

Base class for bayesian model.

A models stores nodes and edges with conditional probability distribution (cpd) and other attributes.

models hold directed edges. Self loops are not allowed neither multiple (parallel) edges.

Nodes can be any hashable python object.

Edges are represented as links between nodes.

Parameters:

data : input graph

Data to initialize graph. If data=None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object.

Examples

Create an empty bayesian model with no nodes and no edges.

>>> from pgmpy.models import BayesianModel
>>> G = BayesianModel()

G can be grown in several ways.

Nodes:

Add one node at a time:

>>> G.add_node('a')

Add the nodes from any container (a list, set or tuple or the nodes from another graph).

>>> G.add_nodes_from(['a', 'b'])

Edges:

G can also be grown by adding edges.

Add one edge,

>>> G.add_edge('a', 'b')

a list of edges,

>>> G.add_edges_from([('a', 'b'), ('b', 'c')])

If some edges connect nodes not yet in the model, the nodes are added automatically. There are no errors when adding nodes or edges that already exist.

Shortcuts:

Many common graph features allow python syntax for speed reporting.

>>> 'a' in G     # check if node in graph
True
>>> len(G)  # number of nodes in graph
3
active_trail_nodes(variables, observed=None)[source]

Returns a dictionary with the given variables as keys and all the nodes reachable from that respective variable as values.

Parameters:

variables: str or array like

variables whose active trails are to be found.

observed : List of nodes (optional)

If given the active trails would be computed assuming these nodes to be observed.

References

Details of the algorithm can be found in ‘Probabilistic Graphical Model Principles and Techniques’ - Koller and Friedman Page 75 Algorithm 3.1

Examples

>>> from pgmpy.models import BayesianModel
>>> student = BayesianModel()
>>> student.add_nodes_from(['diff', 'intel', 'grades'])
>>> student.add_edges_from([('diff', 'grades'), ('intel', 'grades')])
>>> student.active_trail_nodes('diff')
{'diff': {'diff', 'grades'}}
>>> student.active_trail_nodes(['diff', 'intel'], observed='grades')
{'diff': {'diff', 'intel'}, 'intel': {'diff', 'intel'}}
add_cpds(*cpds)[source]

Add CPD (Conditional Probability Distribution) to the Bayesian Model.

Parameters:

cpds : list, set, tuple (array-like)

List of CPDs which will be associated with the model

add_edge(u, v, **kwargs)[source]

Add an edge between u and v.

The nodes u and v will be automatically added if they are not already in the graph

Parameters:

u,v : nodes

Nodes can be any hashable python object.

Examples

>>> from pgmpy.models import BayesianModel/home/abinash/software_packages/numpy-1.7.1
>>> G = BayesianModel()
>>> G.add_nodes_from(['grade', 'intel'])
>>> G.add_edge('grade', 'intel')
check_model()[source]

Check the model for various errors. This method checks for the following errors.

  • Checks if the sum of the probabilities for each state is equal to 1 (tol=0.01).
  • Checks if the CPDs associated with nodes are consistent with their parents.
Returns:

check: boolean

True if all the checks are passed

copy()[source]

Returns a copy of the model.

Returns:BayesianModel: Copy of the model on which the method was called.

Examples

>>> from pgmpy.models import BayesianModel
>>> from pgmpy.factors.discrete import TabularCPD
>>> model = BayesianModel([('A', 'B'), ('B', 'C')])
>>> cpd_a = TabularCPD('A', 2, [[0.2], [0.8]])
>>> cpd_b = TabularCPD('B', 2, [[0.3, 0.7], [0.7, 0.3]],
                       evidence=['A'],
                       evidence_card=[2])
>>> cpd_c = TabularCPD('C', 2, [[0.1, 0.9], [0.9, 0.1]],
                       evidence=['B'],
                       evidence_card=[2])
>>> model.add_cpds(cpd_a, cpd_b, cpd_c)
>>> copy_model = model.copy()
>>> copy_model.nodes()
['C', 'A', 'B']
>>> copy_model.edges()
[('A', 'B'), ('B', 'C')]
>>> copy_model.get_cpds()
[<TabularCPD representing P(A:2) at 0x7f2824930a58>,
 <TabularCPD representing P(B:2 | A:2) at 0x7f2824930a90>,
 <TabularCPD representing P(C:2 | B:2) at 0x7f2824944240>]
fit(data, estimator=None, state_names=[], complete_samples_only=True, **kwargs)[source]

Estimates the CPD for each variable based on a given data set.

Parameters:

data: pandas DataFrame object

DataFrame object with column names identical to the variable names of the network. (If some values in the data are missing the data cells should be set to numpy.NaN. Note that pandas converts each column containing numpy.NaN`s to dtype `float.)

estimator: Estimator class

One of: - MaximumLikelihoodEstimator (default) - BayesianEstimator: In this case, pass ‘prior_type’ and either ‘pseudo_counts’

or ‘equivalent_sample_size’ as additional keyword arguments. See BayesianEstimator.get_parameters() for usage.

state_names: dict (optional)

A dict indicating, for each variable, the discrete set of states that the variable can take. If unspecified, the observed values in the data set are taken to be the only possible states.

complete_samples_only: bool (default `True`)

Specifies how to deal with missing data, if present. If set to True all rows that contain np.Nan somewhere are ignored. If False then, for each variable, every row where neither the variable nor its parents are np.NaN is used.

Examples

>>> import pandas as pd
>>> from pgmpy.models import BayesianModel
>>> from pgmpy.estimators import MaximumLikelihoodEstimator
>>> data = pd.DataFrame(data={'A': [0, 0, 1], 'B': [0, 1, 0], 'C': [1, 1, 0]})
>>> model = BayesianModel([('A', 'C'), ('B', 'C')])
>>> model.fit(data)
>>> model.get_cpds()
[<TabularCPD representing P(A:2) at 0x7fb98a7d50f0>,
<TabularCPD representing P(B:2) at 0x7fb98a7d5588>,
<TabularCPD representing P(C:2 | A:2, B:2) at 0x7fb98a7b1f98>]
get_cardinality(node=None)[source]

Returns the cardinality of the node. Throws an error if the CPD for the queried node hasn’t been added to the network.

Parameters:

node: Any hashable python object(optional).

The node whose cardinality we want. If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Returns:

int or dict : If node is specified returns the cardinality of the node.

If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Examples

>>> from pgmpy.models import BayesianModel
>>> from pgmpy.factors.discrete import TabularCPD
>>> student = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
>>> cpd_diff = TabularCPD('diff',2,[[0.6,0.4]]);
>>> cpd_intel = TabularCPD('intel',2,[[0.7,0.3]]);
>>> cpd_grade = TabularCPD('grade', 2, [[0.1, 0.9, 0.2, 0.7],
...                                     [0.9, 0.1, 0.8, 0.3]],
...                                 ['intel', 'diff'], [2, 2])
>>> student.add_cpds(cpd_diff,cpd_intel,cpd_grade)
>>> student.get_cardinality()
defaultdict(int, {'diff': 2, 'grade': 2, 'intel': 2})
>>> student.get_cardinality('intel')
2
get_cpds(node=None)[source]

Returns the cpd of the node. If node is not specified returns all the CPDs that have been added till now to the graph

Returns:A list of TabularCPDs.

Examples

>>> from pgmpy.models import BayesianModel
>>> from pgmpy.factors.discrete import TabularCPD
>>> student = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
>>> cpd = TabularCPD('grade', 2, [[0.1, 0.9, 0.2, 0.7],
...                               [0.9, 0.1, 0.8, 0.3]],
...                  ['intel', 'diff'], [2, 2])
>>> student.add_cpds(cpd)
>>> student.get_cpds()
get_factorized_product(latex=False)[source]
get_immoralities()[source]

Finds all the immoralities in the model A v-structure X -> Z <- Y is an immorality if there is no direct edge between X and Y .

Returns:set: A set of all the immoralities in the model

Examples

>>> from pgmpy.models import BayesianModel
>>> student = BayesianModel()
>>> student.add_edges_from([('diff', 'grade'), ('intel', 'grade'),
...                         ('intel', 'SAT'), ('grade', 'letter')])
>>> student.get_immoralities()
{('diff','intel')}
get_independencies(latex=False)[source]

Computes independencies in the Bayesian Network, by checking d-seperation.

Parameters:

latex: boolean

If latex=True then latex string of the independence assertion would be created.

Examples

>>> from pgmpy.models import BayesianModel
>>> chain = BayesianModel([('X', 'Y'), ('Y', 'Z')])
>>> chain.get_independencies()
(X _|_ Z | Y)
(Z _|_ X | Y)
is_active_trail(start, end, observed=None)[source]

Returns True if there is any active trail between start and end node

Parameters:

start : Graph Node

end : Graph Node

observed : List of nodes (optional)

If given the active trail would be computed assuming these nodes to be observed.

additional_observed : List of nodes (optional)

If given the active trail would be computed assuming these nodes to be observed along with the nodes marked as observed in the model.

Examples

>>> from pgmpy.models import BayesianModel
>>> student = BayesianModel()
>>> student.add_nodes_from(['diff', 'intel', 'grades', 'letter', 'sat'])
>>> student.add_edges_from([('diff', 'grades'), ('intel', 'grades'), ('grades', 'letter'),
...                         ('intel', 'sat')])
>>> student.is_active_trail('diff', 'intel')
False
>>> student.is_active_trail('grades', 'sat')
True
is_iequivalent(model)[source]

Checks whether the given model is I-equivalent

Two graphs G1 and G2 are said to be I-equivalent if they have same skeleton and have same set of immoralities.

Note: For same skeleton different names of nodes can work but for immoralities names of nodes must be same

Parameters:model : A Bayesian model object, for which you want to check I-equivalence
Returns:boolean : True if both are I-equivalent, False otherwise

Examples

>>> from pgmpy.models import BayesianModel
>>> G = BayesianModel()
>>> G.add_edges_from([('V', 'W'), ('W', 'X'),
...                   ('X', 'Y'), ('Z', 'Y')])
>>> G1 = BayesianModel()
>>> G1.add_edges_from([('W', 'V'), ('X', 'W'),
...                    ('X', 'Y'), ('Z', 'Y')])
>>> G.is_iequivalent(G1)
True
is_imap(JPD)[source]

Checks whether the bayesian model is Imap of given JointProbabilityDistribution

Parameters:

JPD : An instance of JointProbabilityDistribution Class, for which you want to

check the Imap

Returns:

boolean : True if bayesian model is Imap for given Joint Probability Distribution

False otherwise

Examples

>>> from pgmpy.models import BayesianModel
>>> from pgmpy.factors.discrete import TabularCPD
>>> from pgmpy.factors.discrete import JointProbabilityDistribution
>>> G = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
>>> diff_cpd = TabularCPD('diff', 2, [[0.2], [0.8]])
>>> intel_cpd = TabularCPD('intel', 3, [[0.5], [0.3], [0.2]])
>>> grade_cpd = TabularCPD('grade', 3,

... [[0.1,0.1,0.1,0.1,0.1,0.1],

... [0.1,0.1,0.1,0.1,0.1,0.1],

... [0.8,0.8,0.8,0.8,0.8,0.8]],

... evidence=[‘diff’, ‘intel’],

... evidence_card=[2, 3])

>>> G.add_cpds(diff_cpd, intel_cpd, grade_cpd)
>>> val = [0.01, 0.01, 0.08, 0.006, 0.006, 0.048, 0.004, 0.004, 0.032,

0.04, 0.04, 0.32, 0.024, 0.024, 0.192, 0.016, 0.016, 0.128]

>>> JPD = JointProbabilityDistribution(['diff', 'intel', 'grade'], [2, 3, 3], val)
>>> G.is_imap(JPD)

True

local_independencies(variables)[source]

Returns an instance of Independencies containing the local independencies of each of the variables.

Parameters:

variables: str or array like

variables whose local independencies are to be found.

Examples

>>> from pgmpy.models import BayesianModel
>>> student = BayesianModel()
>>> student.add_edges_from([('diff', 'grade'), ('intel', 'grade'),
>>>                         ('grade', 'letter'), ('intel', 'SAT')])
>>> ind = student.local_independencies('grade')
>>> ind
(grade _|_ SAT | diff, intel)
predict(data)[source]

Predicts states of all the missing variables.

Parameters:

data : pandas DataFrame object

A DataFrame object with column names same as the variables in the model.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from pgmpy.models import BayesianModel
>>> values = pd.DataFrame(np.random.randint(low=0, high=2, size=(1000, 5)),
...                       columns=['A', 'B', 'C', 'D', 'E'])
>>> train_data = values[:800]
>>> predict_data = values[800:]
>>> model = BayesianModel([('A', 'B'), ('C', 'B'), ('C', 'D'), ('B', 'E')])
>>> model.fit(values)
>>> predict_data = predict_data.copy()
>>> predict_data.drop('E', axis=1, inplace=True)
>>> y_pred = model.predict(predict_data)
>>> y_pred
    E
800 0
801 1
802 1
803 1
804 0
... ...
993 0
994 0
995 1
996 1
997 0
998 0
999 0
predict_probability(data)[source]

Predicts probabilities of all states of the missing variables.

Parameters:

data : pandas DataFrame object

A DataFrame object with column names same as the variables in the model.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from pgmpy.models import BayesianModel
>>> values = pd.DataFrame(np.random.randint(low=0, high=2, size=(100, 5)),
...                       columns=['A', 'B', 'C', 'D', 'E'])
>>> train_data = values[:80]
>>> predict_data = values[80:]
>>> model = BayesianModel([('A', 'B'), ('C', 'B'), ('C', 'D'), ('B', 'E')])
>>> model.fit(values)
>>> predict_data = predict_data.copy()
>>> predict_data.drop('B', axis=1, inplace=True)
>>> y_prob = model.predict_probability(predict_data)
>>> y_prob
    B_0         B_1
80  0.439178    0.560822
81  0.581970    0.418030
82  0.488275    0.511725
83  0.581970    0.418030
84  0.510794    0.489206
85  0.439178    0.560822
86  0.439178    0.560822
87  0.417124    0.582876
88  0.407978    0.592022
89  0.429905    0.570095
90  0.581970    0.418030
91  0.407978    0.592022
92  0.429905    0.570095
93  0.429905    0.570095
94  0.439178    0.560822
95  0.407978    0.592022
96  0.559904    0.440096
97  0.417124    0.582876
98  0.488275    0.511725
99  0.407978    0.592022
remove_cpds(*cpds)[source]

Removes the cpds that are provided in the argument.

Parameters:

*cpds: TabularCPD object

A CPD object on any subset of the variables of the model which is to be associated with the model.

Examples

>>> from pgmpy.models import BayesianModel
>>> from pgmpy.factors.discrete import TabularCPD
>>> student = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
>>> cpd = TabularCPD('grade', 2, [[0.1, 0.9, 0.2, 0.7],
...                               [0.9, 0.1, 0.8, 0.3]],
...                  ['intel', 'diff'], [2, 2])
>>> student.add_cpds(cpd)
>>> student.remove_cpds(cpd)
remove_node(node)[source]

Remove node from the model.

Removing a node also removes all the associated edges, removes the CPD of the node and marginalizes the CPDs of it’s children.

Parameters:

node : node

Node which is to be removed from the model.

Returns:

None

Examples

>>> import pandas as pd
>>> import numpy as np
>>> from pgmpy.models import BayesianModel
>>> model = BayesianModel([('A', 'B'), ('B', 'C'),
...                        ('A', 'D'), ('D', 'C')])
>>> values = pd.DataFrame(np.random.randint(low=0, high=2, size=(1000, 4)),
...                       columns=['A', 'B', 'C', 'D'])
>>> model.fit(values)
>>> model.get_cpds()
[<TabularCPD representing P(A:2) at 0x7f28248e2438>,
 <TabularCPD representing P(B:2 | A:2) at 0x7f28248e23c8>,
 <TabularCPD representing P(C:2 | B:2, D:2) at 0x7f28248e2748>,
 <TabularCPD representing P(D:2 | A:2) at 0x7f28248e26a0>]
>>> model.remove_node('A')
>>> model.get_cpds()
[<TabularCPD representing P(B:2) at 0x7f28248e23c8>,
 <TabularCPD representing P(C:2 | B:2, D:2) at 0x7f28248e2748>,
 <TabularCPD representing P(D:2) at 0x7f28248e26a0>]
remove_nodes_from(nodes)[source]

Remove multiple nodes from the model.

Removing a node also removes all the associated edges, removes the CPD of the node and marginalizes the CPDs of it’s children.

Parameters:

nodes : list, set (iterable)

Nodes which are to be removed from the model.

Returns:

None

Examples

>>> import pandas as pd
>>> import numpy as np
>>> from pgmpy.models import BayesianModel
>>> model = BayesianModel([('A', 'B'), ('B', 'C'),
...                        ('A', 'D'), ('D', 'C')])
>>> values = pd.DataFrame(np.random.randint(low=0, high=2, size=(1000, 4)),
...                       columns=['A', 'B', 'C', 'D'])
>>> model.fit(values)
>>> model.get_cpds()
[<TabularCPD representing P(A:2) at 0x7f28248e2438>,
 <TabularCPD representing P(B:2 | A:2) at 0x7f28248e23c8>,
 <TabularCPD representing P(C:2 | B:2, D:2) at 0x7f28248e2748>,
 <TabularCPD representing P(D:2 | A:2) at 0x7f28248e26a0>]
>>> model.remove_nodes_from(['A', 'B'])
>>> model.get_cpds()
[<TabularCPD representing P(C:2 | D:2) at 0x7f28248e2a58>,
 <TabularCPD representing P(D:2) at 0x7f28248e26d8>]
to_junction_tree()[source]

Creates a junction tree (or clique tree) for a given bayesian model.

For converting a Bayesian Model into a Clique tree, first it is converted into a Markov one.

For a given markov model (H) a junction tree (G) is a graph 1. where each node in G corresponds to a maximal clique in H 2. each sepset in G separates the variables strictly on one side of the edge to other.

Examples

>>> from pgmpy.models import BayesianModel
>>> from pgmpy.factors.discrete import TabularCPD
>>> G = BayesianModel([('diff', 'grade'), ('intel', 'grade'),
...                    ('intel', 'SAT'), ('grade', 'letter')])
>>> diff_cpd = TabularCPD('diff', 2, [[0.2], [0.8]])
>>> intel_cpd = TabularCPD('intel', 3, [[0.5], [0.3], [0.2]])
>>> grade_cpd = TabularCPD('grade', 3,
...                        [[0.1,0.1,0.1,0.1,0.1,0.1],
...                         [0.1,0.1,0.1,0.1,0.1,0.1],
...                         [0.8,0.8,0.8,0.8,0.8,0.8]],
...                        evidence=['diff', 'intel'],
...                        evidence_card=[2, 3])
>>> sat_cpd = TabularCPD('SAT', 2,
...                      [[0.1, 0.2, 0.7],
...                       [0.9, 0.8, 0.3]],
...                      evidence=['intel'], evidence_card=[3])
>>> letter_cpd = TabularCPD('letter', 2,
...                         [[0.1, 0.4, 0.8],
...                          [0.9, 0.6, 0.2]],
...                         evidence=['grade'], evidence_card=[3])
>>> G.add_cpds(diff_cpd, intel_cpd, grade_cpd, sat_cpd, letter_cpd)
>>> jt = G.to_junction_tree()
to_markov_model()[source]

Converts bayesian model to markov model. The markov model created would be the moral graph of the bayesian model.

Examples

>>> from pgmpy.models import BayesianModel
>>> G = BayesianModel([('diff', 'grade'), ('intel', 'grade'),
...                    ('intel', 'SAT'), ('grade', 'letter')])
>>> mm = G.to_markov_model()
>>> mm.nodes()
['diff', 'grade', 'intel', 'SAT', 'letter']
>>> mm.edges()
[('diff', 'intel'), ('diff', 'grade'), ('intel', 'grade'),
('intel', 'SAT'), ('grade', 'letter')]

Markov Model

class pgmpy.models.MarkovModel.MarkovModel(ebunch=None)[source]

Base class for markov model.

A MarkovModel stores nodes and edges with potentials

MarkovModel holds undirected edges.

Parameters:

data : input graph

Data to initialize graph. If data=None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object.

Examples

Create an empty Markov Model with no nodes and no edges.

>>> from pgmpy.models import MarkovModel
>>> G = MarkovModel()

G can be grown in several ways.

Nodes:

Add one node at a time:

>>> G.add_node('a')

Add the nodes from any container (a list, set or tuple or the nodes from another graph).

>>> G.add_nodes_from(['a', 'b'])

Edges:

G can also be grown by adding edges.

Add one edge,

>>> G.add_edge('a', 'b')

a list of edges,

>>> G.add_edges_from([('a', 'b'), ('b', 'c')])

If some edges connect nodes not yet in the model, the nodes are added automatically. There are no errors when adding nodes or edges that already exist.

Shortcuts:

Many common graph features allow python syntax for speed reporting.

>>> 'a' in G     # check if node in graph
True
>>> len(G)  # number of nodes in graph
3
add_edge(u, v, **kwargs)[source]

Add an edge between u and v.

The nodes u and v will be automatically added if they are not already in the graph

Parameters:

u,v : nodes

Nodes can be any hashable Python object.

Examples

>>> from pgmpy.models import MarkovModel
>>> G = MarkovModel()
>>> G.add_nodes_from(['Alice', 'Bob', 'Charles'])
>>> G.add_edge('Alice', 'Bob')
add_factors(*factors)[source]

Associate a factor to the graph. See factors class for the order of potential values

Parameters:

*factor: pgmpy.factors.factors object

A factor object on any subset of the variables of the model which is to be associated with the model.

Returns:

None

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = MarkovModel([('Alice', 'Bob'), ('Bob', 'Charles'),
...                        ('Charles', 'Debbie'), ('Debbie', 'Alice')])
>>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[3, 2],
...                 values=np.random.rand(6))
>>> student.add_factors(factor)
check_model()[source]

Check the model for various errors. This method checks for the following errors -

  • Checks if the cardinalities of all the variables are consistent across all the factors.
  • Factors are defined for all the random variables.
Returns:

check: boolean

True if all the checks are passed

copy()[source]

Returns a copy of this Markov Model.

Returns:

MarkovModel: Copy of this Markov model.

Examples

>>> from pgmpy.factors.discrete import DiscreteFactor
>>> from pgmpy.models import MarkovModel
>>> G = MarkovModel()
>>> G.add_nodes_from([('a', 'b'), ('b', 'c')])
>>> G.add_edge(('a', 'b'), ('b', 'c'))
>>> G_copy = G.copy()
>>> G_copy.edges()

[((‘a’, ‘b’), (‘b’, ‘c’))]

>>> G_copy.nodes()

[(‘a’, ‘b’), (‘b’, ‘c’)]

>>> factor = DiscreteFactor([('a', 'b')], cardinality=[3],

... values=np.random.rand(3))

>>> G.add_factors(factor)
>>> G.get_factors()

[<DiscreteFactor representing phi((‘a’, ‘b’):3) at 0x...>]

>>> G_copy.get_factors()

[]

get_cardinality(node=None)[source]

Returns the cardinality of the node. If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Parameters:

node: any hashable python object (optional)

The node whose cardinality we want. If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = MarkovModel([('Alice', 'Bob'), ('Bob', 'Charles')])
>>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
...                 values=np.random.rand(4))
>>> student.add_factors(factor)
>>> student.get_cardinality(node='Alice')
2
>>> student.get_cardinality()
defaultdict(<class 'int'>, {'Bob': 2, 'Alice': 2})
get_factors(node=None)[source]

Returns all the factors containing the node. If node is not specified returns all the factors that have been added till now to the graph.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = MarkovModel([('Alice', 'Bob'), ('Bob', 'Charles')])
>>> factor1 = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
...                 values=np.random.rand(4))
>>> factor2 = DiscreteFactor(['Bob', 'Charles'], cardinality=[2, 3],
...                                     values=np.ones(6))
>>> student.add_factors(factor1,factor2)
>>> student.get_factors()
[<DiscreteFactor representing phi(Alice:2, Bob:2) at 0x7f8a0e9bf630>,
 <DiscreteFactor representing phi(Bob:2, Charles:3) at 0x7f8a0e9bf5f8>]
>>> student.get_factors('Alice')
[<DiscreteFactor representing phi(Alice:2, Bob:2) at 0x7f8a0e9bf630>]
get_local_independencies(latex=False)[source]

Returns all the local independencies present in the markov model.

Local independencies are the independence assertion in the form of .. math:: {X perp W - {X} - MB(X) | MB(X)} where MB is the markov blanket of all the random variables in X

Parameters:

latex: boolean

If latex=True then latex string of the indepedence assertion would be created

Examples

>>> from pgmpy.models import MarkovModel
>>> mm = MarkovModel()
>>> mm.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'])
>>> mm.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'),
...                    ('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'),
...                    ('x4', 'x7'), ('x5', 'x7')])
>>> mm.get_local_independecies()
get_partition_function()[source]

Returns the partition function for a given undirected graph.

A partition function is defined as

\sum_{X}(\prod_{i=1}^{m} \phi_i)

where m is the number of factors present in the graph and X are all the random variables present.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = MarkovModel()
>>> G.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'])
>>> G.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'),
...                   ('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'),
...                   ('x4', 'x7'), ('x5', 'x7')])
>>> phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in G.edges()]
>>> G.add_factors(*phi)
>>> G.get_partition_function()
markov_blanket(node)[source]

Returns a markov blanket for a random variable.

Markov blanket is the neighboring nodes of the given node.

Examples

>>> from pgmpy.models import MarkovModel
>>> mm = MarkovModel()
>>> mm.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'])
>>> mm.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'),
...                    ('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'),
...                    ('x4', 'x7'), ('x5', 'x7')])
>>> mm.markov_blanket('x1')
remove_factors(*factors)[source]

Removes the given factors from the added factors.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = MarkovModel([('Alice', 'Bob'), ('Bob', 'Charles')])
>>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
...                 values=np.random.rand(4))
>>> student.add_factors(factor)
>>> student.remove_factors(factor)
to_bayesian_model()[source]

Creates a Bayesian Model which is a minimum I-Map for this markov model.

The ordering of parents may not remain constant. It would depend on the ordering of variable in the junction tree (which is not constant) all the time.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> mm = MarkovModel()
>>> mm.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'])
>>> mm.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'),
...                    ('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'),
...                    ('x4', 'x7'), ('x5', 'x7')])
>>> phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in mm.edges()]
>>> mm.add_factors(*phi)
>>> bm = mm.to_bayesian_model()
to_factor_graph()[source]

Converts the markov model into factor graph.

A factor graph contains two types of nodes. One type corresponds to random variables whereas the second type corresponds to factors over these variables. The graph only contains edges between variables and factor nodes. Each factor node is associated with one factor whose scope is the set of variables that are its neighbors.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = MarkovModel([('Alice', 'Bob'), ('Bob', 'Charles')])
>>> factor1 = DiscreteFactor(['Alice', 'Bob'], [3, 2], np.random.rand(6))
>>> factor2 = DiscreteFactor(['Bob', 'Charles'], [2, 2], np.random.rand(4))
>>> student.add_factors(factor1, factor2)
>>> factor_graph = student.to_factor_graph()
to_junction_tree()[source]

Creates a junction tree (or clique tree) for a given markov model.

For a given markov model (H) a junction tree (G) is a graph 1. where each node in G corresponds to a maximal clique in H 2. each sepset in G separates the variables strictly on one side of the edge to other.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> mm = MarkovModel()
>>> mm.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'])
>>> mm.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'),
...                    ('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'),
...                    ('x4', 'x7'), ('x5', 'x7')])
>>> phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in mm.edges()]
>>> mm.add_factors(*phi)
>>> junction_tree = mm.to_junction_tree()
triangulate(heuristic='H6', order=None, inplace=False)[source]

Triangulate the graph.

If order of deletion is given heuristic algorithm will not be used.

Parameters:

heuristic: H1 | H2 | H3 | H4 | H5 | H6

The heuristic algorithm to use to decide the deletion order of the variables to compute the triangulated graph. Let X be the set of variables and X(i) denotes the i-th variable.

  • S(i) - The size of the clique created by deleting the variable.
  • E(i) - Cardinality of variable X(i).
  • M(i) - Maximum size of cliques given by X(i) and its adjacent nodes.
  • C(i) - Sum of size of cliques given by X(i) and its adjacent nodes.

The heuristic algorithm decide the deletion order if this way:

  • H1 - Delete the variable with minimal S(i).
  • H2 - Delete the variable with minimal S(i)/E(i).
  • H3 - Delete the variable with minimal S(i) - M(i).
  • H4 - Delete the variable with minimal S(i) - C(i).
  • H5 - Delete the variable with minimal S(i)/M(i).
  • H6 - Delete the variable with minimal S(i)/C(i).

order: list, tuple (array-like)

The order of deletion of the variables to compute the triagulated graph. If order is given heuristic algorithm will not be used.

inplace: True | False

if inplace is true then adds the edges to the object from which it is called else returns a new object.

Examples

>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = MarkovModel()
>>> G.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'])
>>> G.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'),
...                   ('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'),
...                   ('x4', 'x7'), ('x5', 'x7')])
>>> phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in G.edges()]
>>> G.add_factors(*phi)
>>> G_chordal = G.triangulate()

Factor Graph

class pgmpy.models.FactorGraph.FactorGraph(ebunch=None)[source]

Class for representing factor graph.

DiscreteFactor graph is a bipartite graph representing factorization of a function. They allow efficient computation of marginal distributions through sum-product algorithm.

A factor graph contains two types of nodes. One type corresponds to random variables whereas the second type corresponds to factors over these variables. The graph only contains edges between variables and factor nodes. Each factor node is associated with one factor whose scope is the set of variables that are its neighbors.

Parameters:

data: input graph

Data to initialize graph. If data=None (default) an empty graph is created. The data is an edge list.

Examples

Create an empty FactorGraph with no nodes and no edges

>>> from pgmpy.models import FactorGraph
>>> G = FactorGraph()

G can be grown by adding variable nodes as well as factor nodes

Nodes:

Add a node at a time or a list of nodes.

>>> G.add_node('a')
>>> G.add_nodes_from(['a', 'b'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1)
>>> G.add_nodes_from([phi1])

Edges:

G can also be grown by adding edges.

>>> G.add_edge('a', phi1)

or a list of edges

>>> G.add_edges_from([('a', phi1), ('b', phi1)])
add_edge(u, v, **kwargs)[source]

Add an edge between variable_node and factor_node.

Parameters:

u, v: nodes

Nodes can be any hashable Python object.

Examples

>>> from pgmpy.models import FactorGraph
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edge('a', phi1)
add_factors(*factors)[source]

Associate a factor to the graph. See factors class for the order of potential values.

Parameters:

*factor: pgmpy.factors.DiscreteFactor object

A factor object on any subset of the variables of the model which is to be associated with the model.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
check_model()[source]

Check the model for various errors. This method checks for the following errors. In the same time it also updates the cardinalities of all the random variables.

  • Check whether bipartite property of factor graph is still maintained or not.
  • Check whether factors are associated for all the random variables or not.
  • Check if factors are defined for each factor node or not.
  • Check if cardinality information for all the variables is availble or not.
  • Check if cardinality of random variable remains same across all the factors.
copy()[source]

Returns a copy of the model.

Returns:FactorGraph : Copy of FactorGraph

Examples

>>> import numpy as np
>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from([('a', 'b'), ('b', 'c')])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> G_copy = G.copy()
>>> G_copy.nodes()
[<Factor representing phi(b:2, c:2) at 0xb4badd4c>, 'b', 'c',
  'a', <Factor representing phi(a:2, b:2) at 0xb4badf2c>]
get_cardinality(node=None)[source]

Returns the cardinality of the node

Parameters:

node: any hashable python object (optional)

The node whose cardinality we want. If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Returns:

int or dict : If node is specified returns the cardinality of the node.

If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> G.add_factors(phi1, phi2)
>>> G.get_cardinality()
    defaultdict(<class 'int'>, {'c': 2, 'b': 2, 'a': 2})
>>> G.get_cardinality('a')
    2
get_factor_nodes()[source]

Returns factors nodes present in the graph.

Before calling this method make sure that all the factors are added properly.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_factors(phi1, phi2)
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> G.get_factor_nodes()
[<DiscreteFactor representing phi(b:2, c:2) at 0x4b8c7f0>,
 <DiscreteFactor representing phi(a:2, b:2) at 0x4b8c5b0>]
get_factors(node=None)[source]

Returns the factors that have been added till now to the graph.

If node is not None, it would return the factor corresponding to the given node.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> G.get_factors()
>>> G.get_factors(node=phi1)
get_partition_function()[source]

Returns the partition function for a given undirected graph.

A partition function is defined as

\sum_{X}(\prod_{i=1}^{m} \phi_i)

where m is the number of factors present in the graph and X are all the random variables present.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> G.get_factors()
>>> G.get_partition_function()
get_variable_nodes()[source]

Returns variable nodes present in the graph.

Before calling this method make sure that all the factors are added properly.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_factors(phi1, phi2)
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> G.get_variable_nodes()
['a', 'b']
remove_factors(*factors)[source]

Removes the given factors from the added factors.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1)
>>> G.remove_factors(phi1)
to_junction_tree()[source]

Create a junction treeo (or clique tree) for a given factor graph.

For a given factor graph (H) a junction tree (G) is a graph 1. where each node in G corresponds to a maximal clique in H 2. each sepset in G separates the variables strictly on one side of edge to other

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> mm = G.to_markov_model()
to_markov_model()[source]

Converts the factor graph into markov model.

A markov model contains nodes as random variables and edge between two nodes imply interaction between them.

Examples

>>> from pgmpy.models import FactorGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = FactorGraph()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> G.add_nodes_from([phi1, phi2])
>>> G.add_edges_from([('a', phi1), ('b', phi1),
...                   ('b', phi2), ('c', phi2)])
>>> mm = G.to_markov_model()

Cluster Graph

class pgmpy.models.ClusterGraph.ClusterGraph(ebunch=None)[source]

Base class for representing Cluster Graph.

Cluster graph is an undirected graph which is associated with a subset of variables. The graph contains undirected edges that connects clusters whose scopes have a non-empty intersection.

Formally, a cluster graph is \mathcal{U} for a set of factors \Phi over \mathcal{X} is an undirected graph, each of whose nodes i is associated with a subset C_i \subseteq X. A cluster graph must be family-preserving - each factor \phi \in \Phi must be associated with a cluster C, denoted \alpha(\phi), such that Scope[\phi] \subseteq C_i. Each edge between a pair of clusters C_i and C_j is associated with a sepset S_{i,j} \subseteq C_i \cap C_j.

Parameters:

data: input graph

Data to initialize graph. If data=None (default) an empty graph is created. The data is an edge list

Examples

Create an empty ClusterGraph with no nodes and no edges

>>> from pgmpy.models import ClusterGraph
>>> G = ClusterGraph()

G can be grown by adding clique nodes.

Nodes:

Add a tuple (or list or set) of nodes as single clique node.

>>> G.add_node(('a', 'b', 'c'))
>>> G.add_nodes_from([('a', 'b'), ('a', 'b', 'c')])

Edges:

G can also be grown by adding edges.

>>> G.add_edge(('a', 'b', 'c'), ('a', 'b'))

or a list of edges

>>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
...                   (('a', 'b', 'c'), ('a', 'c'))])
add_edge(u, v, **kwargs)[source]

Add an edge between two clique nodes.

Parameters:

u, v: nodes

Nodes can be any list or set or tuple of nodes forming a clique.

Examples

>>> from pgmpy.models import ClusterGraph
>>> G = ClusterGraph()
>>> G.add_nodes_from([('a', 'b', 'c'), ('a', 'b'), ('a', 'c')])
>>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
...                   (('a', 'b', 'c'), ('a', 'c'))])
add_factors(*factors)[source]

Associate a factor to the graph. See factors class for the order of potential values

Parameters:

*factor: pgmpy.factors.factors object

A factor object on any subset of the variables of the model which is to be associated with the model.

Returns:

None

Examples

>>> from pgmpy.models import ClusterGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = ClusterGraph()
>>> student.add_node(('Alice', 'Bob'))
>>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[3, 2],
...                 values=np.random.rand(6))
>>> student.add_factors(factor)
add_node(node, **kwargs)[source]

Add a single node to the cluster graph.

Parameters:

node: node

A node should be a collection of nodes forming a clique. It can be a list, set or tuple of nodes

Examples

>>> from pgmpy.models import ClusterGraph
>>> G = ClusterGraph()
>>> G.add_node(('a', 'b', 'c'))
add_nodes_from(nodes, **kwargs)[source]

Add multiple nodes to the cluster graph.

Parameters:

nodes: iterable container

A container of nodes (list, dict, set, etc.).

Examples

>>> from pgmpy.models import ClusterGraph
>>> G = ClusterGraph()
>>> G.add_nodes_from([('a', 'b'), ('a', 'b', 'c')])
check_model()[source]

Check the model for various errors. This method checks for the following errors.

  • Checks if factors are defined for all the cliques or not.
  • Check for running intersection property is not done explicitly over here as it done in the add_edges method.
  • Checks if cardinality information for all the variables is availble or not. If not it raises an error.
  • Check if cardinality of random variable remains same across all the factors.
Returns:

check: boolean

True if all the checks are passed

copy()[source]

Returns a copy of ClusterGraph.

Returns:

ClusterGraph: copy of ClusterGraph

Examples

>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = ClusterGraph()
>>> G.add_nodes_from([('a', 'b'), ('b', 'c')])
>>> G.add_edge(('a', 'b'), ('b', 'c'))
>>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2)
>>> graph_copy = G.copy()
>>> graph_copy.factors

[<DiscreteFactor representing phi(a:2, b:2) at 0xb71b19cc>,

<DiscreteFactor representing phi(b:2, c:2) at 0xb4eaf3ac>]

>>> graph_copy.edges()

[((‘a’, ‘b’), (‘b’, ‘c’))]

>>> graph_copy.nodes()

[(‘a’, ‘b’), (‘b’, ‘c’)]

get_cardinality(node=None)[source]

Returns the cardinality of the node

Parameters:

node: any hashable python object (optional)

The node whose cardinality we want. If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Returns:

int or dict : If node is specified returns the cardinality of the node.

If node is not specified returns a dictionary with the given variable as keys and their respective cardinality as values.

Examples

>>> from pgmpy.models import ClusterGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = ClusterGraph()
>>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
...                 values=np.random.rand(4))
>>> student.add_node(('Alice', 'Bob'))
>>> student.add_factors(factor)
>>> student.get_cardinality()
defaultdict(<class 'int'>, {'Bob': 2, 'Alice': 2})
>>> student.get_cardinality(node='Alice')
2
get_factors(node=None)[source]

Return the factors that have been added till now to the graph.

If node is not None, it would return the factor corresponding to the given node.

Examples

>>> from pgmpy.models import ClusterGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = ClusterGraph()
>>> G.add_nodes_from([('a', 'b', 'c'), ('a', 'b'), ('a', 'c')])
>>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
...                   (('a', 'b', 'c'), ('a', 'c'))])
>>> phi1 = DiscreteFactor(['a', 'b', 'c'], [2, 2, 2], np.random.rand(8))
>>> phi2 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi3 = DiscreteFactor(['a', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2, phi3)
>>> G.get_factors()
>>> G.get_factors(node=('a', 'b', 'c'))
get_partition_function()[source]

Returns the partition function for a given undirected graph.

A partition function is defined as

\sum_{X}(\prod_{i=1}^{m} \phi_i)

where m is the number of factors present in the graph and X are all the random variables present.

Examples

>>> from pgmpy.models import ClusterGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> G = ClusterGraph()
>>> G.add_nodes_from([('a', 'b', 'c'), ('a', 'b'), ('a', 'c')])
>>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
...                   (('a', 'b', 'c'), ('a', 'c'))])
>>> phi1 = DiscreteFactor(['a', 'b', 'c'], [2, 2, 2], np.random.rand(8))
>>> phi2 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
>>> phi3 = DiscreteFactor(['a', 'c'], [2, 2], np.random.rand(4))
>>> G.add_factors(phi1, phi2, phi3)
>>> G.get_partition_function()
remove_factors(*factors)[source]

Removes the given factors from the added factors.

Examples

>>> from pgmpy.models import ClusterGraph
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> student = ClusterGraph()
>>> factor = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
...                 value=np.random.rand(4))
>>> student.add_factors(factor)
>>> student.remove_factors(factor)

Junction Tree

class pgmpy.models.JunctionTree.JunctionTree(ebunch=None)[source]

Class for representing Junction Tree.

Junction tree is undirected graph where each node represents a clique (list, tuple or set of nodes) and edges represent sepset between two cliques. Each sepset in G separates the variables strictly on one side of edge to other.

Parameters:

data: input graph

Data to initialize graph. If data=None (default) an empty graph is created. The data is an edge list.

Examples

Create an empty JunctionTree with no nodes and no edges

>>> from pgmpy.models import JunctionTree
>>> G = JunctionTree()

G can be grown by adding clique nodes.

Nodes:

Add a tuple (or list or set) of nodes as single clique node.

>>> G.add_node(('a', 'b', 'c'))
>>> G.add_nodes_from([('a', 'b'), ('a', 'b', 'c')])

Edges:

G can also be grown by adding edges.

>>> G.add_edge(('a', 'b', 'c'), ('a', 'b'))

or a list of edges

>>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
...                   (('a', 'b', 'c'), ('a', 'c'))])
add_edge(u, v, **kwargs)[source]

Add an edge between two clique nodes.

Parameters:

u, v: nodes

Nodes can be any list or set or tuple of nodes forming a clique.

Examples

>>> from pgmpy.models import JunctionTree
>>> G = JunctionTree()
>>> G.add_nodes_from([('a', 'b', 'c'), ('a', 'b'), ('a', 'c')])
>>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
...                   (('a', 'b', 'c'), ('a', 'c'))])
check_model()[source]

Check the model for various errors. This method checks for the following errors. In the same time also updates the cardinalities of all the random variables.

  • Checks if clique potentials are defined for all the cliques or not.
  • Check for running intersection property is not done explicitly over here as it done in the add_edges method.
Returns:

check: boolean

True if all the checks are passed

copy()[source]

Returns a copy of JunctionTree.

Returns:JunctionTree : copy of JunctionTree

Examples

>>> import numpy as np
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> from pgmpy.models import JunctionTree
>>> G = JunctionTree()
>>> G.add_edges_from([(('a', 'b', 'c'), ('a', 'b')), (('a', 'b', 'c'), ('a', 'c'))])
>>> phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2))
>>> phi2 = DiscreteFactor(['a', 'c'], [1, 2], np.random.rand(2))
>>> G.add_factors(phi1,phi2)
>>> modelCopy = G.copy()
>>> modelCopy.edges()
[(('a', 'b'), ('a', 'b', 'c')), (('a', 'c'), ('a', 'b', 'c'))]
>>> G.factors
[<DiscreteFactor representing phi(a:1, b:2) at 0xb720ee4c>,
 <DiscreteFactor representing phi(a:1, c:2) at 0xb4e1e06c>]
>>> modelCopy.factors
[<DiscreteFactor representing phi(a:1, b:2) at 0xb4bd11ec>,
 <DiscreteFactor representing phi(a:1, c:2) at 0xb4bd138c>]

Markov Chain

class pgmpy.models.MarkovChain.MarkovChain(variables=None, card=None, start_state=None)[source]

Class to represent a Markov Chain with multiple kernels for factored state space, along with methods to simulate a run.

add_transition_model(variable, transition_model)[source]

Adds a transition model for a particular variable.

add_variable(variable, card=0)[source]

Add a variable to the model.

add_variables_from(variables, cards)[source]

Add several variables to the model at once.

copy()[source]

Returns a copy of Markov Chain Model.

generate_sample(start_state=None, size=1)[source]

Generator version of self.sample

is_stationarity(tolerance=0.2, sample=None)[source]

Checks if the given markov chain is stationary and checks the steady state probablity values for the state are consistent.

prob_from_sample(state, sample=None, window_size=None)[source]

Given an instantiation (partial or complete) of the variables of the model, compute the probability of observing it over multiple windows in a given sample.

If ‘sample’ is not passed as an argument, generate the statistic by sampling from the Markov Chain, starting with a random initial state.

random_state()[source]

Generates a random state of the Markov Chain.

sample(start_state=None, size=1)[source]

Sample from the Markov Chain.

set_start_state(start_state)[source]

Set the start state of the Markov Chain. If the start_state is given as a array-like iterable, its contents are reordered in the internal representation.

NoisyOr Model

class pgmpy.models.NoisyOrModel.NoisyOrModel(variables, cardinality, inhibitor_probability)[source]

Base class for Noisy-Or models.

This is an implementation of generalized Noisy-Or models and is not limited to Boolean variables and also any arbitrary function can be used instead of the boolean OR function.

Reference: http://xenon.stanford.edu/~srinivas/research/6-UAI93-Srinivas-Generalization-of-Noisy-Or.pdf

add_variables(variables, cardinality, inhibitor_probability)[source]

Adds variables to the NoisyOrModel.

Parameters:

variables: list, tuple, dict (array like)

array containing names of the variables that are to be added.

cardinality: list, tuple, dict (array like)

array containing integers representing the cardinality of the variables.

inhibitor_probability: list, tuple, dict (array_like)

array containing the inhibitor probabilities corresponding to each variable.

Examples

>>> from pgmpy.models import NoisyOrModel
>>> model = NoisyOrModel(['x1', 'x2', 'x3'], [2, 3, 2], [[0.6, 0.4],
...                                                      [0.2, 0.4, 0.7],
...                                                      [0.1, 0. 4]])
>>> model.add_variables(['x4'], [3], [0.1, 0.4, 0.2])
del_variables(variables)[source]

Deletes variables from the NoisyOrModel.

Parameters:

variables: list, tuple, dict (array like)

list of variables to be deleted.

Examples

>>> from pgmpy.models import NoisyOrModel
>>> model = NoisyOrModel(['x1', 'x2', 'x3'], [2, 3, 2], [[0.6, 0.4],
...                                                      [0.2, 0.4, 0.7],
...                                                      [0.1, 0. 4]])
>>> model.del_variables(['x1'])

Naive Bayes

class pgmpy.models.NaiveBayes.NaiveBayes(ebunch=None)[source]

Class to represent Naive Bayes. Subclass of Bayesian Model. Model holds directed edges from one parent node to multiple children nodes only.

Parameters:

data : input graph

Data to initialize graph. If data=None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object.

Examples

Create an empty Naive Bayes Model with no nodes and no edges.

>>> from pgmpy.models import NaiveBayes
>>> G = NaiveBayes()

G can be grown in several ways.

Nodes:

Add one node at a time:

>>> G.add_node('a')

Add the nodes from any container (a list, set or tuple or the nodes from another graph).

>>> G.add_nodes_from(['a', 'b', 'c'])

Edges:

G can also be grown by adding edges.

Add one edge,

>>> G.add_edge('a', 'b')

a list of edges,

>>> G.add_edges_from([('a', 'b'), ('a', 'c')])

If some edges connect nodes not yet in the model, the nodes are added automatically. There are no errors when adding nodes or edges that already exist.

Shortcuts:

Many common graph features allow python syntax for speed reporting.

>>> 'a' in G     # check if node in graph
True
>>> len(G)  # number of nodes in graph
3
active_trail_nodes(start, observed=None)[source]

Returns all the nodes reachable from start via an active trail.

Parameters:

start: Graph node

observed : List of nodes (optional)

If given the active trail would be computed assuming these nodes to be observed.

Examples

>>> from pgmpy.models import NaiveBayes
>>> model = NaiveBayes()
>>> model.add_nodes_from(['a', 'b', 'c', 'd'])
>>> model.add_edges_from([('a', 'b'), ('a', 'c'), ('a', 'd')])
>>> model.active_trail_nodes('a')
{'a', 'b', 'c', 'd'}
>>> model.active_trail_nodes('a', ['b', 'c'])
{'a', 'd'}
>>> model.active_trail_nodes('b', ['a'])
{'b'}
add_edge(u, v, *kwargs)[source]

Add an edge between u and v.

The nodes u and v will be automatically added if they are not already in the graph

Parameters:

u,v : nodes

Nodes can be any hashable python object.

Examples

>>> from pgmpy.models import NaiveBayes
>>> G = NaiveBayes()
>>> G.add_nodes_from(['a', 'b', 'c'])
>>> G.add_edge('a', 'b')
>>> G.add_edge('a', 'c')
>>> G.edges()
[('a', 'c'), ('a', 'b')]
fit(data, parent_node=None, estimator=None)[source]

Computes the CPD for each node from a given data in the form of a pandas dataframe. If a variable from the data is not present in the model, it adds that node into the model.

Parameters:

data : pandas DataFrame object

A DataFrame object with column names same as the variable names of network

parent_node: any hashable python object (optional)

Parent node of the model, if not specified it looks for a previously specified parent node.

estimator: Estimator class

Any pgmpy estimator. If nothing is specified, the default MaximumLikelihoodEstimator would be used.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from pgmpy.models import NaiveBayes
>>> model = NaiveBayes()
>>> values = pd.DataFrame(np.random.randint(low=0, high=2, size=(1000, 5)),
...                       columns=['A', 'B', 'C', 'D', 'E'])
>>> model.fit(values, 'A')
>>> model.get_cpds()
[<TabularCPD representing P(D:2 | A:2) at 0x4b72870>,
 <TabularCPD representing P(E:2 | A:2) at 0x4bb2150>,
 <TabularCPD representing P(A:2) at 0x4bb23d0>,
 <TabularCPD representing P(B:2 | A:2) at 0x4bb24b0>,
 <TabularCPD representing P(C:2 | A:2) at 0x4bb2750>]
>>> model.edges()
[('A', 'D'), ('A', 'E'), ('A', 'B'), ('A', 'C')]
local_independencies(variables)[source]

Returns an instance of Independencies containing the local independencies of each of the variables.

Parameters:

variables: str or array like

variables whose local independencies are to found.

Examples

>>> from pgmpy.models import NaiveBayes
>>> model = NaiveBayes()
>>> model.add_edges_from([('a', 'b'), ('a', 'c'), ('a', 'd')])
>>> ind = model.local_independencies('b')
>>> ind
(b _|_ d, c | a)

DynamicBayesianNetwork

class pgmpy.models.DynamicBayesianNetwork.DynamicBayesianNetwork(ebunch=None)[source]

Bases: pgmpy.base.DirectedGraph.DirectedGraph

add_cpds(*cpds)[source]

This method adds the cpds to the dynamic bayesian network. Note that while adding variables and the evidence in cpd, they have to be of the following form (node_name, time_slice) Here, node_name is the node that is inserted while the time_slice is an integer value, which denotes the index of the time_slice that the node belongs to.

Parameters:

cpds : list, set, tuple (array-like)

List of CPDs which are to be associated with the model. Each CPD should be an instance of TabularCPD.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> from pgmpy.factors.discrete import TabularCPD
>>> dbn = DBN()
>>> dbn.add_edges_from([(('D', 0),('G', 0)),(('I', 0),('G', 0)),(('D', 0),('D', 1)),(('I', 0),('I', 1))])
>>> grade_cpd = TabularCPD(('G', 0), 3, [[0.3, 0.05, 0.9, 0.5],
...                                      [0.4, 0.25, 0.8, 0.03],
...                                      [0.3, 0.7, 0.02, 0.2]],
...                        evidence=[('I', 0),('D', 0)],
...                        evidence_card=[2, 2])
>>> d_i_cpd = TabularCPD(('D',1), 2, [[0.6, 0.3],
...                                   [0.4, 0.7]],
...                      evidence=[('D',0)],
...                      evidence_card=2)
>>> diff_cpd = TabularCPD(('D', 0), 2, [[0.6, 0.4]])
>>> intel_cpd = TabularCPD(('I', 0), 2, [[0.7, 0.3]])
>>> i_i_cpd = TabularCPD(('I', 1), 2, [[0.5, 0.4],
...                                    [0.5, 0.6]],
...                      evidence=[('I', 0)],
...                      evidence_card=2)
>>> dbn.add_cpds(grade_cpd, d_i_cpd, diff_cpd, intel_cpd, i_i_cpd)
>>> dbn.get_cpds()
[<TabularCPD representing P(('G', 0):3 | ('I', 0):2, ('D', 0):2) at 0x7ff7f27b0cf8>,
 <TabularCPD representing P(('D', 1):2 | ('D', 0):2) at 0x7ff810b9c2e8>,
 <TabularCPD representing P(('D', 0):2) at 0x7ff7f27e6f98>,
 <TabularCPD representing P(('I', 0):2) at 0x7ff7f27e6ba8>,
 <TabularCPD representing P(('I', 1):2 | ('I', 0):2) at 0x7ff7f27e6668>]
add_cycle(nodes, **attr)

Add a cycle.

Parameters:

nodes: iterable container

A container of nodes. A cycle will be constructed from the nodes (in order) and added to the graph.

attr : keyword arguments, optional (default= no attributes)

Attributes to add to every edge in cycle.

See also

add_path, add_star

Examples

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_cycle([0,1,2,3])
>>> G.add_cycle([10,11,12],weight=7)
add_edge(start, end, **kwargs)[source]

Add an edge between two nodes.

The nodes will be automatically added if they are not present in the network.

Parameters:

start: tuple

Both the start and end nodes should specify the time slice as (node_name, time_slice). Here, node_name can be any hashable python object while the time_slice is an integer value, which denotes the time slice that the node belongs to.

end: tuple

Both the start and end nodes should specify the time slice as (node_name, time_slice). Here, node_name can be any hashable python object while the time_slice is an integer value, which denotes the time slice that the node belongs to.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> model = DBN()
>>> model.add_nodes_from(['D', 'I'])
>>> model.add_edge(('D',0), ('I',0))
>>> model.edges()
[(('D', 1), ('I', 1)), (('D', 0), ('I', 0))]
add_edges_from(ebunch, **kwargs)[source]

Add all the edges in ebunch.

If nodes referred in the ebunch are not already present, they will be automatically added. Node names can be any hashable python object.

Parameters:

ebunch : list, array-like

List of edges to add. Each edge must be of the form of ((start, time_slice), (end, time_slice)).

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_edges_from([(('D', 0), ('G', 0)), (('I', 0), ('G', 0))])
>>> dbn.nodes()
['G', 'I', 'D']
>>> dbn.edges()
[(('D', 1), ('G', 1)),
 (('I', 1), ('G', 1)),
 (('D', 0), ('G', 0)),
 (('I', 0), ('G', 0))]
add_node(node, **attr)[source]

Adds a single node to the Network

Parameters:

node: node

A node can be any hashable Python object.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_node('A')
['A']
add_nodes_from(nodes, **attr)[source]

Add multiple nodes to the Network.

Parameters:

nodes: iterable container

A container of nodes (list, dict, set, etc.).

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_nodes_from(['A', 'B', 'C'])
add_path(nodes, **attr)

Add a path.

Parameters:

nodes : iterable container

A container of nodes. A path will be constructed from the nodes (in order) and added to the graph.

attr : keyword arguments, optional (default= no attributes)

Attributes to add to every edge in path.

See also

add_star, add_cycle

Examples

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.add_path([10,11,12],weight=7)
add_star(nodes, **attr)

Add a star.

The first node in nodes is the middle of the star. It is connected to all other nodes.

Parameters:

nodes : iterable container

A container of nodes.

attr : keyword arguments, optional (default= no attributes)

Attributes to add to every edge in star.

See also

add_path, add_cycle

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_star([0,1,2,3])
>>> G.add_star([10,11,12],weight=2)
add_weighted_edges_from(ebunch, weight='weight', **attr)

Add all the edges in ebunch as weighted edges with specified weights.

Parameters:

ebunch : container of edges

Each edge given in the list or container will be added to the graph. The edges must be given as 3-tuples (u,v,w) where w is a number.

weight : string, optional (default= ‘weight’)

The attribute name for the edge weights to be added.

attr : keyword arguments, optional (default= no attributes)

Edge attributes to add/update for all edges.

See also

add_edge
add a single edge
add_edges_from
add multiple edges

Notes

Adding the same edge twice for Graph/DiGraph simply updates the edge data. For MultiGraph/MultiDiGraph, duplicate edges are stored.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)])
adjacency_iter()

Return an iterator of (node, adjacency dict) tuples for all nodes.

This is the fastest way to look at every edge. For directed graphs, only outgoing adjacencies are included.

Returns:

adj_iter : iterator

An iterator of (node, adjacency dictionary) for all nodes in the graph.

See also

adjacency_list

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> [(n,nbrdict) for n,nbrdict in G.adjacency_iter()]
[(0, {1: {}}), (1, {0: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}})]
adjacency_list()

Return an adjacency list representation of the graph.

The output adjacency list is in the order of G.nodes(). For directed graphs, only outgoing adjacencies are included.

Returns:

adj_list : lists of lists

The adjacency structure of the graph as a list of lists.

See also

adjacency_iter

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.adjacency_list() # in order given by G.nodes()
[[1], [0, 2], [1, 3], [2]]
adjlist_dict_factory

alias of dict

check_model()[source]

Check the model for various errors. This method checks for the following errors.

  • Checks if the sum of the probabilities in each associated CPD for each
    state is equal to 1 (tol=0.01).
  • Checks if the CPDs associated with nodes are consistent with their parents.
Returns:

boolean: True if everything seems to be order. Otherwise raises error

according to the problem.

clear()

Remove all nodes and edges from the graph.

This also removes the name, and all graph, node, and edge attributes.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.clear()
>>> G.nodes()
[]
>>> G.edges()
[]
copy()[source]

Returns a copy of the dynamic bayesian network.

Returns:DynamicBayesianNetwork: copy of the dynamic bayesian network

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> from pgmpy.factors.discrete import TabularCPD
>>> dbn = DBN()
>>> dbn.add_edges_from([(('D',0),('G',0)),(('I',0),('G',0)),(('D',0),('D',1)),(('I',0),('I',1))])
>>> grade_cpd =  TabularCPD(('G',0), 3, [[0.3,0.05,0.9,0.5],
                                [0.4,0.25,0.8,0.03],
                                [0.3,0.7,0.02,0.2]], [('I', 0),('D', 0)],[2,2])
>>> dbn.add_cpds(grade_cpd)
>>> dbn_copy = dbn.copy()
>>> dbn_copy.nodes()
['Z', 'G', 'I', 'D']
>>> dbn_copy.edges()
[(('I', 1), ('G', 1)),
(('I', 0), ('I', 1)),
(('I', 0), ('G', 0)),
(('D', 1), ('G', 1)),
(('D', 0), ('G', 0)),
(('D', 0), ('D', 1))]
>> dbn_copy.get_cpds()
[<TabularCPD representing P(('G', 0):3 | ('I', 0):2, ('D', 0):2) at 0x7f13961a3320>]
degree(nbunch=None, weight=None)

Return the degree of a node or nodes.

The node degree is the number of edges adjacent to that node.

Parameters:

nbunch : iterable container, optional (default=all nodes)

A container of nodes. The container will be iterated through once.

weight : string or None, optional (default=None)

The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

Returns:

nd : dictionary, or number

A dictionary with nodes as keys and degree as values or a number if a single node is specified.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.degree(0)
1
>>> G.degree([0,1])
{0: 1, 1: 2}
>>> list(G.degree([0,1]).values())
[1, 2]
degree_iter(nbunch=None, weight=None)

Return an iterator for (node, degree).

The node degree is the number of edges adjacent to the node.

Parameters:

nbunch : iterable container, optional (default=all nodes)

A container of nodes. The container will be iterated through once.

weight : string or None, optional (default=None)

The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

Returns:

nd_iter : an iterator

The iterator returns two-tuples of (node, degree).

Examples

>>> G = nx.DiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> list(G.degree_iter(0)) # node 0 with degree 1
[(0, 1)]
>>> list(G.degree_iter([0,1]))
[(0, 1), (1, 2)]
edge_attr_dict_factory

alias of dict

edges(nbunch=None, data=False, default=None)

Return a list of edges.

Edges are returned as tuples with optional data in the order (node, neighbor, data).

Parameters:

nbunch : iterable container, optional (default= all nodes)

A container of nodes. The container will be iterated through once.

data : string or bool, optional (default=False)

The edge attribute returned in 3-tuple (u,v,ddict[data]). If True, return edge attribute dict in 3-tuple (u,v,ddict). If False, return 2-tuple (u,v).

default : value, optional (default=None)

Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.

Returns:

edge_list: list of edge tuples

Edges that are adjacent to any node in nbunch, or a list of all edges if nbunch is not specified.

See also

edges_iter
return an iterator over the edges

Notes

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> G.edges()
[(0, 1), (1, 2), (2, 3)]
>>> G.edges(data=True) # default edge data is {} (empty dictionary)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> G.edges([0,3])
[(0, 1), (3, 2)]
>>> G.edges(0)
[(0, 1)]
edges_iter(nbunch=None, data=False, default=None)

Return an iterator over the edges.

Edges are returned as tuples with optional data in the order (node, neighbor, data).

Parameters:

nbunch : iterable container, optional (default= all nodes)

A container of nodes. The container will be iterated through once.

data : string or bool, optional (default=False)

The edge attribute returned in 3-tuple (u,v,ddict[data]). If True, return edge attribute dict in 3-tuple (u,v,ddict). If False, return 2-tuple (u,v).

default : value, optional (default=None)

Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.

Returns:

edge_iter : iterator

An iterator of (u,v) or (u,v,d) tuples of edges.

See also

edges
return a list of edges

Notes

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

Examples

>>> G = nx.DiGraph()   # or MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> [e for e in G.edges_iter()]
[(0, 1), (1, 2), (2, 3)]
>>> list(G.edges_iter(data=True)) # default data is {} (empty dict)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1)) 
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> list(G.edges_iter([0,2]))
[(0, 1), (2, 3)]
>>> list(G.edges_iter(0))
[(0, 1)]
get_cpds(node=None, time_slice=0)[source]

Returns the CPDs that have been associated with the network.

Parameters:

node: tuple (node_name, time_slice)

The node should be in the following form (node_name, time_slice). Here, node_name is the node that is inserted while the time_slice is an integer value, which denotes the index of the time_slice that the node belongs to.

time_slice: int

The time_slice should be a positive integer greater than or equal to zero.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> from pgmpy.factors.discrete import TabularCPD
>>> dbn = DBN()
>>> dbn.add_edges_from([(('D',0),('G',0)),(('I',0),('G',0)),(('D',0),('D',1)),(('I',0),('I',1))])
>>> grade_cpd =  TabularCPD(('G',0), 3, [[0.3,0.05,0.9,0.5],
...                                      [0.4,0.25,0.8,0.03],
...                                      [0.3,0.7,0.02,0.2]], [('I', 0),('D', 0)],[2,2])
>>> dbn.add_cpds(grade_cpd)
>>> dbn.get_cpds()
get_edge_data(u, v, default=None)

Return the attribute dictionary associated with edge (u,v).

Parameters:

u, v : nodes

default: any Python object (default=None)

Value to return if the edge (u,v) is not found.

Returns:

edge_dict : dictionary

The edge attribute dictionary.

Notes

It is faster to use G[u][v].

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G[0][1]
{}

Warning: Assigning G[u][v] corrupts the graph data structure. But it is safe to assign attributes to that dictionary,

>>> G[0][1]['weight'] = 7
>>> G[0][1]['weight']
7
>>> G[1][0]['weight']
7

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.get_edge_data(0,1) # default edge data is {}
{}
>>> e = (0,1)
>>> G.get_edge_data(*e) # tuple form
{}
>>> G.get_edge_data('a','b',default=0) # edge not in graph, return 0
0
get_inter_edges()[source]

Returns the inter-slice edges present in the 2-TBN.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_edges_from([(('D', 0), ('G', 0)), (('I', 0), ('G', 0)),
...                     (('G', 0), ('L', 0)), (('D', 0), ('D', 1)),
...                     (('I', 0), ('I', 1)), (('G', 0), ('G', 1)),
...                     (('G', 0), ('L', 1)), (('L', 0), ('L', 1))])
>>> dbn.get_inter_edges()
[(('D', 0), ('D', 1)),
 (('G', 0), ('G', 1)),
 (('G', 0), ('L', 1)),
 (('I', 0), ('I', 1)),
 (('L', 0), ('L', 1))]
get_interface_nodes(time_slice=0)[source]

Returns the nodes in the first timeslice whose children are present in the first timeslice.

Parameters:

time_slice:int

The timeslice should be a positive value greater than or equal to zero

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_nodes_from(['D', 'G', 'I', 'S', 'L'])
>>> dbn.add_edges_from([(('D',0),('G',0)),(('I',0),('G',0)),(('G',0),('L',0)),(('D',0),('D',1))])
>>> dbn.get_interface_nodes()
[('D', 0)]
get_intra_edges(time_slice=0)[source]

Returns the intra slice edges present in the 2-TBN.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_nodes_from(['D', 'G', 'I', 'S', 'L'])
>>> dbn.add_edges_from([(('D', 0), ('G', 0)), (('I', 0), ('G', 0)),
...                     (('G', 0), ('L', 0)), (('D', 0), ('D', 1)),
...                     (('I', 0), ('I', 1)), (('G', 0), ('G', 1)),
...                     (('G', 0), ('L', 1)), (('L', 0), ('L', 1))])
>>> dbn.get_intra_edges()
[(('D', 0), ('G', 0)), (('G', 0), ('L', 0)), (('I', 0), ('G', 0))
get_leaves()

Returns a list of leaves of the graph.

Examples

>>> from pgmpy.base import DirectedGraph
>>> graph = DirectedGraph([('A', 'B'), ('B', 'C'), ('B', 'D')])
>>> graph.get_leaves()
['C', 'D']
get_parents(node)

Returns a list of parents of node.

Throws an error if the node is not present in the graph.

Parameters:

node: string, int or any hashable python object.

The node whose parents would be returned.

Examples

>>> from pgmpy.base import DirectedGraph
>>> G = DirectedGraph(ebunch=[('diff', 'grade'), ('intel', 'grade')])
>>> G.parents(node='grade')
['diff', 'intel']
get_roots()

Returns a list of roots of the graph.

Examples

>>> from pgmpy.base import DirectedGraph
>>> graph = DirectedGraph([('A', 'B'), ('B', 'C'), ('B', 'D'), ('E', 'B')])
>>> graph.get_roots()
['A', 'E']
get_slice_nodes(time_slice=0)[source]

Returns the nodes present in a particular timeslice

Parameters:

time_slice:int

The timeslice should be a positive value greater than or equal to zero

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_nodes_from(['D', 'G', 'I', 'S', 'L'])
>>> dbn.add_edges_from([(('D', 0),('G', 0)),(('I', 0),('G', 0)),(('G', 0),('L', 0)),(('D', 0),('D', 1))])
>>> dbn.get_slice_nodes()
has_edge(u, v)

Return True if the edge (u,v) is in the graph.

Parameters:

u, v : nodes

Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects.

Returns:

edge_ind : bool

True if edge is in the graph, False otherwise.

Examples

Can be called either using two nodes u,v or edge tuple (u,v)

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.has_edge(0,1)  # using two nodes
True
>>> e = (0,1)
>>> G.has_edge(*e)  #  e is a 2-tuple (u,v)
True
>>> e = (0,1,{'weight':7})
>>> G.has_edge(*e[:2])  # e is a 3-tuple (u,v,data_dictionary)
True

The following syntax are all equivalent:

>>> G.has_edge(0,1)
True
>>> 1 in G[0]  # though this gives KeyError if 0 not in G
True
has_node(n)

Return True if the graph contains the node n.

Parameters:n : node

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.has_node(0)
True

It is more readable and simpler to use

>>> 0 in G
True
has_predecessor(u, v)

Return True if node u has predecessor v.

This is true if graph has the edge u<-v.

has_successor(u, v)

Return True if node u has successor v.

This is true if graph has the edge u->v.

in_degree(nbunch=None, weight=None)

Return the in-degree of a node or nodes.

The node in-degree is the number of edges pointing in to the node.

Parameters:

nbunch : iterable container, optional (default=all nodes)

A container of nodes. The container will be iterated through once.

weight : string or None, optional (default=None)

The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

Returns:

nd : dictionary, or number

A dictionary with nodes as keys and in-degree as values or a number if a single node is specified.

Examples

>>> G = nx.DiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.in_degree(0)
0
>>> G.in_degree([0,1])
{0: 0, 1: 1}
>>> list(G.in_degree([0,1]).values())
[0, 1]
in_degree_iter(nbunch=None, weight=None)

Return an iterator for (node, in-degree).

The node in-degree is the number of edges pointing in to the node.

Parameters:

nbunch : iterable container, optional (default=all nodes)

A container of nodes. The container will be iterated through once.

weight : string or None, optional (default=None)

The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

Returns:

nd_iter : an iterator

The iterator returns two-tuples of (node, in-degree).

Examples

>>> G = nx.DiGraph()
>>> G.add_path([0,1,2,3])
>>> list(G.in_degree_iter(0)) # node 0 with degree 0
[(0, 0)]
>>> list(G.in_degree_iter([0,1]))
[(0, 0), (1, 1)]
in_edges(nbunch=None, data=False)

Return a list of the incoming edges.

See also

edges
return a list of edges
in_edges_iter(nbunch=None, data=False)

Return an iterator over the incoming edges.

Parameters:

nbunch : iterable container, optional (default= all nodes)

A container of nodes. The container will be iterated through once.

data : bool, optional (default=False)

If True, return edge attribute dict in 3-tuple (u,v,data).

Returns:

in_edge_iter : iterator

An iterator of (u,v) or (u,v,d) tuples of incoming edges.

See also

edges_iter
return an iterator of edges
initialize_initial_state()[source]

This method will automatically re-adjust the cpds and the edges added to the bayesian network. If an edge that is added as an intra time slice edge in the 0th timeslice, this method will automatically add it in the 1st timeslice. It will also add the cpds. However, to call this method, one needs to add cpds as well as the edges in the bayesian network of the whole skeleton including the 0th and the 1st timeslice,.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> from pgmpy.factors.discrete import TabularCPD
>>> student = DBN()
>>> student.add_nodes_from(['D', 'G', 'I', 'S', 'L'])
>>> student.add_edges_from([(('D', 0),('G', 0)),(('I', 0),('G', 0)),(('D', 0),('D', 1)),(('I', 0),('I', 1))])
>>> grade_cpd = TabularCPD(('G', 0), 3, [[0.3, 0.05, 0.9, 0.5],
...                                      [0.4, 0.25, 0.8, 0.03],
...                                      [0.3, 0.7, 0.02, 0.2]],
...                        evidence=[('I', 0),('D', 0)],
...                        evidence_card=[2, 2])
>>> d_i_cpd = TabularCPD(('D', 1), 2, [[0.6, 0.3],
...                                    [0.4, 0.7]],
...                      evidence=[('D', 0)],
...                      evidence_card=2)
>>> diff_cpd = TabularCPD(('D', 0), 2, [[0.6, 0.4]])
>>> intel_cpd = TabularCPD(('I',0), 2, [[0.7, 0.3]])
>>> i_i_cpd = TabularCPD(('I', 1), 2, [[0.5, 0.4],
...                                    [0.5, 0.6]],
...                      evidence=[('I', 0)],
...                      evidence_card=2)
>>> student.add_cpds(grade_cpd, d_i_cpd, diff_cpd, intel_cpd, i_i_cpd)
>>> student.initialize_initial_state()
is_directed()

Return True if graph is directed, False otherwise.

is_multigraph()

Return True if graph is a multigraph, False otherwise.

moralize()[source]

Removes all the immoralities in the Network and creates a moral graph (UndirectedGraph).

A v-structure X->Z<-Y is an immorality if there is no directed edge between X and Y.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN([(('D',0), ('G',0)), (('I',0), ('G',0))])
>>> moral_graph = dbn.moralize()
>>> moral_graph.edges()
[(('G', 0), ('I', 0)),
(('G', 0), ('D', 0)),
(('D', 1), ('I', 1)),
(('D', 1), ('G', 1)),
(('I', 0), ('D', 0)),
(('G', 1), ('I', 1))]
name
nbunch_iter(nbunch=None)

Return an iterator of nodes contained in nbunch that are also in the graph.

The nodes in nbunch are checked for membership in the graph and if not are silently ignored.

Parameters:

nbunch : iterable container, optional (default=all nodes)

A container of nodes. The container will be iterated through once.

Returns:

niter : iterator

An iterator over nodes in nbunch that are also in the graph. If nbunch is None, iterate over all nodes in the graph.

Raises:

NetworkXError

If nbunch is not a node or or sequence of nodes. If a node in nbunch is not hashable.

See also

Graph.__iter__

Notes

When nbunch is an iterator, the returned iterator yields values directly from nbunch, becoming exhausted when nbunch is exhausted.

To test whether nbunch is a single node, one can use “if nbunch in self:”, even after processing with this routine.

If nbunch is not a node or a (possibly empty) sequence/iterator or None, a NetworkXError is raised. Also, if any object in nbunch is not hashable, a NetworkXError is raised.

neighbors(n)

Return a list of successor nodes of n.

neighbors() and successors() are the same function.

neighbors_iter(n)

Return an iterator over successor nodes of n.

neighbors_iter() and successors_iter() are the same.

node_dict_factory

alias of dict

nodes()[source]

Returns the list of nodes present in the network

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> dbn = DBN()
>>> dbn.add_nodes_from(['A', 'B', 'C'])
>>> dbn.nodes()
['B', 'A', 'C']
nodes_iter(data=False)

Return an iterator over the nodes.

Parameters:

data : boolean, optional (default=False)

If False the iterator returns nodes. If True return a two-tuple of node and node data dictionary

Returns:

niter : iterator

An iterator over nodes. If data=True the iterator gives two-tuples containing (node, node data, dictionary)

Notes

If the node data is not required it is simpler and equivalent to use the expression ‘for n in G’.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> [d for n,d in G.nodes_iter(data=True)]
[{}, {}, {}]
nodes_with_selfloops()

Return a list of nodes with self loops.

A node with a self loop has an edge with both ends adjacent to that node.

Returns:

nodelist : list

A list of nodes with self loops.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.nodes_with_selfloops()
[1]
number_of_edges(u=None, v=None)

Return the number of edges between two nodes.

Parameters:

u, v : nodes, optional (default=all edges)

If u and v are specified, return the number of edges between u and v. Otherwise return the total number of all edges.

Returns:

nedges : int

The number of edges in the graph. If nodes u and v are specified return the number of edges between those nodes.

See also

size

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.number_of_edges()
3
>>> G.number_of_edges(0,1)
1
>>> e = (0,1)
>>> G.number_of_edges(*e)
1
number_of_nodes()

Return the number of nodes in the graph.

Returns:

nnodes : int

The number of nodes in the graph.

See also

order, __len__

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> len(G)
3
number_of_selfloops()

Return the number of selfloop edges.

A selfloop edge has the same node at both ends.

Returns:

nloops : int

The number of selfloops.

Examples

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.number_of_selfloops()
1
order()

Return the number of nodes in the graph.

Returns:

nnodes : int

The number of nodes in the graph.

See also

number_of_nodes, __len__

out_degree(nbunch=None, weight=None)

Return the out-degree of a node or nodes.

The node out-degree is the number of edges pointing out of the node.

Parameters:

nbunch : iterable container, optional (default=all nodes)

A container of nodes. The container will be iterated through once.

weight : string or None, optional (default=None)

The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

Returns:

nd : dictionary, or number

A dictionary with nodes as keys and out-degree as values or a number if a single node is specified.

Examples

>>> G = nx.DiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.out_degree(0)
1
>>> G.out_degree([0,1])
{0: 1, 1: 1}
>>> list(G.out_degree([0,1]).values())
[1, 1]
out_degree_iter(nbunch=None, weight=None)

Return an iterator for (node, out-degree).

The node out-degree is the number of edges pointing out of the node.

Parameters:

nbunch : iterable container, optional (default=all nodes)

A container of nodes. The container will be iterated through once.

weight : string or None, optional (default=None)

The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

Returns:

nd_iter : an iterator

The iterator returns two-tuples of (node, out-degree).

Examples

>>> G = nx.DiGraph()
>>> G.add_path([0,1,2,3])
>>> list(G.out_degree_iter(0)) # node 0 with degree 1
[(0, 1)]
>>> list(G.out_degree_iter([0,1]))
[(0, 1), (1, 1)]
out_edges(nbunch=None, data=False, default=None)

Return a list of edges.

Edges are returned as tuples with optional data in the order (node, neighbor, data).

Parameters:

nbunch : iterable container, optional (default= all nodes)

A container of nodes. The container will be iterated through once.

data : string or bool, optional (default=False)

The edge attribute returned in 3-tuple (u,v,ddict[data]). If True, return edge attribute dict in 3-tuple (u,v,ddict). If False, return 2-tuple (u,v).

default : value, optional (default=None)

Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.

Returns:

edge_list: list of edge tuples

Edges that are adjacent to any node in nbunch, or a list of all edges if nbunch is not specified.

See also

edges_iter
return an iterator over the edges

Notes

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> G.edges()
[(0, 1), (1, 2), (2, 3)]
>>> G.edges(data=True) # default edge data is {} (empty dictionary)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> G.edges([0,3])
[(0, 1), (3, 2)]
>>> G.edges(0)
[(0, 1)]
out_edges_iter(nbunch=None, data=False, default=None)

Return an iterator over the edges.

Edges are returned as tuples with optional data in the order (node, neighbor, data).

Parameters:

nbunch : iterable container, optional (default= all nodes)

A container of nodes. The container will be iterated through once.

data : string or bool, optional (default=False)

The edge attribute returned in 3-tuple (u,v,ddict[data]). If True, return edge attribute dict in 3-tuple (u,v,ddict). If False, return 2-tuple (u,v).

default : value, optional (default=None)

Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.

Returns:

edge_iter : iterator

An iterator of (u,v) or (u,v,d) tuples of edges.

See also

edges
return a list of edges

Notes

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

Examples

>>> G = nx.DiGraph()   # or MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> [e for e in G.edges_iter()]
[(0, 1), (1, 2), (2, 3)]
>>> list(G.edges_iter(data=True)) # default data is {} (empty dict)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1)) 
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> list(G.edges_iter([0,2]))
[(0, 1), (2, 3)]
>>> list(G.edges_iter(0))
[(0, 1)]
predecessors(n)

Return a list of predecessor nodes of n.

predecessors_iter(n)

Return an iterator over predecessor nodes of n.

remove_cpds(*cpds)[source]

Removes the cpds that are provided in the argument.

Parameters:

*cpds : list, set, tuple (array-like)

List of CPDs which are to be associated with the model. Each CPD should be an instance of TabularCPD.

Examples

>>> from pgmpy.models import DynamicBayesianNetwork as DBN
>>> from pgmpy.factors.discrete import TabularCPD
>>> dbn = DBN()
>>> dbn.add_edges_from([(('D',0),('G',0)),(('I',0),('G',0)),(('D',0),('D',1)),(('I',0),('I',1))])
>>> grade_cpd =  TabularCPD(('G',0), 3, [[0.3,0.05,0.9,0.5],
...                                      [0.4,0.25,0.8,0.03],
...                                      [0.3,0.7,0.02,0.2]], [('I', 0),('D', 0)],[2,2])
>>> dbn.add_cpds(grade_cpd)
>>> dbn.get_cpds()
[<TabularCPD representing P(('G', 0):3 | ('I', 0):2, ('D', 0):2) at 0x3348ab0>]
>>> dbn.remove_cpds(grade_cpd)
>>> dbn.get_cpds()
[]
remove_edge(u, v)

Remove the edge between u and v.

Parameters:

u, v : nodes

Remove the edge between nodes u and v.

Raises:

NetworkXError

If there is not an edge between u and v.

See also

remove_edges_from
remove a collection of edges

Examples

>>> G = nx.Graph()   # or DiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.remove_edge(0,1)
>>> e = (1,2)
>>> G.remove_edge(*e) # unpacks e from an edge tuple
>>> e = (2,3,{'weight':7}) # an edge with attribute data
>>> G.remove_edge(*e[:2]) # select first part of edge tuple
remove_edges_from(ebunch)

Remove all edges specified in ebunch.

Parameters:

ebunch: list or container of edge tuples

Each edge given in the list or container will be removed from the graph. The edges can be:

  • 2-tuples (u,v) edge between u and v.
  • 3-tuples (u,v,k) where k is ignored.

See also

remove_edge
remove a single edge

Notes

Will fail silently if an edge in ebunch is not in the graph.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> ebunch=[(1,2),(2,3)]
>>> G.remove_edges_from(ebunch)
remove_node(n)

Remove node n.

Removes the node n and all adjacent edges. Attempting to remove a non-existent node will raise an exception.

Parameters:

n : node

A node in the graph

Raises:

NetworkXError

If n is not in the graph.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.edges()
[(0, 1), (1, 2)]
>>> G.remove_node(1)
>>> G.edges()
[]
remove_nodes_from(nbunch)

Remove multiple nodes.

Parameters:

nodes : iterable container

A container of nodes (list, dict, set, etc.). If a node in the container is not in the graph it is silently ignored.

See also

remove_node

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> e = G.nodes()
>>> e
[0, 1, 2]
>>> G.remove_nodes_from(e)
>>> G.nodes()
[]
reverse(copy=True)

Return the reverse of the graph.

The reverse is a graph with the same nodes and edges but with the directions of the edges reversed.

Parameters:

copy : bool optional (default=True)

If True, return a new DiGraph holding the reversed edges. If False, reverse the reverse graph is created using the original graph (this changes the original graph).

selfloop_edges(data=False, default=None)

Return a list of selfloop edges.

A selfloop edge has the same node at both ends.

Parameters:

data : string or bool, optional (default=False)

Return selfloop edges as two tuples (u,v) (data=False) or three-tuples (u,v,datadict) (data=True) or three-tuples (u,v,datavalue) (data=’attrname’)

default : value, optional (default=None)

Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.

Returns:

edgelist : list of edge tuples

A list of all selfloop edges.

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.selfloop_edges()
[(1, 1)]
>>> G.selfloop_edges(data=True)
[(1, 1, {})]
size(weight=None)

Return the number of edges.

Parameters:

weight : string or None, optional (default=None)

The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1.

Returns:

nedges : int

The number of edges or sum of edge weights in the graph.

See also

number_of_edges

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.size()
3
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge('a','b',weight=2)
>>> G.add_edge('b','c',weight=4)
>>> G.size()
2
>>> G.size(weight='weight')
6.0
subgraph(nbunch)

Return the subgraph induced on nodes in nbunch.

The induced subgraph of the graph contains the nodes in nbunch and the edges between those nodes.

Parameters:

nbunch : list, iterable

A container of nodes which will be iterated through once.

Returns:

G : Graph

A subgraph of the graph with the same edge attributes.

Notes

The graph, edge or node attributes just point to the original graph. So changes to the node or edge structure will not be reflected in the original graph while changes to the attributes will.

To create a subgraph with its own copy of the edge/node attributes use: nx.Graph(G.subgraph(nbunch))

If edge attributes are containers, a deep copy can be obtained using: G.subgraph(nbunch).copy()

For an inplace reduction of a graph to a subgraph you can remove nodes: G.remove_nodes_from([ n in G if n not in set(nbunch)])

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> H = G.subgraph([0,1,2])
>>> H.edges()
[(0, 1), (1, 2)]
successors(n)

Return a list of successor nodes of n.

neighbors() and successors() are the same function.

successors_iter(n)

Return an iterator over successor nodes of n.

neighbors_iter() and successors_iter() are the same.

to_directed()

Return a directed copy of the graph.

Returns:

G : DiGraph

A deepcopy of the graph.

Notes

This returns a “deepcopy” of the edge, node, and graph attributes which attempts to completely copy all of the data and references.

This is in contrast to the similar D=DiGraph(G) which returns a shallow copy of the data.

See the Python copy module for more information on shallow and deep copies, http://docs.python.org/library/copy.html.

Examples

>>> G = nx.Graph()   # or MultiGraph, etc
>>> G.add_path([0,1])
>>> H = G.to_directed()
>>> H.edges()
[(0, 1), (1, 0)]

If already directed, return a (deep) copy

>>> G = nx.DiGraph()   # or MultiDiGraph, etc
>>> G.add_path([0,1])
>>> H = G.to_directed()
>>> H.edges()
[(0, 1)]
to_undirected(reciprocal=False)

Return an undirected representation of the digraph.

Parameters:

reciprocal : bool (optional)

If True only keep edges that appear in both directions in the original digraph.

Returns:

G : Graph

An undirected graph with the same name and nodes and with edge (u,v,data) if either (u,v,data) or (v,u,data) is in the digraph. If both edges exist in digraph and their edge data is different, only one edge is created with an arbitrary choice of which edge data to use. You must check and correct for this manually if desired.

Notes

If edges in both directions (u,v) and (v,u) exist in the graph, attributes for the new undirected edge will be a combination of the attributes of the directed edges. The edge data is updated in the (arbitrary) order that the edges are encountered. For more customized control of the edge attributes use add_edge().

This returns a “deepcopy” of the edge, node, and graph attributes which attempts to completely copy all of the data and references.

This is in contrast to the similar G=DiGraph(D) which returns a shallow copy of the data.

See the Python copy module for more information on shallow and deep copies, http://docs.python.org/library/copy.html.

Warning: If you have subclassed DiGraph to use dict-like objects in the data structure, those changes do not transfer to the Graph created by this method.