Vikhyath_11

a3

Dec 12th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #program 3
  2. #Importing necessary libraries
  3. # import numpy as np
  4. import pandas as pd
  5. # Reading the dataset
  6. data = pd.read_csv('P3Data.csv')
  7. concepts = data.iloc[:, :-1].values
  8. target = data.iloc[:, -1].values
  9. print(concepts)
  10. print(target)
  11. # Candidate Elimination algorithm
  12. def learn(concepts, target):
  13. specific_h = concepts[0].copy() # Initialize specific hypothesis
  14. general_h = [["?" for _ in range(len(specific_h))] for _ in
  15. range(len(specific_h))]
  16. print("\nInitialization of specific_h and general_h")
  17. print("\nSpecific hypothesis: ", specific_h)
  18. print("\nGeneric hypothesis: \n", general_h)
  19. for i, h in enumerate(concepts):
  20. print("\nInstance", i+1, "is ", h)
  21. print("Instance is Positive")
  22. if target[i] == "yes": # Positive instance
  23. specific_h = ['?' if specific_h[x] != h[x] else
  24. specific_h[x] for x in range(len(specific_h))]
  25.  
  26. else: # Negative instance
  27. print("Instance is Negative")
  28. for x in range(len(specific_h)):
  29. if specific_h[x] != h[x]:
  30. general_h[x][x] = specific_h[x]
  31. print("Specific hypothesis after ", i+1, "Instance is ",
  32. specific_h)
  33. print("Generic hypothesis after ", i+1, "Instance is ",
  34. general_h)
  35. print("\n")
  36. # Remove redundant general hypotheses
  37. general_h = [gh for gh in general_h if gh != ['?' for _ in
  38. range(len(specific_h))]]
  39. return specific_h, general_h
  40. # Train and display results
  41. s_final, g_final = learn(concepts, target)
  42. print("Final Specific_h:\n", s_final)
  43. print("Final General_h:", g_final)
  44.  
Advertisement
Add Comment
Please, Sign In to add comment