MCPcopy Create free account
hub / github.com/CyberPoint/libpgm / PGMLearner

Class PGMLearner

libpgm/pgmlearner.py:48–973  ·  view source on GitHub ↗

This class is a machine with tools for learning Bayesian networks from data. It contains the *discrete_mle_estimateparams*, *lg_mle_estimateparams*, *discrete_constraint_estimatestruct*, *lg_constraint_estimatestruct*, *discrete_condind*, *discrete_estimatebn*, and *lg_estimatebn* methods.

Source from the content-addressed store, hash-verified

46from sampleaggregator import SampleAggregator
47
48class PGMLearner():
49 '''
50 This class is a machine with tools for learning Bayesian networks from data. It contains the *discrete_mle_estimateparams*, *lg_mle_estimateparams*, *discrete_constraint_estimatestruct*, *lg_constraint_estimatestruct*, *discrete_condind*, *discrete_estimatebn*, and *lg_estimatebn* methods.
51
52 '''
53 def discrete_mle_estimateparams(self, graphskeleton, data):
54 '''
55 Estimate parameters for a discrete Bayesian network with a structure given by *graphskeleton* in order to maximize the probability of data given by *data*. This function takes the following arguments:
56
57 1. *graphskeleton* -- An instance of the :doc:`GraphSkeleton <graphskeleton>` class containing vertex and edge data.
58 2. *data* -- A list of dicts containing samples from the network in {vertex: value} format. Example::
59
60 [
61 {
62 'Grade': 'B',
63 'SAT': 'lowscore',
64 ...
65 },
66 ...
67 ]
68
69 This function normalizes the distribution of a node's outcomes for each combination of its parents' outcomes. In doing so it creates an estimated tabular conditional probability distribution for each node. It then instantiates a :doc:`DiscreteBayesianNetwork <discretebayesiannetwork>` instance based on the *graphskeleton*, and modifies that instance&#x27;s *Vdata* attribute to reflect the estimated CPDs. It then returns the instance.
70
71 The Vdata attribute instantiated is in the format seen in :doc:`unittestdict`, as described in :doc:`discretebayesiannetwork`.
72
73 Usage example: this would learn parameters from a set of 200 discrete samples::
74
75 import json
76
77 from libpgm.nodedata import NodeData
78 from libpgm.graphskeleton import GraphSkeleton
79 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
80 from libpgm.pgmlearner import PGMLearner
81
82 # generate some data to use
83 nd = NodeData()
84 nd.load("../tests/unittestdict.txt") # an input file
85 skel = GraphSkeleton()
86 skel.load("../tests/unittestdict.txt")
87 skel.toporder()
88 bn = DiscreteBayesianNetwork(skel, nd)
89 data = bn.randomsample(200)
90
91 # instantiate my learner
92 learner = PGMLearner()
93
94 # estimate parameters from data and skeleton
95 result = learner.discrete_mle_estimateparams(skel, data)
96
97 # output
98 print json.dumps(result.Vdata, indent=2)
99
100 ''&#x27;
101 assert (isinstance(graphskeleton, GraphSkeleton)), "First arg must be a loaded GraphSkeleton class."
102 assert (isinstance(data, list) and data and isinstance(data[0], dict)), "Second arg must be a list of dicts."
103
104 # instantiate Bayesian network, and add parent and children data
105 bn = DiscreteBayesianNetwork()

Callers 3

timerFunction · 0.90
setUpMethod · 0.90
examples.pyFile · 0.90

Calls

no outgoing calls

Tested by 1

setUpMethod · 0.72