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

Skip to content

Added minimal-consistent-det #610

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
Aug 8, 2017
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
24 changes: 24 additions & 0 deletions knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from random import shuffle
from utils import powerset
from collections import defaultdict
from itertools import combinations

# ______________________________________________________________________________

Expand Down Expand Up @@ -205,6 +206,29 @@ def build_h_combinations(hypotheses):
# ______________________________________________________________________________


def minimal_consistent_det(E, A):
n = len(A)

for i in range(n + 1):
for A_i in combinations(A, i):
if consistent_det(A_i, E):
return set(A_i)


def consistent_det(A, E):
H = {}

for e in E:
attr_values = tuple(e[attr] for attr in A)
if attr_values in H and H[attr_values] != e['GOAL']:
return False
H[attr_values] = e['GOAL']

return True

# ______________________________________________________________________________


def check_all_consistency(examples, h):
"""Check for the consistency of all examples under h"""
for e in examples:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def test_version_space_learning():
assert [{'Pizza': 'Yes'}] in V


def test_minimal_consistent_det():
assert minimal_consistent_det(party, {'Pizza', 'Soda'}) == {'Pizza'}
assert minimal_consistent_det(party[:2], {'Pizza', 'Soda'}) == set()
assert minimal_consistent_det(animals_umbrellas, {'Species', 'Rain', 'Coat'}) == {'Species', 'Rain', 'Coat'}
assert minimal_consistent_det(conductance, {'Mass', 'Temp', 'Material', 'Size'}) == {'Temp', 'Material'}
assert minimal_consistent_det(conductance, {'Mass', 'Temp', 'Size'}) == {'Mass', 'Temp', 'Size'}


party = [
{'Pizza': 'Yes', 'Soda': 'No', 'GOAL': True},
{'Pizza': 'Yes', 'Soda': 'Yes', 'GOAL': True},
Expand All @@ -65,6 +73,18 @@ def test_version_space_learning():
{'Species': 'Cat', 'Rain': 'No', 'Coat': 'Yes', 'GOAL': True}
]

conductance = [
{'Sample': 'S1', 'Mass': 12, 'Temp': 26, 'Material': 'Cu', 'Size': 3, 'GOAL': 0.59},
{'Sample': 'S1', 'Mass': 12, 'Temp': 100, 'Material': 'Cu', 'Size': 3, 'GOAL': 0.57},
{'Sample': 'S2', 'Mass': 24, 'Temp': 26, 'Material': 'Cu', 'Size': 6, 'GOAL': 0.59},
{'Sample': 'S3', 'Mass': 12, 'Temp': 26, 'Material': 'Pb', 'Size': 2, 'GOAL': 0.05},
{'Sample': 'S3', 'Mass': 12, 'Temp': 100, 'Material': 'Pb', 'Size': 2, 'GOAL': 0.04},
{'Sample': 'S4', 'Mass': 18, 'Temp': 100, 'Material': 'Pb', 'Size': 3, 'GOAL': 0.04},
{'Sample': 'S4', 'Mass': 18, 'Temp': 100, 'Material': 'Pb', 'Size': 3, 'GOAL': 0.04},
{'Sample': 'S5', 'Mass': 24, 'Temp': 100, 'Material': 'Pb', 'Size': 4, 'GOAL': 0.04},
{'Sample': 'S6', 'Mass': 36, 'Temp': 26, 'Material': 'Pb', 'Size': 6, 'GOAL': 0.05},
]

def r_example(Alt, Bar, Fri, Hun, Pat, Price, Rain, Res, Type, Est, GOAL):
return {'Alt': Alt, 'Bar': Bar, 'Fri': Fri, 'Hun': Hun, 'Pat': Pat,
'Price': Price, 'Rain': Rain, 'Res': Res, 'Type': Type, 'Est': Est,
Expand Down