master #1

Merged
s444427 merged 39 commits from s452664/Sztuczna_Inteligencja-projekt:master into master 2021-06-03 16:39:57 +02:00
Showing only changes of commit 4a1a3293fc - Show all commits

63
id3.py
View File

@ -1,15 +1,10 @@
from cases import *
from collections import Counter from collections import Counter
import operator import operator
from types import prepare_class from types import prepare_class
import numpy as np import numpy as np
import copy import copy
class Case:
def __init__(self, values, attributes, Class):
self.values = values
self.attributes = attributes
self.Class = Class
class Node: class Node:
def __init__(self, Class, tag=None): def __init__(self, Class, tag=None):
self.Class = Class self.Class = Class
@ -101,57 +96,23 @@ def treelearn(cases, attributes, default_class):
return t return t
def pretty_print(root, n): def pretty_print(root, n):
if len(root.childs) == 0:
for _ in range(n): for _ in range(n):
print("\t", end="") print(" ", end="")
print(root.Class) print("return " + str(root.Class))
for child in root.childs: for child in root.childs:
for _ in range(n): for _ in range(n):
print("\t", end="") print(" ", end="")
print("== " + str(child[1])) 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) 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) pretty_print(tree, 0)