forked from ksanjeevan/randomforest-density-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
173 lines (100 loc) · 3.95 KB
/
node.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import numpy as np
from df_help import *
class Node(object):
"""
Class for each of the nodes in a decision tree.
"""
def __init__(self, data, quad, depth):
self.go_right = None
self.quad = quad
self.depth = depth
self.s_l = len(data)
self.left = None
self.right = None
def add_split(self, value, axis):
return lambda x: x[axis] > value
'''
def leaf_output(self, x):
"""
Evaluate the density estimation of that leaf on x.
"""
gauss_arg = np.inner(np.transpose((x-self.mu)), np.inner(self.inv_cov, (x-self.mu)))
return (np.exp(-.5*gauss_arg))/(2*np.pi*self.sqrt_cov)
'''
def check_norm(self, grid_axis):
"""
Verify leaf integrates to ~ 1.
"""
dist_vals = []
grid_axis_local = grid_axis.copy()
grid_axis_local[0] = grid_axis_local[0][self.quad[0][0]:self.quad[0][1]+1]
grid_axis_local[1] = grid_axis_local[1][self.quad[1][0]:self.quad[1][1]+1]
deltas = []
for v in grid_axis_local:
deltas.append( v[1]-v[0] )
for i, x in enumerate(grid_axis_local[0]):
dist_vals.append([])
for j, y in enumerate(grid_axis_local[1]):
dist_vals[i].append(self.leaf_output(np.array([x, y])))
integral = integrate_2d(deltas=deltas, func=dist_vals)
if not (integral > 0.95 and integral < 1.05):
print('Node of depth %s, norm = %s'%(self.depth, integral))
return integral
class NodeGauss(Node):
"""
Class for each of the nodes in a decision tree.
"""
def __init__(self, data, quad, depth, leaf=False):
super(NodeGauss, self).__init__(data=data, quad=quad, depth=depth)
self.leaf = leaf
if leaf:
self.cov = np.cov(data, rowvar=False)
# Check cov positive semidef
# np.all(np.linalg.eigvals(np.cov(data, rowvar=False)) > 0)
if np.all(np.linalg.eigvals(np.cov(data, rowvar=False)) > 0):
self.sqrt_cov = np.sqrt(np.linalg.det(self.cov))
self.inv_cov = np.linalg.inv(self.cov)
self.mu = np.mean(data, axis=0)
else:
self.sqrt_cov = 1
self.inv_cov = np.zeros(self.cov.shape)
self.mu = np.mean(data, axis=0)
def leaf_output(self, x):
"""
Evaluate the density estimation of that leaf on x.
"""
gauss_arg = np.inner(np.transpose((x-self.mu)), np.inner(self.inv_cov, (x-self.mu)))
return (np.exp(-.5*gauss_arg))/(2*np.pi*self.sqrt_cov)
def h_rot(x, d):
return math.pow(len(x), -(2.0)/(d+4))*np.var(x, axis=0) + 1e-8
class NodeKDE(Node):
"""
Class for each of the nodes in a decision tree.
"""
def __init__(self, data, quad, depth, leaf=False):
super(NodeKDE, self).__init__(data=data, quad=quad, depth=depth)
self.leaf = leaf
if leaf:
self.data = data
h = h_rot(data, len(data[0]))
self.H = [[h[0], 0],[0, h[1]]]
'''
print('-------------------')
print(data)
print(data.shape)
print(self.H)
'''
self.H_inv = np.linalg.inv(self.H)
self.H_inv_sqrt_det = np.sqrt(np.linalg.det(self.H_inv))
def k_gauss(self, u):
argum = np.sum(u*np.transpose(np.inner(self.H_inv, u)), axis=1)
return np.exp(-.5*argum)
def leaf_output(self, x):
result = self.H_inv_sqrt_det * np.sum(self.k_gauss(x - self.data)) * 1./(2*np.pi*len(self.data))
return result
if __name__ == "__main__":
H = np.array([[2,0],[0,2]])
x = np.array([[1,0],[0,1],[1,1],[0,-1],[-1,0]])
a = np.inner(np.transpose(x), np.inner(H, x))
print(np.inner(H, x).shape)
#np.inner(np.transpose(u), np.inner(self.H_inv, u))