FIND-S Algorithm Demonstration
FIND-S Algorithm (Most Specific Hypothesis)
Algorithm:
1. Initialize the most specific hypothesis h to the first positive example in the data.
2. For each training example x:
- If x is a positive example:
- For each attribute a_i in x, compare it with the current hypothesis h_i:
- If they are the same, do nothing.
- If they are different, replace h_i with ?.
3. Return the final hypothesis h.
Sample CSV Format (training_data.csv):
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
Python Program (FIND-S):
import csv
def read_data(filename):
with open(filename, 'r') as file:
data = list(csv.reader(file))
header = data[0]
examples = data[1:]
return header, examples
def find_s_algorithm(examples):
# Find the first positive example
for example in examples:
if example[-1] == 'Yes':
hypothesis = example[:-1]
break
# Generalize hypothesis with other positive examples
for example in examples:
if example[-1] == 'Yes':
for i in range(len(hypothesis)):
if hypothesis[i] != example[i]:
hypothesis[i] = '?'
return hypothesis
if __name__ == "__main__":
header, examples = read_data('training_data.csv')
final_hypothesis = find_s_algorithm(examples)
print("Final Hypothesis:")
print(['<{}>'.format(', '.join(final_hypothesis))])
Sample Output:
Final Hypothesis:
['<Sunny, Warm, ?, Strong, ?, ?>']
Summary:
- The FIND-S algorithm finds the most specific hypothesis consistent with positive examples only.
- It ignores negative examples completely.
- This demo uses a CSV with attributes and a Yes/No label at the end.
- The hypothesis starts specific and generalizes only when needed.