2016-04-14 3 views
4

Ich versuche, einen "Kandidaten Beseitigung Algorithmus" in Python zu implementieren, aber mein Code funktioniert nicht.Mein Kandidat Beseitigung Algorithmus funktioniert nicht

Ich schrieb drei Funktionen:

  • consistent für die Konsistenz zwischen den Hypothesen überprüft und die Trainingsbeispiele
  • more_general
  • more_specific für die Suche nach den spezifischeren Argument
  • das allgemeinere Argument für die Suche nach

Aber mein Algorithmus fügt nicht hinzu oder entfernt die Hypothesen von G und S. Ich kann nicht finden, wo das Problem ist. Kannst du mir helfen?

# the general hypothesis 

G = [ ('?', '?', '?', '?') ] 

# the specific hypothesis 

S = [('0', '0', '0', '0')] 

# attributes: 
AV = (['short', 'far'], ['cheap', 'expensive'], ['many', 'none'], ['yes', 'no']) 

# training examples: 
D = [ 

    {'sample': ('far', 'cheap',  'many', 'no'), 'positive': True }, 
    {'sample': ('short', 'expensive', 'many', 'no'), 'positive': True }, 
    {'sample': ('far', 'expensive', 'none', 'yes'), 'positive': False}, 
    {'sample': ('short', 'cheap',  'none', 'yes'), 'positive': False}, 
    {'sample': ('short', 'cheap',  'many', 'yes'), 'positive': True } 
] 




def consistent(hypothesis, sample): 

    return all([hypothesis[i] == sample[i] or hypothesis[i] == '?' for i in 
    range(len(hypothesis))]) 


def more_general(a, b): 

    result = False 
    if a == '0' and b != '0': 
     result = True 
    elif a != '?' and b == '?': 
     result = True 

    return result 


def more_specific(a, b): 

    result = False 
    if a == '?' and b != '?': 
     result = True 
    elif a != '0' and b == '0': 
     result = True 

    return result 


for d in D: 

    if d['positive']: 
     G = [g for g in G if consistent(g, d['sample'])] 
     for s in S: 
      if not consistent(s, d['sample']): 
       S.remove(s) 

       # Adding to S all minimal generalizations of s by h: 
       dd = d['sample'] 
       if s == 0: 
        h = dd[s] 
       else: 
        h = '?' 


       if consistent(h, d['sample']) and any([more_general(g, h) for g in G]): 
        S.append(h) 

       #Removing from S any hypothesis that is more general than  another hypothesis in S 
       for s2 in S: 
        if any([more_general(s2, s3) and not s2 == s3 for s3 in S]): 
         S.remove(s2) 

    else: 
     S = [s for s in S if not consistent(s, d['sample'])] 
     for g in G: 
      if consistent(g, d['sample']): 
       G.remove(g) 

       # Add to G all minimal specializations h of g 
       for ai in range(len(AV)): 
        if g[ai] == '?': 
         h = list(g) 
         h[ai] = AV[ai][1 - AV[ai].index(d['sample'][ai])] 
         h = tuple(h) 
         if not consistent(h, d['sample']) and any([more_specific(s, h) for s in S]): 
          G.append(h) 




    print('Sample: {} {}\nG: {}\nS: {}\n'.format('+' if d['positive'] else '-', d['sample'], G, S)) 
+0

Nicht mutieren die Liste, die Sie iterieren über –

+0

@bernard, ich habe ein ähnliches Problem, hast du es gelöst? – Giordano

Antwort

0

Dies ist, wo Sie den Code für mich bricht:

 S.remove(s) 

     dd = d['sample'] 
     if s == 0: 
      h = dd[s] 
     else: 
      h = '?' 

     if consistent(h, d['sample']) and any([more_general(g, h) for g in G]): 
      S.append(h) 

s ein Tupel ist aber in der if Zustand, sind Sie es als Skalarwert Behandlung, wenn auf Null zu vergleichen. Nach der if, h enthält einen Skalar, aber in der nächsten if, der Aufruf an consistent() erwartet sein erstes Argument, h zu einem Tupel, kein Skalar sein.

+0

Ich sehe. Aber wie kann ich das beheben? Soll ich das h-Argument in die Prothese stellen? wie h = (dd [s])? – bernard

+0

Ich gehe davon aus, dass 'h' ein Tupel von vier Elementen sein muss, um an 'constant()' und 'more_general()' übergeben zu werden. Wenn Sie es auf "?" Setzen, ist in Ihrem Algorithmus etwas kaputt. Das heißt, es ist keine gültige Lösung, es nicht in Klammern zu verpacken. Überdenken Sie diesen Abschnitt des Codes. Füge Code-Kommentare hinzu. – cdlane

+0

danke für Ihre Hilfe. Aber kannst du mir erklären, warum es vier Elemente haben sollte? Ich bin neu in Python – bernard