Candidate Elimination algorithm
Candidate Elimination algorithm by Aryadrj
If any error occurred .. then just comment below in comment box or contact me.
#Program->
import pandas as pd
import numpy as np
data=pd.DataFrame(data=pd.read_csv("arya.csv"))
concepts=np.array(data.iloc[:,0:-1])
target=np.array(data.iloc[:,-1])
def learn(concepts,target):
specific=concepts[0].copy()
general=[["?" for i in range(len(specific))] for i in range(len(specific))]
for i,h in enumerate(concepts):
if target[i]=="Yes":
for x in range(len(specific)):
if h[x]!=specific[x]:
specific[x]='?'
general[x][x]='?'
#print(specific[x])
if target[i]=="No":
for x in range(len(specific)):
if h[x]!=specific[x]:
general[x][x]=specific[x]
else:
general[x][x]="?"
indices=[i for i,val in enumerate(general) if val==['?','?','?','?','?','?']]
for i in indices:
general.remove(['?','?','?','?','?','?'])
return specific,general # return function can return from function not loop
s_final,g_final=learn(concepts,target)
print("final s",s_final,sep="\n")
print("final g",g_final,sep="\n")
Output->
Dataset->
Sky,Airtemp,Humidity,Wind,Water,Forecast,WaterSport
Sunny,Warm,Normal,Strong,Warm,Same,Yes
Sunny,Warm,High,Strong,Warm,Same,Yes
Cloudy,Cold,High,Strong,Warm,Change,No
Sunny,Warm,High,Strong,Cool,Change,Yes
If any error occurred .. then just comment below in comment box or contact me.
#Program->
import pandas as pd
import numpy as np
data=pd.DataFrame(data=pd.read_csv("arya.csv"))
concepts=np.array(data.iloc[:,0:-1])
target=np.array(data.iloc[:,-1])
def learn(concepts,target):
specific=concepts[0].copy()
general=[["?" for i in range(len(specific))] for i in range(len(specific))]
for i,h in enumerate(concepts):
if target[i]=="Yes":
for x in range(len(specific)):
if h[x]!=specific[x]:
specific[x]='?'
general[x][x]='?'
#print(specific[x])
if target[i]=="No":
for x in range(len(specific)):
if h[x]!=specific[x]:
general[x][x]=specific[x]
else:
general[x][x]="?"
indices=[i for i,val in enumerate(general) if val==['?','?','?','?','?','?']]
for i in indices:
general.remove(['?','?','?','?','?','?'])
return specific,general # return function can return from function not loop
s_final,g_final=learn(concepts,target)
print("final s",s_final,sep="\n")
print("final g",g_final,sep="\n")
Output->
final s ['Sunny' 'Warm' '?' 'Strong' '?' '?'] final g [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Dataset->
Sky,Airtemp,Humidity,Wind,Water,Forecast,WaterSport
Sunny,Warm,Normal,Strong,Warm,Same,Yes
Sunny,Warm,High,Strong,Warm,Same,Yes
Cloudy,Cold,High,Strong,Warm,Change,No
Sunny,Warm,High,Strong,Cool,Change,Yes
save the dataset as arya.csv
it showing error in last line
ReplyDeleteCan you please tell me what error msg you are getting?
Delete