[Python] Candidate Elimination Algorithm Implementation Not Working as Expected

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to implement the Candidate Elimination Algorithm to learn a concept from a dataset. However, my code is not producing the expected results. Below is my implementation and the dataset I am using.

    My Code:

    import pandas as pd
    import numpy as np
    import os, sys

    df = pd.read_csv("chumma.csv")

    val_dict = {
    "0":["Some","Many"],
    "1":['Small','Big','Medium'],
    '2':['No'],
    '3':['Affordable','Expensive'],
    '4':['One','Many','Few']
    }

    X = df.iloc[:,:-1].values
    Y = df.iloc[:,-1].values

    S = [['$']*5]
    for i in range(len(Y)):
    if Y == ' Yes':
    S = X.copy()
    break

    G = [["?"]*len(S)]

    def is_consistent(x_data, y_data, g_hyp):
    out_hyp = []
    for hyp in g_hyp:
    is_valid = True
    for i in range(len(x_data)):
    matches_hyp = True
    for j in range(len(hyp)):
    if hyp[j]!="?" and hyp[j]!=x_data[j]:
    matches_hyp = False
    break
    if (y_data == ' Yes' and not matches_hyp) or (y_data == ' No' and matches_hyp):
    is_valid = False
    break

    if is_valid and hyp not in out_hyp:
    out_hyp.append(hyp)

    return out_hyp


    for i,instance in enumerate(X):
    if Y == ' Yes':
    for j in range(len(S)):
    if S[j]!=instance[j]:
    S[j] = "?"
    else:
    g_hyp = []
    for g in G:
    for j in range(len(g)):
    if g[j] == "?":
    d_vals = val_dict[str(j)]
    for k in range(0,len(d_vals)):
    if d_vals[k] != instance[j]:
    new_g = g.copy()
    new_g[j] = d_vals[k]
    print(new_g)
    g_hyp.append(new_g)
    print()
    break
    G = is_consistent(X[:i+1], Y[:i+1], g_hyp)


    print("Final Specific Hypothesis:",S)
    print("Final General Hypothesis:",G)


    Dataset (chumma.csv):

    Citations, Size, InLibrary, Price, Editions, Buy
    Some, Small, No, Affordable, One, No
    Many, Big, No, Expensive, Many, Yes
    Many, Medium, No, Expensive, Few, Yes
    Some, Small, No, Affordable, One, No
    Many, Big, No, Expensive, Many, Yes
    Many, Medium, No, Expensive, Few, Yes


    Expected Output The algorithm should output the specific hypothesis (S) and general hypothesis (G) that represent the concept learned from the dataset

    Issue The code does not seem to correctly update the specific hypothesis (S) and general hypothesis (G). Specifically:

    • S is not being generalized correctly for positive examples.
    • G is not being specialized correctly for negative examples.

    What is wrong with my implementation of the Candidate Elimination Algorithm and how can I fix the code to correctly update S and G?

    Continue reading...

Compartilhe esta Página