0% found this document useful (0 votes)
16 views7 pages

Lab 2

Uploaded by

22b148
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Lab 2

Uploaded by

22b148
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.No.

1 CANDIDATE ELIMINATION
19-03-24 LEARNING ALGORITHM

Aim:
To implement and demonstrate the Candidate-
Elimination algorithm for a given set of training data
examples stored in a .CSV file (enjoysport.csv) to output a
description of the set of all hypotheses consistent with the
training examples

Candidate-Elimination Learning Algorithm:

The candidate-elimination algorithm computes the


version space containing all hypotheses from H that are
consistent with an observed sequence of training examples.

1. Initialize G to the set of maximally general


hypotheses in H
2. Initialize S to the set of maximally specific
hypotheses in H
3. For each training example d, do
If d is a positive example,
• Remove from G any hypothesis inconsistent
with d
• For each hypothesis s in S that is not
consistent with d, Remove s from S
• Add to S all minimal generalizations h of s
such that his consistent with d, and some
member of G is more general than h
• Remove from S any hypothesis that is more
general than another hypothesis in S

If d is a negative example
 Remove from S any hypothesis inconsistent with d
 For each hypothesis g in G that is not consistent
withd
 Remove g from G
 Add to G all minimal specializations h of g such that
 his consistent with d, and some member of S is more
specific than h
 Remove from G any hypothesis that is less general
than another hypothesis in G
Program:

import numpy as np
import pandas as pd

data =
pd.DataFrame(data=pd.read_csv('Downloads/enjoysport.csv'))
concepts = np.array(data.iloc[:,0:-1])
print(concepts)
target = np.array(data.iloc[:,-1])
print(target)

def learn(concepts,target):
specific_h = concepts[0].copy()
print("initialisation 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]='?'
print(specific_h)
print(specific_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("steps of candidate Elimination Algorithm",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:

Thus the program to implement candidate elimination algorithm


was executed successfully.

You might also like