![]() |
Quantum Fog
0.9.3
|
Public Member Functions | |
def | __init__ (self, states_df, score_type, max_num_mtries, ess=1.0, verbose=False, vtx_to_states=None) |
def | climb (self) |
def | do_move (self, move, score_change, do_finish=True) |
def | refresh_nx_graph (self) |
def | would_create_cycle (self, move) |
def | move_approved (self, move) |
def | finish_do_move (self, move) |
def | restart (self, mtry_num) |
def | cache_this (self, move, score_change) |
def | empty_cache (self) |
![]() | |
def | __init__ (self, is_quantum, states_df, vtx_to_states=None) |
def | fill_bnet_with_parents (self, vtx_to_parents) |
Static Public Member Functions | |
def | do_move_vtx_to_parents (move, vtx_to_parents, reversal=False) |
def | do_move_nx_graph (move, nx_graph, reversal=False) |
def | HC_lner_test (LnerClass, verbose=False) |
![]() | |
def | learn_nd_state_names (bnet, states_df) |
def | import_nd_state_names (bnet, vtx_to_states) |
def | int_sts_detector (sub_states_df) |
Public Attributes | |
max_num_mtries | |
score_type | |
verbose | |
vertices | |
vtx_to_parents | |
scorer | |
nx_graph | |
![]() | |
is_quantum | |
bnet | |
states_df | |
ord_nodes | |
The HiilClimbingLner( Hill Climbing Learner ) class learns the structure of a bnet using a greedy strategy, meaning it goes for the highest short term gain without caring that that may not be in its long term interest, as it may lead it to a local rather than the global maximum. Each 'move' consists of either adding, deleting or reversing the direction of an arrow. Each move is given a score. Score keeping is done by an object of a separate class called NetStrucScorer. A 'try' or 'mtry' (move try) is a set of candidate moves. Only the highest scoring move of a try is actually performed. Classes that inherit from this one wil have the prefix HC_ for easy identification and so that they stay together in an alphabetical listing. References ---------- 1. Nicholas Cullen neuroBN at github Attributes ---------- is_quantum : bool True for quantum bnets and False for classical bnets bnet : BayesNet a BayesNet in which we store what is learned states_df : pandas.DataFrame a Pandas DataFrame with training data. column = node and row = sample. Each row/sample gives the state of the col/node ord_nodes : list[DirectedNode] a list of DirectedNode's named and in the same order as the column labels of self.states_df. max_num_mtries : int maximum number of move tries nx_graph : networkx.DiGraph a networkx directed graph used to store arrows score_type : str score type, either 'LL', 'BIC, 'AIC', 'BDEU' or 'K2' scorer : NetStrucScorer object of NetStrucScorer class that keeps a running record of scores verbose : bool True for this prints a running commentary to console vertices : list[str] list of vertices (node names). Same as states_df.columns vtx_to_parents : dict[str, list[str]] dictionary mapping each vertex to a list of its parents's names
def learning.HillClimbingLner.HillClimbingLner.__init__ | ( | self, | |
states_df, | |||
score_type, | |||
max_num_mtries, | |||
ess = 1.0 , |
|||
verbose = False , |
|||
vtx_to_states = None |
|||
) |
Constructor Parameters ---------- states_df : pandas.core.frame.DataFrame score_type : str max_num_mtries : int ess : float Equivalent Sample Size, a parameter in BDEU scorer. Fudge factor that is supposed to grow as the amount of prior knowledge grows. verbose : bool vtx_to_states : dict[str, list[str]] A dictionary mapping each node name to a list of its state names. This information will be stored in self.bnet. If vtx_to_states=None, constructor will learn vtx_to_states from states_df Returns ------- None
def learning.HillClimbingLner.HillClimbingLner.cache_this | ( | self, | |
move, | |||
score_change | |||
) |
This is a hook function that allows subclasses of this class to store in a list every move and its score change that was considered (whether it was the best move of the try or not) for the current try. Parameters ---------- move : tuple[str, str, str] score_change : tuple[float, float, float] Returns ------- None
def learning.HillClimbingLner.HillClimbingLner.climb | ( | self | ) |
This is the main engine of the whole class. Most of the other functions of this class are called inside this function. Returns ------- None
def learning.HillClimbingLner.HillClimbingLner.do_move | ( | self, | |
move, | |||
score_change, | |||
do_finish = True |
|||
) |
Once the move has been approved/vetted, it is performed by this method. Parameters ---------- move : tuple[str, str, str] a move is a 3-tuple (beg_vtx, end_vtx, action) describing an arrow beg_vtx->end_vtx that will be either added, deleted or reversed, depending on whether action = 'add', 'del', or 'rev'. score_change : tuple[float, float, float] score_change is a 3-tuple (beg_vtx_score_ch, end_vtx_score_ch, tot_score_ch). beg_score_ch is the score change of the beg_vtx of move, end_score_ch is the score change of the end_vtx of move. This is possible because all score functions can be evaluated for a single vtx. tot_score_ch is the sum of beg_score_ch and end_score_ch minus a positive penalty that increases with the size of the network. do_finish : bool True if you want to finish off this function with a call to finish_do_move(). Returns ------- None
|
static |
Applies move or its reversal to a networkx dag. Parameters ---------- move : tuple[str, str, str] nx_graph : networkx.DiGraph reversal : bool If True, the function applies reversal of move to nx_graph Returns ------- None
|
static |
Applies a move or its reversal to a vtx_to_parents Parameters ---------- move : tuple[str, str, str] vtx_to_parents : dict[str, list[str]] dictionary mapping each vertex to a list of its parents's names reversal : bool If True, the function applies reversal of move to vtx_to_parents Returns ------- None
def learning.HillClimbingLner.HillClimbingLner.empty_cache | ( | self | ) |
This is a hook function that allows subclasses of this class to clear its cached list of moves before starting the next try. Returns ------- None
def learning.HillClimbingLner.HillClimbingLner.finish_do_move | ( | self, | |
move | |||
) |
This is a hook function that allows subclasses of this class to do some additional processing before the do_move() function is concluded. Parameters ---------- move : tuple[str, str, str] Returns ------- None
|
static |
This static method gives a simple example that we use to test HillClimbingLner and its subclasses (those starting with HC_). The method takes in as input training data generated from 2 graphs (the classical versions of wetgrass and earthquake), and it outputs a drawing of the learned structure for 2 scoring functions ( BIC-frequentist and BDEU-bayesian) Parameters ---------- LnerClass : HillClimbingLner or subclass This is either HillClimbingLner without quotes or the name of a subclass of that class. verbose : bool Returns -------
def learning.HillClimbingLner.HillClimbingLner.move_approved | ( | self, | |
move | |||
) |
This is a hook function that allows subclasses of this class to impose more stringent requirements on a move before it is approved, beyond the usual requirement that the move not create a cycle. The function returns a bool, its decision. Parameters ---------- move : tuple[str, str, str] Returns ------- bool
def learning.HillClimbingLner.HillClimbingLner.refresh_nx_graph | ( | self | ) |
This function clears self.nx_graph and refills it with info in self.vtx_to_parents. Returns ------- None
def learning.HillClimbingLner.HillClimbingLner.restart | ( | self, | |
mtry_num | |||
) |
This is a hook function that allows subclasses of this class to restart the trying process when the last try yielded no moves with positive score change. mtry_num is passed in. (restart_approved, mtry_num) are returned, where restart_approved is a boolean signaling approval and mtry_num is an int, usually either the inputted mtry_num or zero. Parameters ---------- mtry_num : int move try num is the number of the current try Returns ------- bool, int
def learning.HillClimbingLner.HillClimbingLner.would_create_cycle | ( | self, | |
move | |||
) |
This function performs the move 'move' on self.nx_graph and then tests to see if the new graph has cycles. It communicates the result of the tests in the bool output. It restores nx_graph to its original state after the testing for cycles is concluded. Parameters ---------- move : tuple[str, str, str] Returns ------- bool