-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFind-S.py
60 lines (52 loc) · 2.27 KB
/
Find-S.py
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
import random
attributes = [['Sunny','Rainy'],
['Warm','Cold'],
['Normal','High'],
['Strong','Weak'],
['Warm','Cool'],
['Same','Change']]
num_attributes = len(attributes)
def getRandomTrainingExample(target_concept = ['?'] * num_attributes):
training_example = []
classification = True
for i in range(num_attributes):
training_example.append(attributes[i][random.randint(0,1)])
if target_concept[i] != '?' and target_concept[i] != training_example[i]:
classification = False
return training_example, classification
def findS(training_examples = []):
hypothesis = ['0'] * num_attributes
for example in training_examples:
if example[1]:
for i in range(num_attributes):
example_attribute = example[0][i]
hypothesis_attribute = hypothesis[i]
if example_attribute == attributes[i][0]:
if hypothesis_attribute == '0':
hypothesis_attribute = attributes[i][0]
elif hypothesis_attribute == attributes[i][1]:
hypothesis_attribute = '?'
elif example_attribute == attributes[i][1]:
if hypothesis_attribute == '0':
hypothesis_attribute = attributes[i][1]
elif hypothesis_attribute == attributes[i][0]:
hypothesis_attribute = '?'
hypothesis[i] = hypothesis_attribute
return hypothesis
def experiment(target_concept = ['?'] * num_attributes):
training_examples = []
while findS(training_examples) != target_concept:
training_examples.append(getRandomTrainingExample(target_concept))
return len(training_examples)
def main():
target_concept = ['Sunny','Warm','?','?','?','?']
num_experiments = 1000
experiment_results = []
for i in range(num_experiments):
experiment_results.append(experiment(target_concept))
average_result = sum(experiment_results) / num_experiments
print(str(len(experiment_results)) + ' Experiments Ran')
print('Average # Examples Required: ' + str(average_result))
print('Target Concept:' + str(target_concept))
if __name__ == "__main__":
main()