Ex 1 in ML
Ex 1 in ML
AIM
To illustrate this algorithm, assume the learner is given the sequence of training examples
from the EnjoySport task.
Sky Airtem Humidit Wind Enjoyspor
p y t
Sunny Warm Normal Strong Yes
Sunny Warm High Strong Yes
Rainy Cold High Strong No
Sunny Warm High Strong Yes
CANDIDATE-ELIMINTION algorithm begins by initializing the version space to the set of
all hypotheses in H;
Step 1
For training example d,
(Sunny ,Warm,Normal,Strong) +
S0 <∅ ,∅ ,∅ , ∅ >
G2 <?, ?,?,?>
Step 3
For training example d,
(Rainy,Cold,High,Strong)-
OUTPUT
PROGRAM
import numpy as np
import pandas as pd
data = pd.read_csv('a.csv')
concepts = np.array(data.iloc[:, 0:-1])
target = np.array(data.iloc[:, -1])
def learn(concepts, target):
specific_h = concepts[0].copy()
print("initialization of specific_h\n", specific_h)
general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_h))]
print("initialization of general_h\n", general_h)
for i, h in enumerate(concepts):
if target[i] == "yes":
print("if instance is Positive")
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = '?'
general_h[x][x] = '?'
else:
general_h[x][x] = '?'
elif target[i] == "no":
print("if instance is Negative")
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("step{}".format(i + 1))
print(specific_h)
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
s_final, g_final = learn(concepts, target)
print("Final Specific_h:", s_final, sep="\n")
print("Final General_h:", g_final, sep="\n")
Result: