4. 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.
import pandas as pd
def find_s_algorithm(file_path):
We are creating a function named find_s_algorithm.
It takes file_path as input (the CSV file containing training data).
data = pd.read_csv(file_path)
We use pandas (pd) to read the dataset.
The CSV file is loaded into a table format called a DataFrame.
Now data contains all training examples.
print("Training data:")
print(data)
Displays the dataset so we can see the input examples.
attributes = [Link][:-1]
class_label = [Link][-1]
[Link] gives all column names.
[:-1] → selects all columns except the last one (these are input attributes).
[-1] → selects the last column (this is the target/class label).
Example:
Sky Temp Humidity Wind Play
Attributes → Sky, Temp, Humidity, Wind
Class label → Play
hypothesis = ['?' for _ in attributes]
We create an initial hypothesis.
? means most general value.
If there are 4 attributes → hypothesis = ['?', '?', '?', '?']
This means:
"We don’t know anything yet."
for index, row in [Link]():
iterrows() goes row by row.
row contains one training example at a time.
if row[class_label] == 'Yes':
Find-S works only on positive examples.
If class label is Yes, we update hypothesis.
If class label is No, we ignore it.
for i, value in enumerate(row[attributes]):
We compare each attribute value with the current hypothesis.
if hypothesis[i] == '?' or hypothesis[i] == value:
hypothesis[i] = value
else:
hypothesis[i] = '?'
Three cases:
Case 1️⃣: Hypothesis is '?'
Replace it with the current value.
Example:
Hypothesis: ['?', '?', '?']
First positive example: Sunny, Warm, Normal
New hypothesis: ['Sunny', 'Warm', 'Normal']
Case 2️⃣: Same Value
Keep it as it is.
Example:
Hypothesis: ['Sunny', 'Warm', 'Normal']
Next example: Sunny, Warm, High
First two match → keep them.
Case 3️⃣: Different Value
Replace with ?
Example:
Hypothesis: ['Sunny', 'Warm', 'Normal']
Next example: Rainy, Warm, Normal
First attribute different → change to ?
New hypothesis: ['?', 'Warm', 'Normal']
return hypothesis
After checking all positive examples, we return the final hypothesis.
file_path = 'training_data.csv'
hypothesis = find_s_algorithm(file_path)
We give the CSV file path.
The function runs and calculates the hypothesis.
print("\nThe final hypothesis is:", hypothesis)
Displays the final generalized rule.