Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fe21fb

Browse files
authored
Update notebook.py
- remove duplicate psource - add section headers - add mdp visualization code
1 parent bf2a627 commit 0fe21fb

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

notebook.py

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55
from logic import parse_definite_clause, standardize_variables, unify, subst
66
from learning import DataSet
77
from IPython.display import HTML, display
8-
from collections import Counter
8+
from collections import Counter, defaultdict
99

1010
import matplotlib.pyplot as plt
1111
import numpy as np
1212

1313
import os, struct
1414
import array
15+
import time
1516

1617

1718
#______________________________________________________________________________
19+
# Magic Words
1820

1921

2022
def pseudocode(algorithm):
2123
"""Print the pseudocode for the given algorithm."""
2224
from urllib.request import urlopen
2325
from IPython.display import Markdown
2426

27+
algorithm = algorithm.replace(' ', '-')
2528
url = "https://raw.githubusercontent.com/aimacode/aima-pseudocode/master/md/{}.md".format(algorithm)
2629
f = urlopen(url)
2730
md = f.read().decode('utf-8')
@@ -43,25 +46,8 @@ def psource(*functions):
4346
except ImportError:
4447
print(source_code)
4548

46-
47-
# ______________________________________________________________________________
48-
49-
50-
def psource(*functions):
51-
"Print the source code for the given function(s)."
52-
source_code = '\n\n'.join(getsource(fn) for fn in functions)
53-
try:
54-
from pygments.formatters import HtmlFormatter
55-
from pygments.lexers import PythonLexer
56-
from pygments import highlight
57-
58-
display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True))))
59-
60-
except ImportError:
61-
print(source_code)
62-
63-
6449
# ______________________________________________________________________________
50+
# Iris Visualization
6551

6652

6753
def show_iris(i=0, j=1, k=2):
@@ -106,6 +92,7 @@ def show_iris(i=0, j=1, k=2):
10692
plt.show()
10793

10894
# ______________________________________________________________________________
95+
# MNIST
10996

11097

11198
def load_MNIST(path="aima-data/MNIST"):
@@ -193,6 +180,52 @@ def show_ave_MNIST(labels, images):
193180
plt.show()
194181

195182
# ______________________________________________________________________________
183+
# MDP
184+
185+
186+
def make_plot_grid_step_function(columns, rows, U_over_time):
187+
"""ipywidgets interactive function supports single parameter as input.
188+
This function creates and return such a function by taking as input
189+
other parameters."""
190+
191+
def plot_grid_step(iteration):
192+
data = U_over_time[iteration]
193+
data = defaultdict(lambda: 0, data)
194+
grid = []
195+
for row in range(rows):
196+
current_row = []
197+
for column in range(columns):
198+
current_row.append(data[(column, row)])
199+
grid.append(current_row)
200+
grid.reverse() # output like book
201+
fig = plt.imshow(grid, cmap=plt.cm.bwr, interpolation='nearest')
202+
203+
plt.axis('off')
204+
fig.axes.get_xaxis().set_visible(False)
205+
fig.axes.get_yaxis().set_visible(False)
206+
207+
for col in range(len(grid)):
208+
for row in range(len(grid[0])):
209+
magic = grid[col][row]
210+
fig.axes.text(row, col, "{0:.2f}".format(magic), va='center', ha='center')
211+
212+
plt.show()
213+
214+
return plot_grid_step
215+
216+
def make_visualize(slider):
217+
"""Takes an input a sliderand returns callback function
218+
for timer and animation."""
219+
220+
def visualize_callback(Visualize, time_step):
221+
if Visualize is True:
222+
for i in range(slider.min, slider.max + 1):
223+
slider.value = i
224+
time.sleep(float(time_step))
225+
226+
return visualize_callback
227+
228+
# ______________________________________________________________________________
196229

197230

198231
_canvas = """

0 commit comments

Comments
 (0)