From 31105416bd7fafb96925d022c027a7407f684fd3 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Fri, 12 Sep 2014 17:12:02 +0200 Subject: [PATCH] Decision Tree --- decisiontree.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 decisiontree.py diff --git a/decisiontree.py b/decisiontree.py new file mode 100644 index 0000000..5bf8e23 --- /dev/null +++ b/decisiontree.py @@ -0,0 +1,66 @@ +class Node(object): + pass + +def menu(prompt, decisions): + print("> %s" % prompt) + for i,decision in enumerate(decisions): + print("%d. %s" % (i,decision)) + answer = int(input("Answer: ")) + return answer + +class DecisionNode(Node): + def __init__(self, question): + self.question = question + self.decisions = [] + + def addDecision(self, response, node): + self.decisions.append( (response,node) ) + + def display(self): + i = menu(self.question, [ d[0] for d in self.decisions ]) + return self.decisions[i][1] + +class AnswerNode(Node): + def __init__(self, answer): + self.answer = answer + + def display(self): + print(self.answer) + return None + + +if __name__ == "__main__": + high = AnswerNode("hohes Risiko") + medium = AnswerNode("mittleres Risiko") + low = AnswerNode("niedriges Risiko") + + n6 = DecisionNode("Einkommen?") + n6.addDecision("0 Eur bis 15 Tsd Eur", high) + n6.addDecision("15 Tsd Eur bis 35 Tsd Eur", medium) + n6.addDecision("ueber 35 Tsd Eur", low) + + n4 = DecisionNode("Sicherheiten?") + n4.addDecision("keine", n6) + n4.addDecision("angemessen", low) + + n3 = DecisionNode("Verschuldung?") + n3.addDecision("hoch", n4) + n3.addDecision("niedrig", low) + + n2 = DecisionNode("Sicherheiten?") + n2.addDecision("keine", high) + n2.addDecision("angemessen", medium) + + n1 = DecisionNode("Verschuldung?") + n1.addDecision("hoch", high) + n1.addDecision("niedrig", n4) + + n0 = DecisionNode("Kreditwuerdigkeit?") + n0.addDecision("unbekannt",n1) + n0.addDecision("schlecht",n2) + n0.addDecision("gut",n3) + + node = n0 + + while node is not None: + node = node.display()