-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
79 lines (69 loc) · 2 KB
/
node.py
File metadata and controls
79 lines (69 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sys
import random
import math
import copy
from collections import defaultdict
class Node:
def __init__(self):
self.attribute = None
self.children = None
self.label = None
self.distance = None #saving the distribution as a dictionary
self.threshold = None
self.attributeType = None
self.sameAttributes = 0
self.total = 0
def isLeaf(self):
if self.label != None:
return True
return False
def addLabel(self, label):
self.label = label
def setAttribute(self, attribute):
self.attribute = attribute
def setThreshold(self, threshold):
self.threshold = threshold
def setDistance(self, S, attributeType): #must add attribute before use
self.distance = {}
self.attributeType = attributeType
if attributeType == "Nominal":
for entry in S:
value = entry.attribute[self.attribute]
if value == "?":
continue
if value == self.attribute:
self.sameAttributes += 1
self.total += 1
if value in self.distance.keys():
self.distance[value] += 1
else:
self.distance[value] = 1
for value in self.distance.keys():
if self.total == 0:
break
self.distance[value] = self.distance[value]/self.total
elif attributeType == "Continuous":
self.distance[">"] = 0
self.distance["<="] = 0
for entry in S:
value = entry.attribute[self.attribute]
if value == "?":
continue
self.total+=1
if value > self.threshold:
self.distance[">"] +=1
else:
self.distance["<="] +=1
for value in self.distance.keys():
if self.total == 0:
break
self.distance[value] = self.distance[value]/self.total
def addChildren(self, values):
self.children = {}
for value in values:
self.children[value] = Node()
# Find the child for continuous functions because can't use the value as a key for the children.
def findChildContinuous(self, value):
if value <= self.threshold:
return self.children["<="]
return self.children[">"]