In
[1]:
def learn(concepts, target):
print('Concepts list =',concepts)
print('Target list = ',target)
specific_h = concepts[0].copy()
print("initialization of specific_h and general_h")
print(specific_h)
general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_h))]
print(general_h)
for i, h in enumerate(concepts):
if target[i] == "Yes":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = '?'
general_h[x][x] = '?'
if target[i] == "No":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = '?'
print(" steps of Candidate Elimination Algorithm",i+1)
print("Specific_h ",i+1,"\n ")
print(specific_h)
print("general_h ", i+1, "\n ")
print(general_h)
indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]
for i in indices:
general_h.remove(['?', '?', '?', '?', '?', '?'])
return specific_h, general_h
In [2]:
rows = open('C:/Users/User/College/Training.csv')
a=[]
p=[]
concepts=[]
target=[]
for r in rows:
a=r.split(",")
p.append(a)
for l in p[1:]:
target.append(l[-1].rstrip())
concepts.append(l[:-1])
s_final, g_final = learn(concepts, target)
print("Final Specific_h:", s_final, sep="\n")
print("Final General_h:", g_final, sep="\n")
Concepts list = [['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same'], ['Sunny', 'Warm', 'High', 'S
trong', 'Warm', 'Same'], ['Cloudy', 'Cold', 'High', 'Strong', 'Warm', 'Change'], ['Sunny', 'Warm', '
High', 'Strong', 'Cool', 'Change']]
Target list = ['Yes', 'Yes', 'No', 'Yes']
initialization of specific_h and general_h
['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?
', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
steps of Candidate Elimination Algorithm 1
Specific_h 1
['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
general_h 1
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?
', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
steps of Candidate Elimination Algorithm 2
Specific_h 2
['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
general_h 2
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?
', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
steps of Candidate Elimination Algorithm 3
Specific_h 3
['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
general_h 3
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?
'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', 'Same'
]]
steps of Candidate Elimination Algorithm 4
Specific_h 4
['Sunny', 'Warm', '?', 'Strong', '?', '?']
general_h 4
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?
'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Final Specific_h:
['Sunny', 'Warm', '?', 'Strong', '?', '?']
Final General_h:
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?']]
In [ ]: