Zaktualizuj 'id3.py'

This commit is contained in:
Cezary Adamczak 2021-06-03 16:07:54 +02:00
parent b8110ebf36
commit 4a1a3293fc

63
id3.py
View File

@ -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):
if len(root.childs) == 0:
for _ in range(n):
print("\t", end="")
print(root.Class)
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)