-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANDEL.py
40 lines (35 loc) · 1.19 KB
/
CANDEL.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
import csv
a = []
with open('manufacturer.csv', 'r') as trainData:
for row in csv.reader(trainData):
a.append(row)
print(row)
n=len(a[0])-1
print("\n The initial value of hypothesis: ")
s = ['0'] * n
g = ['?'] * n
print ("\n The most specific hypothesis S0 :",s)
print (" \n The most general hypothesis G0 :",g)
s=a[0][:-1]
temp=[]
print("\n Candidate Elimination algorithm\n")
for i in range(len(a)):
if a[i][n]=="yes":
for j in range(n):
if a[i][j]!=s[j]:
s[j]='?'
for j in range(n):
for k in range(len(temp)-1):
if temp[k][j]!='?' and temp[k][j]!=s[j]:
del temp[k]
if a[i][n]=="no":
for j in range(n):
if s[j]!=a[i][j] and s[j]!='?':
g[j]=s[j]
temp.append(g)
g= ['?']*n
print("\n For Training Example No :{0} the hypothesis is S{0} ".format(i+1),s)
if (len(temp)==0):
print(" For Training Example No :{0} the hypothesis is G{0} ".format(i+1),g)
else:
print(" For Training Example No :{0} the hypothesis is G{0}".format(i+1),temp)