def id3(data): if all(x['analito'] == 'glicose' for x in data): return Leaf('glicose') if all(x['analito'] == 'triglicerideo' for x in data): return Leaf('triglicerideo') if len([k for k in data[0].keys() if k != 'analito']) <= 0: return Leaf(most_common([d['analito'] for d in data])) # Entropia dos atributos splited = {k: entropy([x[k] for x in data.copy()]) for k in data[0].keys()} splited.pop('analito') # Atributo separador separator = min(splited.items(), key=lambda x: x[1])[0] node = Node(separator) if is_continuous(data[0][separator]): for x in data: x[separator] = round(x[separator], PRECISION) for v in set(x[separator] for x in data): aux = [x for x in data.copy() if separator in x and x[separator] == v] for i in aux: i.pop(separator) node.add_son({v: id3(aux)}) return node
def predict(element, tree): if type(tree) is Leaf: return tree.answer if is_continuous(element[tree.attr]): return predict(element, tree.sons[nearest(element[tree.attr], list(tree.sons.keys()), precision=PRECISION)]) return predict(element, tree.sons[element[tree.attr]])