Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN PYTHON, complete the following %matplotlib inline import matplotlib.pyplot as plt import networkx as nx def build_graph(tree, G, parent=None, value=None): ''' complete this part tree
IN PYTHON, complete the following
%matplotlib inline import matplotlib.pyplot as plt import networkx as nx def build_graph(tree, G, parent=None, value=None): ''' complete this part tree is the decision tree format used by Joel Grus in Data Science from Scratch: leaf node: True/False decision node: tuple of (attribute, subtree_dict) ''' if tree in [True, False]: G.add_node(tree) # add leaf node G.add_edge(parent, tree, label=value) else: attribute, subtree_dict = tree attribute = str(attribute) G.add_node(attribute) # add attribute node if parent is not None: G.add_edge(parent, attribute, label=value) for attribute_value, subtree in subtree_dict.items(): attribute_value = str(attribute_value) build_graph(subtree, G, attribute, attribute_value) # tree built previously for candidate hire classification problem tree = ('level', \ {None: True, \ 'Senior': ('tweets', \ {'no': False, None: False, 'yes': True}), \ 'Junior': ('phd', \ {'no': True, None: True, 'yes': False}), \ 'Mid': True}) G = nx.DiGraph() build_graph(tree, G) pos = nx.spring_layout(G) # draw nodes and edges nx.draw_networkx_nodes(G, pos) nx.draw_networkx_edges(G, pos) # add node and edge labels labels = {node:str(node) for i, node in enumerate(G.nodes())} elabels = {edge:G[edge[0]][edge[1]]["label"] for i, edge in enumerate(G.edges())} nx.draw_networkx_labels(G, pos, labels) nx.draw_networkx_edge_labels(G, pos, edge_labels=elabels) plt.axis('off')
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started