ML 1 – FIND-S ALGORITHM

  ML LAB
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.


SOLUTION  1 – To display only the final output

trainingdata.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


prog1.py


import csv

h=[‘0’for i in range(6)]
with open(“trainingdata.csv”) as f:
    data=csv.reader(f)
    data=list(data)
    
    for i in data:
        if i[-1]==”Yes”:
            for j in range(6):
                if h[j]==’0′:
                    h[j]=i[j]
                elif h[j]!=i[j]:
                    h[j]=’?’

    print(h)

Output

[‘Sunny’, ‘Warm’, ‘?’, ‘Strong’, ‘?’, ‘?’]

OR

SOLUTION  1 – To display steps & final output

import csv

h=[‘0’for i in range(6)]
with open(“trainingdata.csv”) as f:
    data=csv.reader(f)
    data=list(data)
    
    print(“The +ve examples are:”)
    for i in data:
        if i[-1]==”Yes”:
            print(i)
     
    print(“nThe steps of Find-S Algo are:”)
    for i in data:
        if i[-1]==”Yes”:
            for j in range(6):
                if h[j]==’0′:
                    h[j]=i[j]
                elif h[j]!=i[j]:
                    h[j]=’?’
                    print(h)

    print(“nFinal specific hypothesis:n”,h)

Output

The +ve examples are:
[‘Sunny’, ‘Warm’, ‘Normal’, ‘Strong’, ‘Warm’, ‘Same’, ‘Yes’]
[‘Sunny’, ‘Warm’, ‘High’, ‘Strong’, ‘Warm’, ‘Same’, ‘Yes’]
[‘Sunny’, ‘Warm’, ‘High’, ‘Strong’, ‘Cool’, ‘Change’, ‘Yes’]

The steps of Find-S Algo are:
[‘Sunny’, ‘Warm’, ‘?’, ‘Strong’, ‘Warm’, ‘Same’]
[‘Sunny’, ‘Warm’, ‘?’, ‘Strong’, ‘Warm’, ‘Same’]
[‘Sunny’, ‘Warm’, ‘?’, ‘Strong’, ‘?’, ‘Same’]
[‘Sunny’, ‘Warm’, ‘?’, ‘Strong’, ‘?’, ‘?’]

Final specific hypothesis:
 [‘Sunny’, ‘Warm’, ‘?’, ‘Strong’, ‘?’, ‘?’]

SOLUTION  2 

trainingdata.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

lab1.py

import csv
hypo = [‘%’,’%’,’%’,’%’,’%’,’%’];

with open(‘trainingdata.csv’) as csv_file:
    readcsv = csv.reader(csv_file, delimiter=’,’)
    print(readcsv)

    data = []
    print(“nThe given training examples are:”)
    for row in readcsv:
        print(row)
        if row[len(row)-1].upper() == “YES”:
            data.append(row)

print(“nThe positive examples are:”);
for x in data:
    print(x);
print(“n”);

TotalExamples = len(data);
i=0;
j=0;
k=0;
print(“The steps of the Find-s algorithm are :n”,hypo);
list = [];
p=0;
d=len(data[p])-1;
for j in range(d):
    list.append(data[i][j]);
hypo=list;
i=1;
for i in range(TotalExamples):
    for k in range(d):
        if hypo[k]!=data[i][k]:
            hypo[k]=’?’;
            k=k+1;   
        else:
            hypo[k];
    print(hypo);
i=i+1;

print(“nThe maximally specific Find-s hypothesis for the given training examples is :”);
list=[];
for i in range(d):
    list.append(hypo[i]);
print(list);

STEPS & OUTPUT:

to view steps & output click HERE

LEAVE A COMMENT