-
Notifications
You must be signed in to change notification settings - Fork 7
/
density_forest.py
310 lines (193 loc) · 8.01 KB
/
density_forest.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
from df_help import *
from grid import Grid
from tree import Tree
from node import NodeGauss, NodeKDE
import argparse
from pylab import *
argparser = argparse.ArgumentParser(
description='randomforest-density: density estimation using random forests.')
argparser.add_argument(
'-l',
'--leaf',
help='Choose what leaf estimator to use'
' (Gaussian [\'gauss\'] or KDE [\'kde\'])',
default='gauss')
argparser.add_argument(
'-d',
'--data',
help='Path to data (.npy file, shape (sample_zie, 2)).',
default='')
argparser.add_argument(
'-g',
'--granularity',
help='Number of division for the Grid',
default=100)
args = argparser.parse_args()
assert args.leaf in ['kde', 'gauss'], "Pass valid leaf estimator"
LEAF_DICT = {
'kde': NodeKDE,
'gauss': NodeGauss
}
LEAF_TYPE = args.leaf
MODE = 'est' if args.data else 'demo'
DATA_PATH = args.data
DIVS = args.granularity
def gauss_entropy_func(S):
"""
Gaussian differential entropy (ignoring constant terms since
we're interested in the delta).
"""
return np.log(np.linalg.det(np.cov(S, rowvar=False)) + 1e-8)
class DensityForest:
"""
Class for density forest, compute entropy threshold for stop condition, then build and
train forest of trees with randomness rho.
"""
def __init__(self, data, grid_obj, f_size, rho=1.):
self.data = data
self.f_size = f_size
self.node_class = NodeGauss
self.entropy_func = gauss_entropy_func
self.grid_obj = grid_obj
self.grid = self.grid_obj.axis
self.rho = rho
def train(self):
self.opt_entropy = self.tune_entropy_threshold(plot_debug=True)
self.forest = self.build_forest()
def estimate(self):
self.dist = self.compute_density()
return self.dist
def compute_density(self):
dist = []
for j, y in enumerate(self.grid[1]):
dist.append([])
for i, x in enumerate(self.grid[0]):
dist[j].append(self.forest_output(np.array([x, y])))
return dist
def plot_density(self, fname='density_estimation.png'):
X = self.grid[0]
Y = self.grid[1]
Z = self.dist
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(111)
vmin=np.min(Z)
vmax=np.max(Z)
var = plt.pcolormesh(np.array(X),np.array(Y),np.array(Z), cmap=cm.Blues, vmin=vmin, vmax=vmax)
plt.colorbar(var, ticks=np.arange(vmin, vmax, (vmax-vmin)/8))
ax = plt.gca()
gris = 200.0
ax.set_facecolor((gris/255, gris/255, gris/255))
#ax.scatter(*zip(*self.data), alpha=.5, c='k', s=10., lw=0)
plt.xlim(np.min(X), np.max(X))
plt.ylim(np.min(Y), np.max(Y))
plt.grid()
ax.set_title('rho = %s, |T| = %d, max_entropy = %.2f'%(self.rho, self.f_size, self.opt_entropy))
fig.savefig(fname, format='png')
plt.close()
def forest_output(self, x):
result = []
for i, t in self.forest.items():
result.append( t.output(x) )
return np.mean(result)
def build_forest(self):
forest = {}
for t in range(self.f_size):
forest[t] = Tree(self, rho=self.rho)
forest[t].tree_leaf_plots(fname='tree_opt%s.png'%t)
path = os.getcwd() + '/plots/'
mkdir_p(path)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
if MODE == 'demo':
color = ['lightcoral', 'dodgerblue', 'mediumseagreen', 'darkorange']
for t in range(self.f_size):
print(len(forest[t].leaf_nodes))
for c, n in enumerate(forest[t].leaf_nodes):
[[i1, i2], [j1, j2]] = n.quad
x1, x2 = self.grid[0][i1], self.grid[0][i2]
y1, y2 = self.grid[1][j1], self.grid[1][j2]
ax.fill_between([x1,x2], y1, y2, alpha=.15, color=color[c])
pd.DataFrame(self.data, columns=['x', 'y']).plot(ax=ax, x='x', y='y', kind='scatter', lw=0, alpha=.6, s=20, c='k')
plt.savefig(path + 'combined.png', format='png')
plt.close()
return forest
# Implement Online L-curve optimization like EWMA to get rid of input depth
def tune_entropy_threshold(self, n=5, depth=6, plot_debug=False):
"""
Compute mean optimal entropy based on L-curve elbow method.
"""
e_arr = []
for i in range(n):
var = Tree(self, rho=.5, depth=depth)
e_arr += [pair + [i] for pair in var.entropy_gain_evol]
var.domain_splits_plots(subpath='%s/'%i)
entropy_evol = pd.DataFrame(e_arr, columns=['depth', 'entropy', 'tree'])
entropy_evol = entropy_evol.groupby(['tree', 'depth'])[['entropy']].mean().reset_index().pivot(columns='tree', index='depth', values='entropy').fillna(0)
entropy_elbow_cand = entropy_evol.apply(lambda x: opt_L_curve(np.array(x.index), np.array(x)))
avg_opt_entropy = entropy_elbow_cand.mean()
if plot_debug:
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
entropy_evol.plot(ax=ax, kind='line', alpha=.6, lw=3., title='Avg. Opt. Entropy = %.2f'%avg_opt_entropy)
plt.savefig('evol.png', format='png')
plt.close()
return avg_opt_entropy
def _run_rf(data, grid_obj):
print('Starting...')
df_obj = DensityForest(data, grid_obj=grid_obj, f_size=5, rho=.5)
df_obj.node_class = LEAF_DICT[LEAF_TYPE]
print('Training...')
df_obj.train()
print('Estimating...')
pdf = df_obj.estimate()
print('Plotting...')
df_obj.plot_density(fname='density_estimation_kde.png')
return df_obj, pdf
def run_demo():
params = {
'mu' : [[0, 0], [0, 20], [50, 40], [10, -20], [20,5]],
'cov': [[[1, 0], [0, 150]], [[90, 0], [0, 5]], [[4, 1], [1, 4]], [[40, 5], [5, 40]], [[90, 15], [15, 16]]],
'n':[700, 20, 200, 400, 300]}
gauss_data_obj = TestDataGauss(params=params, fname='data_new.npy', replace=False)
gauss_data_obj.check_plot()
df_obj, _ = _run_rf(gauss_data_obj.data, gauss_data_obj.grid_obj)
print('Comparing...')
comp = CompareDistributions(original=gauss_data_obj, estimate=df_obj)
comp.vizualize_both('density_comp_kde.png', show_data=False)
def run():
data_obj = TestDataAny(fname=DATA_PATH, partitions=DIVS)
density_estimation, pdf = _run_rf(data_obj.data, data_obj.grid_obj)
pdf_path = os.path.dirname(DATA_PATH)
np.save(pdf_path, pdf)
print('\tDensity estimation function stored at: %s'%pdf_path)
if __name__ == "__main__":
if MODE == 'demo':
print('---------------------------')
print('DEMO')
print('---------------------------')
run_demo()
else:
print('---------------------------')
print('Density Estimation')
print('---------------------------')
run()
'''
params = {
'mu' : [[0, 0], [0, 55], [20, 15], [45, 20]],
'cov': [[[2, 0], [0, 80]], [[90, 0], [0, 5]], [[4, 0], [0, 4]], [[40, 0], [0, 40]]],
'n':[100, 100, 100, 100]}
var = TestDataGauss(params=params, fname='data.npy')
'''
'''
foo = DensityForest(var.data, grid_obj=var.grid_obj, f_size=5, rho=.5)
foo.train()
foo.estimate()
foo.plot_density()
tri = CompareDistributions(original=var, estimate=foo)
tri.vizualize_both('density_comp.png')
'''
#print(foo.forest[0].output([0, 40]))