Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# KNOWLEDGE\n",
"\n",
"The [knowledge](https://github.com/aimacode/aima-python/blob/master/knowledge.py) module covers **Chapter 19: Knowledge in Learning** from Stuart Russel's and Peter Norvig's book *Artificial Intelligence: A Modern Approach*.\n",
"\n",
"Execute the cell below to get started."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from knowledge import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CONTENTS\n",
"\n",
"* Overview\n",
"* Current-Best Learning"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## OVERVIEW\n",
"\n",
"Like the [learning module](https://github.com/aimacode/aima-python/blob/master/learning.ipynb), this chapter focuses on methods for generating a model/hypothesis for a domain. Unlike though the learning chapter, here we use prior knowledge to help us learn from new experiences and find a proper hypothesis.\n",
"\n",
"### First-Order Logic\n",
"\n",
"Usually knowledge in this field is represented as **first-order logic**, a type of logic that uses variables and quantifiers in logical sentences. Hypotheses are represented by logical sentences with variables, while examples are logical sentences with set values instead of variables. The goal is to assign a value to a special first-order logic predicate, called **goal predicate**, for new examples given a hypothesis. We learn this hypothesis by infering knowledge from some given examples.\n",
"\n",
"### Representation\n",
"\n",
"In this module, we use dictionaries to represent examples, with keys the attribute names and values the corresponding example values. Examples also have an extra boolean field, 'GOAL', for the goal predicate. A hypothesis is represented as a list of dictionaries. Each dictionary in that list represents a disjunction. Inside these dictionaries/disjunctions we have conjunctions.\n",
"\n",
"For example, say we want to predict if an animal (cat or dog) will take an umbrella given whether or not it rains or the animal wears a coat. The goal value is 'take an umbrella' and is denoted by the key 'GOAL'. An example:\n",
"\n",
"`{'Species': 'Cat', 'Coat': 'Yes', 'Rain': 'Yes', 'GOAL': True}`\n",
"\n",
"A hypothesis can be the following:\n",
"\n",
"`[{'Species': 'Cat'}]`\n",
"\n",
"which means an animal will take an umbrella if and only if it is a cat.\n",
"\n",
"### Consistency\n",
"\n",
"We say that an example `e` is **consistent** with an hypothesis `h` if the assignment from the hypothesis for `e` is the same as `e['GOAL']`. If the above example and hypothesis are `e` and `h` respectively, then `e` is consistent with `h` since `e['Species'] == 'Cat'`. For `e = {'Species': 'Dog', 'Coat': 'Yes', 'Rain': 'Yes', 'GOAL': True}`, the example is no longer consistent with `h`, since the value assigned to `e` is *False* while `e['GOAL']` is *True*."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## [CURRENT-BEST LEARNING](https://github.com/aimacode/aima-pseudocode/blob/master/md/Current-Best-Learning.md)\n",
"\n",
"### Overview\n",
"\n",
"In **Current-Best Learning**, we start with a hypothesis and we refine it as we iterate through the examples. For each example, there are three possible outcomes. The example is consistent with the hypothesis, the example is a **false positive** (real value is false but got predicted as true) and **false negative** (real value is true but got predicted as false). Depending on the outcome we refine the hypothesis accordingly:\n",
"\n",
"* Consistent: We do not change the hypothesis and we move on to the next example.\n",
"\n",
"* False Positive: We **specialize** the hypothesis, which means we add a conjunction.\n",
"\n",
"* False Negative: We **generalize** the hypothesis, either by removing a conjunction or a disjunction, or by adding a disjunction.\n",
"\n",
"When specializing and generalizing, we should take care to not create inconsistencies with previous examples. To avoid that caveat, backtracking is needed. Thankfully, there is not just one specialization or generalization, so we have a lot to choose from. We will go through all the specialization/generalizations and we will refine our hypothesis as the first specialization/generalization consistent with all the examples seen up to that point."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Implementation\n",
"\n",
"As mentioned previously, examples are dictionaries (with keys the attribute names) and hypotheses are lists of dictionaries (each dictionary is a disjunction). Also, in the hypothesis, we denote the *NOT* operation with an exclamation mark (!).\n",
"\n",
"We have functions to calculate the list of all specializations/generalizations, to check if an example is consistent/false positive/false negative with a hypothesis. We also have an auxiliary function to add a disjunction (or operation) to a hypothesis, and two other functions to check consistency of all (or just the negative) examples.\n",
"\n",
"You can read the source by running the cells below:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource current_best_learning"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource specializations"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource generalizations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can view the auxiliary functions in the [knowledge module](https://github.com/aimacode/aima-python/blob/master/knowledge.py). A few notes on the functionality of some of the important methods:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* `specializations`: For each disjunction in the hypothesis, it adds a conjunction for values in the examples encountered so far (if the conjunction is consistent with all the examples). It returns a list of hypotheses.\n",
"\n",
"* `generalizations`: It adds to the list of hypotheses in three phases. First it deletes disjunctions, then it deletes conjunctions and finally it adds a disjunction.\n",
"\n",
"* `add_or`: Used by `generalizations` to add an *or operation* (a disjunction) to the hypothesis. Since the last example is the problematic one which wasn't consistent with the hypothesis, it will model the new disjunction to that example. It creates a disjunction for each combination of attributes in the example and returns the new hypotheses consistent with the negative examples encountered so far. We do not need to check the consistency of positive examples, since they are already consistent with at least one other disjunction in the hypotheses' set, so this new disjunction doesn't affect them. In other words, if the value of a positive example is negative under the disjunction, it doesn't matter since we know there exists a disjunction consistent with the example."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the algorithm stops searching the specializations/generalizations after the first consistent hypothesis is found, usually you will get different results each time you run the code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Examples\n",
"\n",
"We will take a look at two examples. The first is a trivial one, while the second is a bit more complicated (you can also find it in the book).\n",
"\n",
"First we have the \"animals taking umbrellas\" example. Here we want to find a hypothesis to predict whether or not an animal will take an umbrella. The attributes are `Species`, `Rain` and `Coat`. The possible values are `[Cat, Dog]`, `[Yes, No]` and `[Yes, No]` respectively. Below we give seven examples (with `GOAL` we denote whether an animal will take an umbrella or not):"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"animals_umbrellas = [\n",
" {'Species': 'Cat', 'Rain': 'Yes', 'Coat': 'No', 'GOAL': True},\n",
" {'Species': 'Cat', 'Rain': 'Yes', 'Coat': 'Yes', 'GOAL': True},\n",
" {'Species': 'Dog', 'Rain': 'Yes', 'Coat': 'Yes', 'GOAL': True},\n",
" {'Species': 'Dog', 'Rain': 'Yes', 'Coat': 'No', 'GOAL': False},\n",
" {'Species': 'Dog', 'Rain': 'No', 'Coat': 'No', 'GOAL': False},\n",
" {'Species': 'Cat', 'Rain': 'No', 'Coat': 'No', 'GOAL': False},\n",
" {'Species': 'Cat', 'Rain': 'No', 'Coat': 'Yes', 'GOAL': True}\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let our initial hypothesis be `[{'Species': 'Cat'}]`. That means every cat will be taking an umbrella. We can see that this is not true, but it doesn't matter since we will refine the hypothesis using the Current-Best algorithm. First, let's see how that initial hypothesis fares to have a point of reference."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"False\n",
"False\n",
"False\n",
"True\n",
"True\n"
]
}
],
"source": [
"initial_h = [{'Species': 'Cat'}]\n",
"\n",
"for e in animals_umbrellas:\n",
" print(guess_value(e, initial_h))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We got 5/7 correct. Not terribly bad, but we can do better. Let's run the algorithm and see how that performs."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"False\n",
"False\n",
"False\n",
"True\n"
]
}
],
"source": [
"h = current_best_learning(animals_umbrellas, initial_h)\n",
"\n",
"for e in animals_umbrellas:\n",
" print(guess_value(e, h))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We got everything right! Let's print our hypothesis:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'Species': 'Cat', 'Rain': '!No'}, {'Coat': 'Yes', 'Species': 'Dog', 'Rain': 'Yes'}, {'Coat': 'Yes', 'Species': 'Cat'}]\n"
]
}
],
"source": [
"print(h)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If an example meets any of the disjunctions in the list, it will be `True`, otherwise it will be `False`.\n",
"\n",
"Let's move on to a bigger example, the \"Restaurant\" example from the book. The attributes for each example are the following:\n",
"\n",
"* Alternative option (`Alt`)\n",
"* Bar to hang out/wait (`Bar`)\n",
"* Day is Friday (`Fri`)\n",
"* Is hungry (`Hun`)\n",
"* How much does it cost (`Price`, takes values in [$, $$, $$$])\n",
"* How many patrons are there (`Pat`, takes values in [None, Some, Full])\n",
"* Is raining (`Rain`)\n",
"* Has made reservation (`Res`)\n",
"* Type of restaurant (`Type`, takes values in [French, Thai, Burger, Italian])\n",
"* Estimated waiting time (`Est`, takes values in [0-10, 10-30, 30-60, >60])\n",
"\n",
"We want to predict if someone will wait or not (Goal = WillWait). Below we show twelve examples found in the book."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"In code:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"restaurant = [\n",
" {'Alt': 'Yes', 'Bar': 'No', 'Fri': 'No', 'Hun': 'Yes', 'Pat': 'Some',\n",
" 'Price': '$$$', 'Rain': 'No', 'Res': 'Yes', 'Type': 'French', 'Est': '0-10',\n",
" 'GOAL': True},\n",
"\n",
" {'Alt': 'Yes', 'Bar': 'No', 'Fri': 'No', 'Hun': 'Yes', 'Pat': 'Full',\n",
" 'Price': '$', 'Rain': 'No', 'Res': 'No', 'Type': 'Thai', 'Est': '30-60',\n",
" 'GOAL': False},\n",
"\n",
" {'Alt': 'No', 'Bar': 'Yes', 'Fri': 'No', 'Hun': 'No', 'Pat': 'Some',\n",
" 'Price': '$', 'Rain': 'No', 'Res': 'No', 'Type': 'Burger', 'Est': '0-10',\n",
" 'GOAL': True},\n",
"\n",
" {'Alt': 'Yes', 'Bar': 'No', 'Fri': 'Yes', 'Hun': 'Yes', 'Pat': 'Full',\n",
" 'Price': '$', 'Rain': 'Yes', 'Res': 'No', 'Type': 'Thai', 'Est': '10-30',\n",
" 'GOAL': True},\n",
"\n",
" {'Alt': 'Yes', 'Bar': 'No', 'Fri': 'Yes', 'Hun': 'No', 'Pat': 'Full',\n",
" 'Price': '$$$', 'Rain': 'No', 'Res': 'Yes', 'Type': 'French', 'Est': '>60',\n",
" 'GOAL': False},\n",
"\n",
" {'Alt': 'No', 'Bar': 'Yes', 'Fri': 'No', 'Hun': 'Yes', 'Pat': 'Some',\n",
" 'Price': '$$', 'Rain': 'Yes', 'Res': 'Yes', 'Type': 'Italian', 'Est': '0-10',\n",
" 'GOAL': True},\n",
"\n",
" {'Alt': 'No', 'Bar': 'Yes', 'Fri': 'No', 'Hun': 'No', 'Pat': 'None',\n",
" 'Price': '$', 'Rain': 'Yes', 'Res': 'No', 'Type': 'Burger', 'Est': '0-10',\n",
" 'GOAL': False},\n",
"\n",
" {'Alt': 'No', 'Bar': 'No', 'Fri': 'No', 'Hun': 'Yes', 'Pat': 'Some',\n",
" 'Price': '$$', 'Rain': 'Yes', 'Res': 'Yes', 'Type': 'Thai', 'Est': '0-10',\n",
" 'GOAL': True},\n",
"\n",
" {'Alt': 'No', 'Bar': 'Yes', 'Fri': 'Yes', 'Hun': 'No', 'Pat': 'Full',\n",
" 'Price': '$', 'Rain': 'Yes', 'Res': 'No', 'Type': 'Burger', 'Est': '>60',\n",
" 'GOAL': False},\n",
"\n",
" {'Alt': 'Yes', 'Bar': 'Yes', 'Fri': 'Yes', 'Hun': 'Yes', 'Pat': 'Full',\n",
" 'Price': '$$$', 'Rain': 'No', 'Res': 'Yes', 'Type': 'Italian', 'Est': '10-30',\n",
" 'GOAL': False},\n",
"\n",
" {'Alt': 'No', 'Bar': 'No', 'Fri': 'No', 'Hun': 'No', 'Pat': 'None',\n",
" 'Price': '$', 'Rain': 'No', 'Res': 'No', 'Type': 'Thai', 'Est': '0-10',\n",
" 'GOAL': False},\n",
"\n",
" {'Alt': 'Yes', 'Bar': 'Yes', 'Fri': 'Yes', 'Hun': 'Yes', 'Pat': 'Full',\n",
" 'Price': '$', 'Rain': 'No', 'Res': 'No', 'Type': 'Burger', 'Est': '30-60',\n",
" 'GOAL': True}\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Say our initial hypothesis is that there should be an alternative option and let's run the algorithm."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n",
"True\n",
"False\n",
"True\n",
"False\n",
"True\n",
"False\n",
"False\n",
"False\n",
"True\n"
]
}
],
"source": [
"initial_h = [{'Alt': 'Yes'}]\n",
"h = current_best_learning(restaurant, initial_h)\n",
"for e in restaurant:\n",
" print(guess_value(e, h))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The predictions are correct. Let's see the hypothesis that accomplished that:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'Type': '!Thai', 'Fri': '!Yes', 'Alt': 'Yes'}, {'Fri': 'No', 'Type': 'Burger', 'Pat': '!None', 'Alt': 'No'}, {'Fri': 'Yes', 'Est': '10-30', 'Pat': 'Full', 'Rain': 'Yes', 'Res': 'No', 'Bar': 'No', 'Price': '$'}, {'Fri': 'No', 'Est': '0-10', 'Pat': 'Some', 'Res': 'Yes', 'Type': 'Italian', 'Alt': 'No'}, {'Fri': 'No', 'Pat': 'Some', 'Res': 'Yes', 'Type': 'Thai', 'Hun': 'Yes', 'Alt': 'No', 'Price': '$$'}, {'Fri': 'Yes', 'Pat': 'Full', 'Rain': 'No', 'Alt': 'Yes', 'Type': 'Burger', 'Hun': 'Yes', 'Bar': 'Yes', 'Price': '$'}]\n"
]
}
],
"source": [
"print(h)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It might be quite complicated, with many disjunctions if we are unlucky, but it will always be correct, as long as a correct hypothesis exists."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}