EXP 4-Find S
EXP 4-Find S
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