bnsl package
bnsl consists of several core classes, that is:
bnsl
-DAG
-Score
-Estimator
DAG class
- class bnsl.graph.DAG(incoming_graph_data=None, **kwargs)
Bases:
networkx.classes.digraph.DiGraph
The graph class used in this project, inherited class nx.DiGraph. When it is initialized, it has to be acyclic.
- property adj_df
The adjacent matrix in the form of pd.Dataframe of DAG. :param **kwargs:
- Returns
pd.Dataframe
- property adj_np
The adjacent matrix in the form of np.array of DAG.
- Returns
np.array
- do_operation(operation)
- property genome
The genome of DAG
- Returns
np.array
- legal_operations()
Iterator, yield all legal operations like (‘+’, (u, v)), (‘-’, (u, v)), (‘flip’, (u, v)).
- Yields
operation (‘+’, (u, v))
- read(path: str, mode='edge_list', source='source node', target='target node')
Read DAG from csv file.There are two modes: edge_list, adjacent_matrix.
In the edge_list mode, the csv file looks like:
source node,target node asia,tub tub,either either,dysp either,xray
In the adjacent_matrix mode, the csv file looks like:
,asia,tub,either,smoke,lung,bronc,dysp,xray asia,0,1,0,0,0,0,0,0 tub,0,0,1,0,0,0,0,0 either,0,0,0,0,0,0,1,1 smoke,0,0,0,0,1,1,0,0 lung,0,0,1,0,0,0,0,0 bronc,0,0,0,0,0,0,1,0 dysp,0,0,0,0,0,0,0,0 xray,0,0,0,0,0,0,0,0
- Parameters
path – file path.
mode – edge_list mode or adjacent_matrix mode.
source – the source node column.
target – the target node column.
- save(path: str, mode='edge_list')
Save the DAG into csv file.
- Parameters
path – file path.
mode – edge_list mode or adjacent_matrix mode.
- score(score_method, detail=False)
Calculate the score of the DAG.
- Parameters
score_method – score criteria instance
detail – return a dictionary containing every local score. Default is False.
- Returns
score of the DAG (or score, dict)
- show()
Draw the figure of DAG
- summary()
Print a summary to describe current dag.
Score class
- class bnsl.score.MDL_score(data: pandas.core.frame.DataFrame)
Bases:
bnsl.base.Score
MDL score.
reference:
Lam, W., & Bacchus, F. (1994). Learning Bayesian belief networks: An approach based on the MDL principle. Computational intelligence, 10(3), 269-293. Yuan, C., Malone, B., & Wu, X. (2011, June). Learning optimal Bayesian networks using A* search. In Twenty-Second International Joint Conference on Artificial Intelligence.
- likelihood(Nijk: numpy.ndarray)
Calculate the likelihood.
Nijk is a table-like array. q1 q2 q3 r1 10 12 30 r2 50 60 17 (qi are the states of parents) (ri are the states of x)
- Parameters
Nijk – state table
- Returns
likelihood.
- local_score(x: str, parents: tuple)
Calculate local score.
References
Kitson, N.K., Constantinou, A.C., Guo, Z., Liu, Y. and Chobtham, K., 2021. A survey of Bayesian Network structure learning. arXiv preprint arXiv:2109.11415.
- Parameters
x – str: Node.
parents – tuple: Parents of node.
- Returns
Local score
- Return type
float
- class bnsl.score.BIC_score(data: pandas.core.frame.DataFrame, **kwargs)
Bases:
bnsl.base.Score
BIC score class, BIC score is minus MDL score.
- local_score(x, parents)
Calculate local score of BIC score.
- Parameters
x – str: node
parents – tuple: parents of node.
- Returns
BIC score
- class bnsl.score.BDeu_score(data: pandas.core.frame.DataFrame, equivalent_sample_size=10, **kwargs)
Bases:
bnsl.score.MDL_score
BDeu score.
- local_score(x, parents)
Calculate local score.
- Parameters
x – node.
parents – parents of node.
- Returns
BDeu score.