Candidate elimination
def candidate_elimination(data):
G = [["?"] * (len(data[0]) - 1)] # General hypothesis
S = ["0"] * (len(data[0]) - 1) # Specific hypothesis
for instance in data:
x, y = instance[:-1], instance[-1]
if y == "Yes": # Positive example
G = [g for g in G if all(g[i] == "?" or g[i] == x[i] for i in range(len(x)))]
S = [x[i] if s == "0" else s if s == x[i] else "?" for i, s in enumerate(S)]
else: # Negative example
G = [g for g in G for i in range(len(x)) if g[i] == "?" or g[i] != x[i]]
return S, G
# Dataset: [Attributes..., Class]
data = [["Sunny", "Warm", "Normal", "Strong", "Warm", "Same", "Yes"],
["Sunny", "Warm", "High", "Strong", "Warm", "Same", "Yes"],
["Rainy", "Cold", "High", "Strong", "Warm", "Change", "No"],
["Sunny", "Warm", "High", "Strong", "Cool", "Change", "Yes"]]
S, G = candidate_elimination(data)
print("Specific Hypothesis:", S)
print("General Hypotheses:", G)
Comments
Post a Comment