0% found this document useful (0 votes)
3 views

lab_program-2

The document outlines the implementation of the Candidate-Elimination algorithm to determine hypotheses consistent with training examples from a CSV file. It describes the algorithm's steps, including handling positive and negative examples, and provides a Python program that demonstrates the learning process using a dataset. The final output shows the specific and general hypotheses derived from the training examples.

Uploaded by

n44943916
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lab_program-2

The document outlines the implementation of the Candidate-Elimination algorithm to determine hypotheses consistent with training examples from a CSV file. It describes the algorithm's steps, including handling positive and negative examples, and provides a Python program that demonstrates the learning process using a dataset. The final output shows the specific and general hypotheses derived from the training examples.

Uploaded by

n44943916
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 incrementally builds the version space


given a hypothesis space H and a set E of examples. The examples are added
one by one; each example possibly shrinks the version space by removing the
hypotheses that are inconsistent with the example. The candidate elimination
algorithm does this by updating the general and specific boundary for each new
example.
 You can consider this as an extended form of Find-S algorithm.
 Consider both positive and negative examples.
 Actually, positive examples are used here as Find-S algorithm (Basically
they are generalizing from the specification).
 While the negative example is specified from generalize form.

CANDIDATE-ELIMINATION Learning Algorithm

The CANDIDATE-ELIMINTION algorithm computes the version space containing all


hypotheses from H that are consistent with an observed sequence of training examples.

Algorithm:
Step1: Load Data set
Step2: Initialize General Hypothesis and Specific Hypothesis.
Step3: For each training example
Step4: If example is positive example
if attribute_value == hypothesis_value:
Do nothing
else:
replace attribute value with '?' (Basically generalizing it)
Step5: If example is Negative example
Make generalize hypothesis more specific.

CANDIDATE- ELIMINTION algorithm using version spaces

Training Examples:

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
1
4 Sunny Warm High Strong Cool Change Yes

2
Program:

import numpy as np
import pandas as pd
data = pd.DataFrame(data=pd.read_csv('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("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] ='?'
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")

3
Data Set:

Sky AirTemp Humidity Wind Water Forecast EnjoySport

sunny warm normal strong warm same yes


sunny warm high strong warm same yes
rainy cold high strong warm change no
sunny warm high strong cool change yes

Output:

Final Specific_h:
['sunny' 'warm' '?' 'strong' '?' '?']

Final General_h:
[['sunny', '?', '?', '?', '?', '?'],
['?', 'warm', '?', '?', '?', '?']]

You might also like