ML Project using Bayesian networks
# -*- coding: utf-8 -*- """directed_graph.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1i5mT15-UVwTbi5J21cbh6WmeWahDZDTb """ import networkx as nx G = nx.DiGraph() # create a directed graph G.add_node("ab") # label nodes print(G.nodes) G.add_edges_from([("ab", "nc"), ("fb", "nc")]) # add edges print(G.nodes) print(G.edges) G.add_node("ab", ab_y=0.1, ab_n=0.9) # add node attributes - useful for attaching probabiity tables to nodes G.add_node("fb", fb_y=0.2, fb_n=0.8) # let's go down one level and attach a table to the "no charging node" G.add_node("nc", ab_y_fb_y_nc=y= 0.75, ab_y_fb_n_nc=y= 0.4, ab_n_fb_y_nc=y= 0.6, ab_n_fb_n_nc=y= 0.1,ab_y_fb_y_nc_n= 0.25, ab_y_fb_n_nc_n= 0.6, ab_n_fb_y_nc_n= 0.4, ab_n_fb_n_nc_n= 0.9) # probabiluity table for "no charging" node """Add nodes, edges and probability tables for the rest of the network. After your network is fully defined you can begin the inference process. One useful tool to use in inferencing is the depth first search procedure that will return all paths leading to a given node from all root nodes in the graph. This is implemented via the all_simple_paths method in Networkx """ roots = (v for v, d in G.in_degree() if d == 0) all_paths = [] for root in roots: paths = nx.all_simple_paths(G, root, "nc") all_paths.extend(paths) all_paths