Skip to content

Commit 790213a

Browse files
Chipe1norvig
authored andcommitted
Added minimal-consistent-det (aimacode#610)
1 parent 1d645d4 commit 790213a

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

knowledge.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from random import shuffle
44
from utils import powerset
55
from collections import defaultdict
6+
from itertools import combinations
67

78
# ______________________________________________________________________________
89

@@ -205,6 +206,29 @@ def build_h_combinations(hypotheses):
205206
# ______________________________________________________________________________
206207

207208

209+
def minimal_consistent_det(E, A):
210+
n = len(A)
211+
212+
for i in range(n + 1):
213+
for A_i in combinations(A, i):
214+
if consistent_det(A_i, E):
215+
return set(A_i)
216+
217+
218+
def consistent_det(A, E):
219+
H = {}
220+
221+
for e in E:
222+
attr_values = tuple(e[attr] for attr in A)
223+
if attr_values in H and H[attr_values] != e['GOAL']:
224+
return False
225+
H[attr_values] = e['GOAL']
226+
227+
return True
228+
229+
# ______________________________________________________________________________
230+
231+
208232
def check_all_consistency(examples, h):
209233
"""Check for the consistency of all examples under h"""
210234
for e in examples:

tests/test_knowledge.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def test_version_space_learning():
4949
assert [{'Pizza': 'Yes'}] in V
5050

5151

52+
def test_minimal_consistent_det():
53+
assert minimal_consistent_det(party, {'Pizza', 'Soda'}) == {'Pizza'}
54+
assert minimal_consistent_det(party[:2], {'Pizza', 'Soda'}) == set()
55+
assert minimal_consistent_det(animals_umbrellas, {'Species', 'Rain', 'Coat'}) == {'Species', 'Rain', 'Coat'}
56+
assert minimal_consistent_det(conductance, {'Mass', 'Temp', 'Material', 'Size'}) == {'Temp', 'Material'}
57+
assert minimal_consistent_det(conductance, {'Mass', 'Temp', 'Size'}) == {'Mass', 'Temp', 'Size'}
58+
59+
5260
party = [
5361
{'Pizza': 'Yes', 'Soda': 'No', 'GOAL': True},
5462
{'Pizza': 'Yes', 'Soda': 'Yes', 'GOAL': True},
@@ -65,6 +73,18 @@ def test_version_space_learning():
6573
{'Species': 'Cat', 'Rain': 'No', 'Coat': 'Yes', 'GOAL': True}
6674
]
6775

76+
conductance = [
77+
{'Sample': 'S1', 'Mass': 12, 'Temp': 26, 'Material': 'Cu', 'Size': 3, 'GOAL': 0.59},
78+
{'Sample': 'S1', 'Mass': 12, 'Temp': 100, 'Material': 'Cu', 'Size': 3, 'GOAL': 0.57},
79+
{'Sample': 'S2', 'Mass': 24, 'Temp': 26, 'Material': 'Cu', 'Size': 6, 'GOAL': 0.59},
80+
{'Sample': 'S3', 'Mass': 12, 'Temp': 26, 'Material': 'Pb', 'Size': 2, 'GOAL': 0.05},
81+
{'Sample': 'S3', 'Mass': 12, 'Temp': 100, 'Material': 'Pb', 'Size': 2, 'GOAL': 0.04},
82+
{'Sample': 'S4', 'Mass': 18, 'Temp': 100, 'Material': 'Pb', 'Size': 3, 'GOAL': 0.04},
83+
{'Sample': 'S4', 'Mass': 18, 'Temp': 100, 'Material': 'Pb', 'Size': 3, 'GOAL': 0.04},
84+
{'Sample': 'S5', 'Mass': 24, 'Temp': 100, 'Material': 'Pb', 'Size': 4, 'GOAL': 0.04},
85+
{'Sample': 'S6', 'Mass': 36, 'Temp': 26, 'Material': 'Pb', 'Size': 6, 'GOAL': 0.05},
86+
]
87+
6888
def r_example(Alt, Bar, Fri, Hun, Pat, Price, Rain, Res, Type, Est, GOAL):
6989
return {'Alt': Alt, 'Bar': Bar, 'Fri': Fri, 'Hun': Hun, 'Pat': Pat,
7090
'Price': Price, 'Rain': Rain, 'Res': Res, 'Type': Type, 'Est': Est,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy