0% found this document useful (0 votes)
12 views4 pages

MLT Practical 1 and 2

The document contains implementations of the FIND-S and Candidate Elimination algorithms for hypothesis learning using training data from a CSV file. The FIND-S algorithm identifies the most specific hypothesis based on positive examples, while the Candidate Elimination algorithm maintains both specific and general hypotheses based on positive and negative examples. Both algorithms print the hypotheses at various stages of processing the training data.

Uploaded by

shubhipandey25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

MLT Practical 1 and 2

The document contains implementations of the FIND-S and Candidate Elimination algorithms for hypothesis learning using training data from a CSV file. The FIND-S algorithm identifies the most specific hypothesis based on positive examples, while the Candidate Elimination algorithm maintains both specific and general hypotheses based on positive and negative examples. Both algorithms print the hypotheses at various stages of processing the training data.

Uploaded by

shubhipandey25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Implement and demonstrate the FIND-S algorithm to find the most specific hypothesis

import pandas as pd

def read_csv(enjoySport.csv):

data = pd.read_csv(enjoySport.csv)

return data

def find_s_algorithm(training_data):

attributes = training_data.iloc[:, :-1].values

labels = training_data.iloc[:, -1].values

hypothesis = ["?"] * len(attributes[0])

print("Initial Hypothesis:", hypothesis)

for i, label in enumerate(labels):

if label.lower() == "yes":

hypothesis = attributes[i].tolist()

print(f"Hypothesis after first positive example {i+1}: {hypothesis}")

break

for i, label in enumerate(labels):

if label.lower() == "yes":

for j in range(len(hypothesis)):

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

hypothesis[j] = "?"

print(f"Hypothesis after processing positive {i+1}: {hypothesis}")

return hypothesis

def main():

file_path = "training_data.csv"

training_data = read_csv(file_path)

print("Training Data:")

print(training_data)

hypothesis = find_s_algorithm(training_data)
print("\nFinal Most Specific Hypothesis:")

print(hypothesis)

if __name__ == "__main__":

main()
//Elimination algorithm for a given set of training data examples stored in a .CSV file.

import pandas as pd

import numpy as np

def read_csv(enjoysport.csv):

data = pd.read_csv(enjoysport.csv)

return data

def candidate_elimination_algorithm(training_data):

attributes = training_data.iloc[:, :-1].values

labels = training_data.iloc[:, -1].values

specific_hypothesis = attributes[0].copy()

general_hypothesis = ['?' for _ in range(len(specific_hypothesis))]

print(f"Initial Specific Hypothesis: {specific_hypothesis}")

print(f"Initial General Hypothesis: {general_hypothesis}\n")

for i, label in enumerate(labels):

if label.lower() == "yes":

for j in range(len(specific_hypothesis)):

if specific_hypothesis[j] != attributes[i][j]:

specific_hypothesis[j] = "?"

elif label.lower() == "no":

for j in range(len(specific_hypothesis)):

if specific_hypothesis[j] != attributes[i][j]:

general_hypothesis[j] = specific_hypothesis[j]

else:

general_hypothesis[j] = '?'

print(f"After training example {i+1}:")

print(f"Specific Hypothesis: {specific_hypothesis}")

print(f"General Hypothesis: {general_hypothesis}\n")

return specific_hypothesis, general_hypothesis


def main():

file_path = "training_data.csv"

training_data = read_csv(file_path)

print("Training Data:")

print(training_data)

specific_h, general_h = candidate_elimination_algorithm(training_data)

print("\nFinal Specific Hypothesis:")

print(specific_h)

print("\nFinal General Hypothesis:")

print(general_h)

if __name__ == "__main__":

main()

You might also like