Factor Graphs¶
API Reference¶
- class pomegranate.FactorGraph.FactorGraph¶
A Factor Graph model.
A bipartite graph where conditional probability tables are on one side, and marginals for each of the variables involved are on the other side.
- Parameters
- namestr, optional
The name of the model. Default is None.
- bake()¶
Finalize the topology of the model.
Assign a numerical index to every state and create the underlying arrays corresponding to the states and edges between the states. This method must be called before any of the probability-calculating methods. This is the same as the HMM bake, except that at the end it sets current state information.
- Parameters
- None
- Returns
- None
- from_dict()¶
Deserialize this object from a dictionary of parameters.
- marginal()¶
Return the marginal probabilities of each variable in the graph.
This is equivalent to a pass of belief propagation on a graph where no data has been given. This will calculate the probability of each variable being in each possible emission when nothing is known.
- Parameters
- None
- Returns
- marginalsarray-like, shape (n_nodes)
An array of univariate distribution objects showing the marginal probabilities of that variable.
- plot()¶
Draw this model’s graph using NetworkX and matplotlib.
Note that this relies on networkx’s built-in graphing capabilities (and not Graphviz) and thus can’t draw self-loops.
See networkx.draw_networkx() for the keywords you can pass in.
- Parameters
- **kwargsany
The arguments to pass into networkx.draw_networkx()
- Returns
- None
- predict_proba()¶
Returns the probabilities of each variable in the graph given evidence.
This calculates the marginal probability distributions for each state given the evidence provided through loopy belief propagation. Loopy belief propagation is an approximate algorithm which is exact for certain graph structures.
- Parameters
- datadict or array-like, shape <= n_nodes, optional
The evidence supplied to the graph. This can either be a dictionary with keys being state names and values being the observed values (either the emissions or a distribution over the emissions) or an array with the values being ordered according to the nodes incorporation in the graph (the order fed into .add_states/add_nodes) and None for variables which are unknown. If nothing is fed in then calculate the marginal of the graph.
- max_iterationsint, optional
The number of iterations with which to do loopy belief propagation. Usually requires only 1.
- check_inputbool, optional
Check to make sure that the observed symbol is a valid symbol for that distribution to produce.
- Returns
- probabilitiesarray-like, shape (n_nodes)
An array of univariate distribution objects showing the probabilities of each variable.
- to_dict()¶
Serialize this object to a dictionary of parameters.