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

EXP 4-Find S

The document outlines the implementation of the Find-S algorithm using a CSV file containing training data. It describes the process of loading the data, initializing a hypothesis, iterating through the dataset to update the hypothesis based on positive examples, and returning the final hypothesis. The provided source code demonstrates these steps using the pandas library in Python.

Uploaded by

s7743258
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)
60 views2 pages

EXP 4-Find S

The document outlines the implementation of the Find-S algorithm using a CSV file containing training data. It describes the process of loading the data, initializing a hypothesis, iterating through the dataset to update the hypothesis based on positive examples, and returning the final hypothesis. The provided source code demonstrates these steps using the pandas library in Python.

Uploaded by

s7743258
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

Program 4:

AIM: For a given set of training data examples stored in a .CSV file, implement and demonstrate the
Find-S algorithm to output a description of the set of all hypotheses consistent with the training
examples.
Solu:
Explanation:
Data Loading:The code reads a CSV file training_data.csv using the pandas library to load the data into a
DataFrame.
attributes = data.columns[:-1]: This line gets all the columns except the last one (which is assumed to be the
class label).
class_label = data.columns[-1]: This line gets the last column which represents the class label (in your case,
presumably 'Yes' or 'No').
Initialize Hypothesis:hypothesis = ['?' for _ in attributes]: A list is initialized with ? for each attribute,
representing an unknown or undefined condition for the hypothesis.
Iterate Over Rows:For each row in the dataset, it checks if the class label is 'Yes' (i.e., the positive
example).
If the class label is 'Yes', it then compares the value of each attribute:
If the attribute value is equal to the current hypothesis value (or if the hypothesis is still ?), it updates the
hypothesis with the attribute value.
If the attribute value is different from the current hypothesis, the hypothesis for that attribute is set to ?,
meaning the current hypothesis cannot account for that condition.
Return Hypothesis: After iterating through all rows, the final hypothesis is returned.
Note : Download CSV file before running the code

Source Code:
import pandas as pd
def find_s_algorithm(file_path):
data = pd.read_csv(file_path)
print("Training data:")
print(data)
attributes = data.columns[:-1]
class_label = data.columns[-1]
hypothesis = ['?' for _ in attributes]
for index, row in data.iterrows():
if row[class_label] == 'Yes':
for i, value in enumerate(row[attributes]):

9
if hypothesis[i] == '?' or hypothesis[i] == value:

hypothesis[i] = value
else:
hypothesis[i] = '?'
return hypothesis
file_path = 'training_data.csv'
hypothesis = find_s_algorithm(file_path)
print("\nThe final hypothesis is:", hypothesis)

OUTPUT:

10

You might also like