Experiment-2
For a given set of training data examples stored in a .CSV file, implement and demonstrate
the Candidate-Elimination algorithm to output a description of the set of all hypotheses
consistent with the training examples.
The Candidate-Elimination Algorithm computes the version space containing all hypotheses
from H that are consistent with an observed sequence of training examples. It begins by
initializing the version space to the set of all hypotheses in H; that is, by initializing the G
boundary set to contain the most general hypothesis in H
G0← {(?,?,?,?,?,?)}
and initializing the S boundary set to contain the most specific (least general)hypothesis
S0← {(ø, ø, ø, ø, ø, ø)}
Example Sky AirTemp Humidity Wind Water Forecast EnjoySport
1 Sunny Warm Normal Strong Warm Same Yes
2 Sunny Warm High Strong Warm Same Yes
3 Rainy Cold High Strong Warm Change No
4 Sunny Warm High Strong Cool Change Yes
import numpy as np
import pandas as pd
data = pd.read_csv('/content/play1.csv')
data
concepts = np.array(data.iloc[:,:-1])
target = np.array(data.iloc[:,-1])
def learn(concepts, target):
specific_h = concepts[0].copy()
general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_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] = '?'
print(" \n\nFor Training instance No:{0} the hypothesis is\n".format(i))
print("Specific Hypothesis: ",specific_h)
print("General Hypothesis: ",general_h,)
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(" \n\nFor Training instance No:{0} the hypothesis is\n".format(i))
print("Specific Hypothesis: ", specific_h)
print("General Hypothesis: ",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
print("*"*20,"Candidate-Elimination algorithm","*"*20)
s_final, g_final = learn(concepts, target)
print("Final Specific hypothesis:", s_final)
print("Final General hypothesis:", g_final)
OUTPUT:
***************** Candidate-Elimination algorithm ********************
For Training instance No:0 the hypothesis is
Specific Hypothesis: ['Sunny' 'warm' 'Normal' 'Strong' 'Warm' 'Same']
General Hypothesis: [['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?',
'?']]
For Training instance No:1 the hypothesis is
Specific Hypothesis: ['Sunny' 'warm' '?' 'Strong' 'Warm' 'Same']
General Hypothesis: [['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?',
'?']]
For Training instance No:2 the hypothesis is
Specific Hypothesis: ['Sunny' 'warm' '?' 'Strong' 'Warm' 'Same']
General Hypothesis: [['Sunny', '?', '?', '?', '?', '?'], ['?', 'warm',
'?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', 'Same']]
For Training instance No:3 the hypothesis is
Specific Hypothesis: ['Sunny' 'warm' '?' 'Strong' '?' '?']
General Hypothesis: [['Sunny', '?', '?', '?', '?', '?'], ['?', 'warm',
'?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?']]
Final Specific hypothesis: ['Sunny' 'warm' '?' 'Strong' '?' '?']
Final General hypothesis: [['Sunny', '?', '?', '?', '?', '?'], ['?',
'warm', '?', '?', '?', '?']]