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

Skip to content

Some optimizations in knowledge.py and test_knowledge.py #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
# ______________________________________________________________________________


def current_best_learning(examples, h, examples_so_far=None):
def current_best_learning(examples, h, examples_so_far=[]):
""" [Figure 19.2]
The hypothesis is a list of dictionaries, with each dictionary representing
a disjunction."""
if not examples:
return h

examples_so_far = examples_so_far or []
e = examples[0]
if is_consistent(e, h):
return current_best_learning(examples[1:], h, examples_so_far + [e])
Expand Down
2 changes: 1 addition & 1 deletion knowledge_current_best.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.6.7"
}
},
"nbformat": 4,
Expand Down
12 changes: 3 additions & 9 deletions tests/test_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,21 @@ def test_current_best_learning():
examples = restaurant
hypothesis = [{'Alt': 'Yes'}]
h = current_best_learning(examples, hypothesis)
values = []
for e in examples:
values.append(guess_value(e, h))
values = [guess_value(e, h) for e in examples]

assert values == [True, False, True, True, False, True, False, True, False, False, False, True]

examples = animals_umbrellas
initial_h = [{'Species': 'Cat'}]
h = current_best_learning(examples, initial_h)
values = []
for e in examples:
values.append(guess_value(e, h))
values = [guess_value(e, h) for e in examples]

assert values == [True, True, True, False, False, False, True]

examples = party
initial_h = [{'Pizza': 'Yes'}]
h = current_best_learning(examples, initial_h)
values = []
for e in examples:
values.append(guess_value(e, h))
values = [guess_value(e, h) for e in examples]

assert values == [True, True, False]

Expand Down