Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #program 3
- #Importing necessary libraries
- # import numpy as np
- import pandas as pd
- # Reading the dataset
- data = pd.read_csv('P3Data.csv')
- concepts = data.iloc[:, :-1].values
- target = data.iloc[:, -1].values
- print(concepts)
- print(target)
- # Candidate Elimination algorithm
- def learn(concepts, target):
- specific_h = concepts[0].copy() # Initialize specific hypothesis
- general_h = [["?" for _ in range(len(specific_h))] for _ in
- range(len(specific_h))]
- print("\nInitialization of specific_h and general_h")
- print("\nSpecific hypothesis: ", specific_h)
- print("\nGeneric hypothesis: \n", general_h)
- for i, h in enumerate(concepts):
- print("\nInstance", i+1, "is ", h)
- print("Instance is Positive")
- if target[i] == "yes": # Positive instance
- specific_h = ['?' if specific_h[x] != h[x] else
- specific_h[x] for x in range(len(specific_h))]
- else: # Negative instance
- print("Instance is Negative")
- for x in range(len(specific_h)):
- if specific_h[x] != h[x]:
- general_h[x][x] = specific_h[x]
- print("Specific hypothesis after ", i+1, "Instance is ",
- specific_h)
- print("Generic hypothesis after ", i+1, "Instance is ",
- general_h)
- print("\n")
- # Remove redundant general hypotheses
- general_h = [gh for gh in general_h if gh != ['?' for _ in
- range(len(specific_h))]]
- return specific_h, general_h
- # Train and display results
- s_final, g_final = learn(concepts, target)
- print("Final Specific_h:\n", s_final)
- print("Final General_h:", g_final)
Advertisement
Add Comment
Please, Sign In to add comment