# Draw Perceptron graph by graphviz

## Goal¶

This post aims to introduce how to draw a diagram for perceptron.

Reference

## Libraries¶

In [1]:
from graphviz import Digraph


## Create a node list and dictionary for the edges¶

In [12]:
# List of nodes
l_nodes = ['1', 'x0', 'x1', 'y']

# Dictionary mapping from label name to the edge between two nodes
d_edges = {'b': ('1', 'y'),
'w0': ('x0', 'y'),
'w1': ('x1', 'y')}


## Visualize a graph for perceptron¶

In [13]:
# Create Digraph object
dot = Digraph()
dot.attr(rankdir='LR')

for n in l_nodes:
dot.node(n)

for label, edges in d_edges.items():
dot.edge(edges[0], edges[1], label=label)

# Fill node 1 by gray
dot.node('1', style='filled')

# Visualize the graph
dot

Out[13]:

# Introduction to Graphviz in Jupyter Notebook

## Goal¶

The goal in this post is to introduce graphviz to draw the graph when we explain graph-related algorithm e.g., tree, binary search etc. It would be nicer to have such a visualization to quickly digest problems and solutions.

Since we work with TreeNode and trees in a list-expresion e.g., [1, 2, null, 3] in LeetCode, the goal of this post is to easily convert the given tree in a list-expression into the visualization like below.

In [1]:
from IPython.display import Image
Image('digraph.png')

Out[1]: