Candidate Elimination

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Dileepchandu G

20BCI7313

Candidate Elimination Algorithm

In [4]: import numpy as np


import pandas as pd

data = pd.read_csv('enjoysport.csv')

In [3]: concepts = np.array(data.iloc[:, 0:-1])


target = np.array(data.iloc[:, -1])

In [4]: def learn(concepts, target):


specific_h = concepts[0].copy()
general_h = [["?" for i in range(len(specific_h))] for j 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] = '?'
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] = '?'
indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]
for i in indices:
general_h.remove(['?', '?', '?', '?', '?', '?'])
return specific_h, general_h

In [5]: s_final, g_final = learn(concepts, target)

In [6]: print('Final S: ', s_final, sep="\n")


print('Final G: ', g_final, sep="\n")

Final S:
['sunny' 'warm' '?' 'strong' '?' '?']
Final G:
[['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?', '?', '?']]

In [7]: data.head()

Out[7]: sky airTemp humidity wind water forecast enjoySport

0 sunny warm normal strong warm same yes

1 sunny warm high strong warm same yes

2 rainy cold high strong warm change no

3 sunny warm high strong cool change yes

In [ ]:

You might also like