B.tech 5th Sem Machin learning lab practical
Program 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 random
import csv
import pandas as pd
att= [['Sunny','Rainy'],
['Warm','Cold'],
['Normal','High'],
['Strong','Weak'],
['Warm','Cool'],
['Same','Change']]
attribute=len(att)
print("attribute length=",attribute)
print("\n Most general hypothesis:['?','?','?','?,'?','?']")
print("\n Most specific hypothesis:['0','0','0','0','0','0']")
a=[]
print("\n Given training data set")
with open('C:\\Users\pc1\\Desktop\\machine\\ws.csv') as file:
reader=csv.reader(file)
for r in reader:
a.append(r)
print(r)
print("\n The initial value of hypothesis: ")
hypothesis = ['0'] * attribute
print(hypothesis)
# Comparing with First Training Example
for j in range(0,attribute):
hypothesis[j] = a[0][j];
print(hypothesis[j])
# Comparing with Remaining Training Examples of Given Data Set
print("\n Find S: Finding a Maximally Specific Hypothesis\n")
b=len(a)
print("\n length of a=",b)
for i in range(0,len(a)):
if a[i][attribute]=='Yes':
for j in range(0,attribute):
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")
Output:-
attribute length= 6
Most general hypothesis:['?','?','?','?,'?','?'] Most specific hypothesis:['0','0','0','0','0','0']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']Sunny Warm Normal Strong Warm SameFind S: Finding a Maximally Specific Hypothesis length of a= 4 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 :Program 2: For a given set of training data examples stored in a .CSV file, implement and demonstrate the Candidate-Elimination algorithm to output a description on of the set of all hypotheses consistent with the training examples.import numpy as np import pandas as pd# Loading Data from a CSV File d=pd.read_csv(r"C:\Users\pc1\Desktop\machine\trainingdata.csv") data = pd.DataFrame(d) print(d)# Separating concept features from Target concepts = np.array(data.iloc[:,0:-1]) print(concepts)# Isolating target into a separate DataFrame # copying last column to target array target = np.array(data.iloc[:,-1]) print(target)def learn(concepts, target): ''' learn() function implements the learning method of the Candidate elimination algorithm. Arguments: concepts - a data frame with all the features target - a data frame with corresponding output values ''' # Initialise S0 with the first instance from concepts # .copy() makes sure a new list is created instead of just pointing to the same memory location specific_h = concepts[0].copy() print("\nInitialization of specific_h and general_h") print(specific_h) #h=["#" for i in range(0,5)] #print(h) general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_h))] print(general_h) # The learning iterations for i, h in enumerate(concepts): # Checking if the hypothesis has a positive target if target[i] == "Yes": for x in range(len(specific_h)): # Change values in S & G only if values change if h[x] != specific_h[x]: specific_h[x] = '?' general_h[x][x] = '?' # Checking if the hypothesis has a positive target if target[i] == "No": for x in range(len(specific_h)): # For negative hyposthesis change values only in G if h[x] != specific_h[x]: general_h[x][x] = specific_h[x] else: general_h[x][x] = '?' print("\nSteps of Candidate Elimination Algorithm",i+1) print(specific_h) print(general_h) # find indices where we have empty rows, meaning those that are unchanged indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']] for i in indices: # remove those rows from general_h general_h.remove(['?', '?', '?', '?', '?', '?']) # Return final values return specific_h, general_hs_final, g_final = learn(concepts, target) print("\nFinal Specific_h:", s_final, sep="\n") print("\nFinal General_h:", g_final, sep="\n")Output:sky airTemp humidity wind water forecast enjoySport 0 Sunny Warm Normal Strong Warm Same Yes 1 Sunny Warm High Strong Warm Same Yes 2 Rainy Cold High Strong Warm Change No 3 Sunny Warm High Strong Cool Change Yes[['Sunny' 'Warm' 'Normal' 'Strong' 'Warm' 'Same'] ['Sunny' 'Warm' 'High' 'Strong' 'Warm' 'Same'] ['Rainy' 'Cold' 'High' 'Strong' 'Warm' 'Change'] ['Sunny' 'Warm' 'High' 'Strong' 'Cool' 'Change']]['Yes' 'Yes' 'No' 'Yes']Initialization of specific_h and general_h ['Sunny' 'Warm' 'Normal' 'Strong' 'Warm' 'Same'] [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']] Steps of Candidate Elimination Algorithm 1 ['Sunny' 'Warm' 'Normal' 'Strong' 'Warm' 'Same'] [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']] Steps of Candidate Elimination Algorithm 2 ['Sunny' 'Warm' '?' 'Strong' 'Warm' 'Same'] [['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']] Steps of Candidate Elimination Algorithm 3 ['Sunny' 'Warm' '?' 'Strong' 'Warm' 'Same'] [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', 'Same']] Steps of Candidate Elimination Algorithm 4 ['Sunny' 'Warm' '?' 'Strong' '?' '?'] [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']] Final Specific_h: ['Sunny' 'Warm' '?' 'Strong' '?' '?'] Final General_h: [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?']]
Comments
Post a Comment