0% found this document useful (0 votes)
12 views2 pages

MLT Experiment 2

Aktu

Uploaded by

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

MLT Experiment 2

Aktu

Uploaded by

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

Experiment No- 2

Aim: -
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.

Candidate Elimination Algorithm: -

1. Initialize specific_h to the most specific hypothesis in the hypothesis space.

2. Initialize general_h to the most general hypothesis in the hypothesis space.

3. For each training example:


a. If it is a positive example (target = Y):
 Update specific_h and general_h to include only attributes
consistent with the example.
b. If it is a negative example (target = N):
 Update general_h to exclude attributes inconsistent with the
example.

4. Remove any hypotheses in general_h that are more specific than any hypothesis in
specific_h.

5. Return specific_h and general_h as the final hypotheses.

Program Code: -
# --> PRATEEK 2201921539010 <--

import numpy as np
import pandas as pd

# Load the training data from the CSV file


data = pd.read_csv('/content/New.csv')

# Separate features (concepts) and target variable


concepts = data.iloc[:, :-1].values
target = data.iloc[:, -1].values

# Define the function to implement the Candidate Elimination algorithm


def candidate_elimination(concepts, target):
specific_h = concepts[0].copy() # Initialize specific_h with the first instance
general_h = [['?' for _ in range(len(specific_h))] for _ in range(len(specific_h))]

# Iterate over each instance in the dataset


for i, h in enumerate(concepts):
if target[i] == "Y": # Case: Positive instance
for x in range(len(specific_h)):
if h[x]! = specific_h[x]:
specific_h[x] = ‘?’ # Update specific_h to '?' if different from current specific_h
general_h[x][x] = ‘?’ # Update general_h to '?' if different from current specific_h

if target[i] == "N": # Case: Negative instance


for x in range(len(specific_h)):
if h[x]! = specific_h[x]:
general_h[x][x] = specific_h[x] # Update if different from current specific_h
else:
general_h[x][x] = ‘?’ # Otherwise, update general_h to '?'

return specific_h, general_h

# Apply the Candidate Elimination algorithm to get the final hypothesis


s_final, g_final = candidate_elimination(concepts, target)

# Print the final hypotheses


print ("Final Specific_h:")
print(s_final)
print ("Final General_h:")
print(g_final)

Dataset: -

Product Price Discount Will_Purchase


Laptop High Yes Yes
Phone Medium No No
TV High Yes No
Headphones Low No Yes
Tablet Medium Yes No
Camera High No No
Watch Medium Yes Yes
Speakers Low No Yes
Gaming Console High Yes Yes
Smartwatch Medium No No

Output: -

You might also like