5
5
from logic import parse_definite_clause , standardize_variables , unify , subst
6
6
from learning import DataSet
7
7
from IPython .display import HTML , display
8
- from collections import Counter
8
+ from collections import Counter , defaultdict
9
9
10
10
import matplotlib .pyplot as plt
11
11
import numpy as np
12
12
13
13
import os , struct
14
14
import array
15
+ import time
15
16
16
17
17
18
#______________________________________________________________________________
19
+ # Magic Words
18
20
19
21
20
22
def pseudocode (algorithm ):
21
23
"""Print the pseudocode for the given algorithm."""
22
24
from urllib .request import urlopen
23
25
from IPython .display import Markdown
24
26
27
+ algorithm = algorithm .replace (' ' , '-' )
25
28
url = "https://raw.githubusercontent.com/aimacode/aima-pseudocode/master/md/{}.md" .format (algorithm )
26
29
f = urlopen (url )
27
30
md = f .read ().decode ('utf-8' )
@@ -43,25 +46,8 @@ def psource(*functions):
43
46
except ImportError :
44
47
print (source_code )
45
48
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
-
64
49
# ______________________________________________________________________________
50
+ # Iris Visualization
65
51
66
52
67
53
def show_iris (i = 0 , j = 1 , k = 2 ):
@@ -106,6 +92,7 @@ def show_iris(i=0, j=1, k=2):
106
92
plt .show ()
107
93
108
94
# ______________________________________________________________________________
95
+ # MNIST
109
96
110
97
111
98
def load_MNIST (path = "aima-data/MNIST" ):
@@ -193,6 +180,52 @@ def show_ave_MNIST(labels, images):
193
180
plt .show ()
194
181
195
182
# ______________________________________________________________________________
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
+ # ______________________________________________________________________________
196
229
197
230
198
231
_canvas = """
0 commit comments