11
11
# ______________________________________________________________________________
12
12
13
13
14
- def current_best_learning (examples , h , examples_so_far = [] ):
14
+ def current_best_learning (examples , h , examples_so_far = None ):
15
15
""" [Figure 19.2]
16
16
The hypothesis is a list of dictionaries, with each dictionary representing
17
17
a disjunction."""
18
18
if not examples :
19
19
return h
20
20
21
+ examples_so_far = examples_so_far or []
21
22
e = examples [0 ]
22
23
if is_consistent (e , h ):
23
24
return current_best_learning (examples [1 :], h , examples_so_far + [e ])
@@ -95,7 +96,7 @@ def generalizations(examples_so_far, h):
95
96
96
97
97
98
def add_or (examples_so_far , h ):
98
- """Adds an OR operation to the hypothesis. The AND operations in the disjunction
99
+ """Add an OR operation to the hypothesis. The AND operations in the disjunction
99
100
are generated by the last example (which is the problematic one)."""
100
101
ors = []
101
102
e = examples_so_far [- 1 ]
@@ -135,7 +136,7 @@ def version_space_update(V, e):
135
136
136
137
137
138
def all_hypotheses (examples ):
138
- """Builds a list of all the possible hypotheses"""
139
+ """Build a list of all the possible hypotheses"""
139
140
values = values_table (examples )
140
141
h_powerset = powerset (values .keys ())
141
142
hypotheses = []
@@ -148,7 +149,7 @@ def all_hypotheses(examples):
148
149
149
150
150
151
def values_table (examples ):
151
- """Builds a table with all the possible values for each attribute.
152
+ """Build a table with all the possible values for each attribute.
152
153
Returns a dictionary with keys the attribute names and values a list
153
154
with the possible values for the corresponding attribute."""
154
155
values = defaultdict (lambda : [])
@@ -210,7 +211,7 @@ def build_h_combinations(hypotheses):
210
211
211
212
212
213
def minimal_consistent_det (E , A ):
213
- """Returns a minimal set of attributes which give consistent determination"""
214
+ """Return a minimal set of attributes which give consistent determination"""
214
215
n = len (A )
215
216
216
217
for i in range (n + 1 ):
@@ -220,7 +221,7 @@ def minimal_consistent_det(E, A):
220
221
221
222
222
223
def consistent_det (A , E ):
223
- """Checks if the attributes(A) is consistent with the examples(E)"""
224
+ """Check if the attributes(A) is consistent with the examples(E)"""
224
225
H = {}
225
226
226
227
for e in E :
@@ -235,9 +236,9 @@ def consistent_det(A, E):
235
236
236
237
237
238
class FOIL_container (FolKB ):
238
- """Holds the kb and other necessary elements required by FOIL"""
239
+ """Hold the kb and other necessary elements required by FOIL. """
239
240
240
- def __init__ (self , clauses = [] ):
241
+ def __init__ (self , clauses = None ):
241
242
self .const_syms = set ()
242
243
self .pred_syms = set ()
243
244
FolKB .__init__ (self , clauses )
@@ -251,7 +252,7 @@ def tell(self, sentence):
251
252
raise Exception ("Not a definite clause: {}" .format (sentence ))
252
253
253
254
def foil (self , examples , target ):
254
- """Learns a list of first-order horn clauses
255
+ """Learn a list of first-order horn clauses
255
256
'examples' is a tuple: (positive_examples, negative_examples).
256
257
positive_examples and negative_examples are both lists which contain substitutions."""
257
258
clauses = []
@@ -268,10 +269,10 @@ def foil(self, examples, target):
268
269
return clauses
269
270
270
271
def new_clause (self , examples , target ):
271
- """Finds a horn clause which satisfies part of the positive
272
+ """Find a horn clause which satisfies part of the positive
272
273
examples but none of the negative examples.
273
274
The horn clause is specified as [consequent, list of antecedents]
274
- Return value is the tuple (horn_clause, extended_positive_examples)"""
275
+ Return value is the tuple (horn_clause, extended_positive_examples). """
275
276
clause = [target , []]
276
277
# [positive_examples, negative_examples]
277
278
extended_examples = examples
@@ -284,14 +285,14 @@ def new_clause(self, examples, target):
284
285
return (clause , extended_examples [0 ])
285
286
286
287
def extend_example (self , example , literal ):
287
- """Generates extended examples which satisfy the literal"""
288
+ """Generate extended examples which satisfy the literal. """
288
289
# find all substitutions that satisfy literal
289
290
for s in self .ask_generator (subst (example , literal )):
290
291
s .update (example )
291
292
yield s
292
293
293
294
def new_literals (self , clause ):
294
- """Generates new literals based on known predicate symbols.
295
+ """Generate new literals based on known predicate symbols.
295
296
Generated literal must share atleast one variable with clause"""
296
297
share_vars = variables (clause [0 ])
297
298
for l in clause [1 ]:
@@ -304,7 +305,7 @@ def new_literals(self, clause):
304
305
yield Expr (pred , * [var for var in args ])
305
306
306
307
def choose_literal (self , literals , examples ):
307
- """Chooses the best literal based on the information gain"""
308
+ """Choose the best literal based on the information gain. """
308
309
def gain (l ):
309
310
pre_pos = len (examples [0 ])
310
311
pre_neg = len (examples [1 ])
@@ -328,8 +329,8 @@ def represents(d):
328
329
return max (literals , key = gain )
329
330
330
331
def update_examples (self , target , examples , extended_examples ):
331
- """Adds to the kb those examples what are represented in extended_examples
332
- List of omitted examples is returned"""
332
+ """Add to the kb those examples what are represented in extended_examples
333
+ List of omitted examples is returned. """
333
334
uncovered = []
334
335
for example in examples :
335
336
def represents (d ):
@@ -346,7 +347,7 @@ def represents(d):
346
347
347
348
348
349
def check_all_consistency (examples , h ):
349
- """Check for the consistency of all examples under h"""
350
+ """Check for the consistency of all examples under h. """
350
351
for e in examples :
351
352
if not is_consistent (e , h ):
352
353
return False
@@ -355,7 +356,7 @@ def check_all_consistency(examples, h):
355
356
356
357
357
358
def check_negative_consistency (examples , h ):
358
- """Check if the negative examples are consistent under h"""
359
+ """Check if the negative examples are consistent under h. """
359
360
for e in examples :
360
361
if e ['GOAL' ]:
361
362
continue
@@ -367,7 +368,7 @@ def check_negative_consistency(examples, h):
367
368
368
369
369
370
def disjunction_value (e , d ):
370
- """The value of example e under disjunction d"""
371
+ """The value of example e under disjunction d. """
371
372
for k , v in d .items ():
372
373
if v [0 ] == '!' :
373
374
# v is a NOT expression
@@ -381,7 +382,7 @@ def disjunction_value(e, d):
381
382
382
383
383
384
def guess_value (e , h ):
384
- """Guess value of example e under hypothesis h"""
385
+ """Guess value of example e under hypothesis h. """
385
386
for d in h :
386
387
if disjunction_value (e , d ):
387
388
return True
@@ -394,16 +395,8 @@ def is_consistent(e, h):
394
395
395
396
396
397
def false_positive (e , h ):
397
- if e ["GOAL" ] == False :
398
- if guess_value (e , h ):
399
- return True
400
-
401
- return False
398
+ return guess_value (e , h ) and not e ["GOAL" ]
402
399
403
400
404
401
def false_negative (e , h ):
405
- if e ["GOAL" ] == True :
406
- if not guess_value (e , h ):
407
- return True
408
-
409
- return False
402
+ return e ["GOAL" ] and not guess_value (e , h )
0 commit comments