Advertisement
yesh666

CE

Mar 26th, 2023
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3.  
  4. data = pd.read_csv('data.csv')
  5. print(data)
  6. concepts = np.array(data.iloc[:,0:-1])
  7. print(concepts)
  8. target = np.array(data.iloc[:,-1])
  9. print(target)
  10.  
  11. def learn(concepts, target):
  12.     specific_h = concepts[0].copy()
  13.     print("\nInitialization of specific_h and genearal_h")
  14.     print( specific_h)
  15.     general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_h))]
  16.     print(general_h)  
  17.  
  18.     for i, h in enumerate(concepts):
  19.         if target[i] == "yes":
  20.             for x in range(len(specific_h)):
  21.                 if h[x]!= specific_h[x]:                    
  22.                     specific_h[x] ='?'                    
  23.                     general_h[x][x] ='?'
  24.                    
  25.         if target[i] == "no":            
  26.             for x in range(len(specific_h)):
  27.                 if h[x]!= specific_h[x]:                    
  28.                     general_h[x][x] = specific_h[x]                
  29.                 else:                    
  30.                     general_h[x][x] = '?'  
  31.                    
  32.         print("\n steps of candidate eliminaton algorithm",i+1)
  33.         print( specific_h)        
  34.         print( general_h)
  35.  
  36.  
  37.     indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]    
  38.     for i in indices:  
  39.         general_h.remove(['?', '?', '?', '?', '?', '?'])
  40.     return specific_h, general_h
  41.  
  42. s_final, g_final = learn(concepts, target)
  43.  
  44. print("Final Specific_h: ", s_final, sep="\n")
  45. print("Final General_h: ", g_final, sep="\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement