FIND S Algorithm in Python

 
Video Tutorials
Artificial Intelligence Tutorial
Machine Learning Projects TutorialPython Video tutorial
Computer Graphics Video TutorialC++ Video Tutorial
Big Data Analytics Video TutorialSoft Computing Video Tutorial
Placement Video TutorialJava Video Tutorial

Python Program to Implement FIND S Algorithm – to get Maximally Specific Hypothesis

Exp. No. 1. Implement and demonstrate the FIND-S algorithm in Python for finding the most specific hypothesis based on a given set of training data samples. Read the training data from a .CSV file

Find-S Algorithm Machine Learning

1. Initilize h to the most specific hypothesis in H
2. For each positive training instance x
      For each attribute contraint ai in h
          If the contraint ai is satisfied by x
              then do nothing
          Else
              replace ai in h by the next more general constraint that is satisfied by x
3. Output the hypothesis h

Python Program to Implement and Demonstrate FIND-S Algorithm

"""
1. Implement and demonstrate the FIND-S algorithm for finding the  
most specific hypothesis based on a given set of training data samples. 
Read the training data from a .CSV file
"""
import csv
a = []
with open('enjoysport.csv', 'r') as csvfile:
    next(csvfile)
    for row in csv.reader(csvfile):
        a.append(row)
    print(a)

print("\nThe total number of training instances are : ",len(a))

num_attribute = len(a[0])-1

print("\nThe initial hypothesis is : ")
hypothesis = ['0']*num_attribute
print(hypothesis)

for i in range(0, len(a)):
    if a[i][num_attribute] == 'yes':
        print ("\nInstance ", i+1, "is", a[i], " and is Positive Instance")
        for j in range(0, num_attribute):
            if hypothesis[j] == '0' or hypothesis[j] == a[i][j]:
                hypothesis[j] = a[i][j]
            else:
                hypothesis[j] = '?'
        print("The hypothesis for the training instance", i+1, " is: " , hypothesis, "\n")

    if a[i][num_attribute] == 'no':
        print ("\nInstance ", i+1, "is", a[i], " and is Negative Instance Hence Ignored")
        print("The hypothesis for the training instance", i+1, " is: " , hypothesis, "\n")

print("\nThe Maximally specific hypothesis for the training instance is ", hypothesis)

Output:

EnjoySport Dataset is saved as .csv (comma separated values) file in the current working directory otherwise use the complete path of the dataset set in the program:

skyairtemphumiditywindwaterforcastenjoysport
sunnywarmnormalstrongwarmsameyes
sunnywarmhighstrongwarmsameyes
rainycoldhighstrongwarmchangeno
sunnywarmhighstrongcoolchangeyes

[[‘sky’, ‘airtemp’, ‘humidity’, ‘wind’, ‘water’, ‘forcast’, ‘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’]]

The total number of training instances are : 5

The initial hypothesis is : [‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’]

Instance 2 is [‘sunny’, ‘warm’, ‘normal’, ‘strong’, ‘warm’, ‘same’, ‘yes’] and is Positive Instance

The hypothesis for the training instance 2 is: [‘sunny’, ‘warm’, ‘normal’, ‘strong’, ‘warm’, ‘same’]

Instance 3 is [‘sunny’, ‘warm’, ‘high’, ‘strong’, ‘warm’, ‘same’, ‘yes’] and is Positive Instance

The hypothesis for the training instance 3 is: [‘sunny’, ‘warm’, ‘?’, ‘strong’, ‘warm’, ‘same’]

Instance 4 is [‘rainy’, ‘cold’, ‘high’, ‘strong’, ‘warm’, ‘change’, ‘no’] and is Negative Instance Hence Ignored

The hypothesis for the training instance 4 is: [‘sunny’, ‘warm’, ‘?’, ‘strong’, ‘warm’, ‘same’]

Instance 5 is [‘sunny’, ‘warm’, ‘high’, ‘strong’, ‘cool’, ‘change’, ‘yes’] and is Positive Instance

The hypothesis for the training instance 5 is: [‘sunny’, ‘warm’, ‘?’, ‘strong’, ‘?’, ‘?’]

The Maximally specific hypothesis for the training instance is [‘sunny’, ‘warm’, ‘?’, ‘strong’, ‘?’, ‘?’]

Solved Numerical Example – Find S Algorithm to Find the Most Specific Hypothesis

Summary

This tutorial discusses how to Implement and demonstrate the FIND-S algorithm in Python for finding the most specific hypothesis based on a given set of training data samples. The training data is read from a .CSV file. If you like the tutorial share with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *