Sunteți pe pagina 1din 2

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
attributes = [ ['Sunny', 'Cloudy', 'Rainy'],
['Warm', 'Cold'],
['Normal', 'High'],
['Strong', 'Weak'],
['Warm', 'Cool'],
['Same', 'Change']]
total_attributes = len(attributes)
print("\nTotal number of attributes is: ", total_attributes)
print ("The most specific hypothesis: ['0','0','0','0','0','0']")
print ("The most general hypothesis: ['?','?','?','?','?','?']")

a=[]
print("\nThe Given Training Data Set is:")
with open('T:\\ML\\datasheet\\EnjoySport.csv', 'r') as cfile:
for row in csv.reader(cfile):
a.append(row)
print(row)

print("\nTotal number of records is: ", len(a))


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

#Comparing with Training Examples of Given Data Set


for i in range(0, len(a)):
if a[i][total_attributes] == 'Yes':
for j in range(0, total_attributes):
if hypothesis[j] == '0' or hypothesis[j] == a[i][j]:
hypothesis[j] = a[i][j]
else:
hypothesis[j] = '?'
print("\nHypothesis for Training Example No {} is: \n".format(i+1), hypothesis)
print("\nThe Maximally Specific Hypothesis for a given Training Examples :")
print(hypothesis)
----------------------------------------------------------------------------------------------------------------------------------

Input File: EnjoySport.CSV

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

Output:

Total number of attributes is: 6


The most specific hypothesis: ['0','0','0','0','0','0']
The most general hypothesis: ['?','?','?','?','?','?']

The Given Training Data Set is:


['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']

Total number of records is: 4

The initial hypothesis is:


['0', '0', '0', '0', '0', '0']

Hypothesis for Training Example No 1 is:


['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']

Hypothesis for Training Example No 2 is:


['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']

Hypothesis for Training Example No 3 is:


['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']

Hypothesis for Training Example No 4 is:


['Sunny', 'Warm', '?', 'Strong', '?', '?']

The Maximally Specific Hypothesis for a given Training Examples :


['Sunny', 'Warm', '?', 'Strong', '?', '?']

S-ar putea să vă placă și