Implementation of Find S algorithm using Python

Introduction :

Find-S algorithm is a basic concept learning algorithm in machine learning. Find-S algorithm finds the most specific hypothesis that fits all the positive examples. We have to note here that the algorithm considers only those positive training example. Find-S algorithm starts with the most specific hypothesis and generalizes this hypothesis each time it fails to classify an observed positive training data. Hence, Find-S algorithm moves from the most specific hypothesis to the most general hypothesis.



data.csv




#Find S by Vikas Konaparthi


import random

import csv



attributes = [['Sunny','Rainy'],

              ['Warm','Cold'],

              ['Normal','High'],

              ['Strong','Weak'],

              ['Warm','Cool'],

              ['Same','Change']]



num_attributes = len(attributes)



print (" \n The most general hypothesis : ['?','?','?','?','?','?']\n")

print ("\n The most specific hypothesis : ['0','0','0','0','0','0']\n")


a = []

print("\n The Given Training Data Set \n")


with open('data.csv', 'r') as csvFile:

    reader = csv.reader(csvFile)

    for row in reader:

        a.append (row)

        print(row)



print("\n The initial value of hypothesis: ")

hypothesis = ['0'] * num_attributes

print(hypothesis)


# Comparing with First Training Example 

for j in range(0,num_attributes):

        hypothesis[j] = a[0][j];


# Comparing with Remaining Training Examples of Given Data Set


print("\n Find S by VK\n")


for i in range(0,len(a)):

    if a[i][num_attributes]=='Yes':

            for j in range(0,num_attributes):

                if a[i][j]!=hypothesis[j]:

                    hypothesis[j]='?'

                else :

                    hypothesis[j]= a[i][j] 

    print(" For Training Example No :{0} the hypothesis is ".format(i),hypothesis)

                

print("\n The Maximally Specific Hypothesis for a given Training Examples :\n")

print(hypothesis)

Output:


The most general hypothesis : ['?','?','?','?','?','?']                                                                        
The most specific hypothesis : ['0','0','0','0','0','0']                                                      
The Given Training Data Set                                                                            
['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 initial value of hypothesis:                                                                                               
['0', '0', '0', '0', '0', '0']                                                                                      
Find S by VK                                                                                                                  
For Training Example No :0 the hypothesis is  ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']                            
For Training Example No :1 the hypothesis is  ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']                                 
For Training Example No :2 the hypothesis is  ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']                                 
For Training Example No :3 the hypothesis is  ['Sunny', 'Warm', '?', 'Strong', '?', '?']                                       
                                                
The Maximally Specific Hypothesis for a given Training Examples :                                                        
['Sunny', 'Warm', '?', 'Strong', '?', '?']                 

RECOMMENDED: PLEASE SOLVE IT ON “PRACTICE ”.

Previous Post Next Post