From 4a1a3293fc5d9839823835a5c26cfff6fe91ca4e Mon Sep 17 00:00:00 2001 From: Cezary Adamczak Date: Thu, 3 Jun 2021 16:07:54 +0200 Subject: [PATCH] Zaktualizuj 'id3.py' --- id3.py | 65 ++++++++++++---------------------------------------------- 1 file changed, 13 insertions(+), 52 deletions(-) diff --git a/id3.py b/id3.py index d14314e..c554803 100644 --- a/id3.py +++ b/id3.py @@ -1,15 +1,10 @@ +from cases import * from collections import Counter import operator from types import prepare_class import numpy as np import copy -class Case: - def __init__(self, values, attributes, Class): - self.values = values - self.attributes = attributes - self.Class = Class - class Node: def __init__(self, Class, tag=None): self.Class = Class @@ -101,57 +96,23 @@ def treelearn(cases, attributes, default_class): return t def pretty_print(root, n): - for _ in range(n): - print("\t", end="") - print(root.Class) + if len(root.childs) == 0: + for _ in range(n): + print(" ", end="") + print("return " + str(root.Class)) for child in root.childs: for _ in range(n): - print("\t", end="") - print("== " + str(child[1])) + print(" ", end="") + if child != root.childs[0]: + print("el", end= "") + if len(str(child[1])) > 1: + print("if self." + str(root.Class) + " == \"" + str(child[1]) + "\":") + else: + print("if self." + str(root.Class) + " == " + str(child[1]) + ":") pretty_print(child[0], n+1) -attr = ["hydration", "fertility", "plant_type", "ticks", "is_healthy", "tractor_there"] -ccases = [] -k = Case([2, 0, "wheat", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([3, 0, "wheat", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([4, 0, "wheat", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([1, 1, "wheat", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([3, 0, "wheat", 20, 0, 0], attr, 0) -ccases.append(k) -k = Case([2, 0, "wheat", 20, 0, 0], attr, 0) -ccases.append(k) -k = Case([4, 0, "potato", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([3, 0, "potato", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([2, 0, "potato", 31, 0, 0], attr, 0) -ccases.append(k) -k = Case([2, 0, "potato", 31, 0, 0], attr, 0) -ccases.append(k) -k = Case([2, 1, "potato", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([1, 1, "potato", 31, 0, 0], attr, 0) -ccases.append(k) -k = Case([4, 1, "potato", 31, 0, 0], attr, 1) -ccases.append(k) -k = Case([4, 1, "potato", 19, 0, 0], attr, 0) -ccases.append(k) -k = Case([4, 1, "potato", 31, 1, 0], attr, 0) -ccases.append(k) -k = Case([4, 1, "wheat", 19, 0, 0], attr, 0) -ccases.append(k) -k = Case([4, 1, "potato", 31, 0, 1], attr, 0) -ccases.append(k) -k = Case([4, 1, "wheat", 31, 1, 0], attr, 0) -ccases.append(k) -k = Case([2, 0, "wheat", 31, 0, 1], attr, 0) -ccases.append(k) -tree = treelearn(ccases, attr, 0) +tree = treelearn(cases, attributes, 0) pretty_print(tree, 0)