-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexample.py
More file actions
118 lines (76 loc) · 3.14 KB
/
example.py
File metadata and controls
118 lines (76 loc) · 3.14 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
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
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import _tree, export_graphviz
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
from sklearn.utils import check_random_state
def leaf_depths(tree, node_id = 0):
left_child = tree.children_left[node_id]
right_child = tree.children_right[node_id]
if left_child == _tree.TREE_LEAF:
depths = np.array([0])
else:
left_depths = leaf_depths(tree, left_child) + 1
right_depths = leaf_depths(tree, right_child) + 1
depths = np.append(left_depths, right_depths)
return depths
def leaf_samples(tree, node_id = 0):
left_child = tree.children_left[node_id]
right_child = tree.children_right[node_id]
if left_child == _tree.TREE_LEAF:
samples = np.array([tree.n_node_samples[node_id]])
else:
left_samples = leaf_samples(tree, left_child)
right_samples = leaf_samples(tree, right_child)
samples = np.append(left_samples, right_samples)
return samples
def draw_tree(ensemble, tree_id=0):
plt.figure(figsize=(8,8))
plt.subplot(211)
tree = ensemble.estimators_[tree_id].tree_
depths = leaf_depths(tree)
plt.hist(depths, histtype='step', color='#9933ff',
bins=range(min(depths), max(depths)+1))
plt.xlabel("Depth of leaf nodes (tree %s)" % tree_id)
plt.subplot(212)
samples = leaf_samples(tree)
plt.hist(samples, histtype='step', color='#3399ff',
bins=range(min(samples), max(samples)+1))
plt.xlabel("Number of samples in leaf nodes (tree %s)" % tree_id)
plt.show()
def draw_ensemble(ensemble):
plt.figure(figsize=(8,8))
plt.subplot(211)
depths_all = np.array([], dtype=int)
for x in ensemble.estimators_:
tree = x.tree_
depths = leaf_depths(tree)
depths_all = np.append(depths_all, depths)
plt.hist(depths, histtype='step', color='#ddaaff',
bins=range(min(depths), max(depths)+1))
plt.hist(depths_all, histtype='step', color='#9933ff',
bins=range(min(depths_all), max(depths_all)+1),
weights=np.ones(len(depths_all))/len(ensemble.estimators_),
linewidth=2)
plt.xlabel("Depth of leaf nodes")
samples_all = np.array([], dtype=int)
plt.subplot(212)
for x in ensemble.estimators_:
tree = x.tree_
samples = leaf_samples(tree)
samples_all = np.append(samples_all, samples)
plt.hist(samples, histtype='step', color='#aaddff',
bins=range(min(samples), max(samples)+1))
plt.hist(samples_all, histtype='step', color='#3399ff',
bins=range(min(samples_all), max(samples_all)+1),
weights=np.ones(len(samples_all))/len(ensemble.estimators_),
linewidth=2)
plt.xlabel("Number of samples in leaf nodes")
plt.show()
boston = load_boston()
X = boston.data
y = boston.target
rnd = check_random_state(0)
ensemble = RandomForestRegressor(n_estimators=100, random_state=rnd)
ensemble.fit(X,y)
draw_ensemble(ensemble)