-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtreeplot.py
More file actions
311 lines (268 loc) · 10.1 KB
/
treeplot.py
File metadata and controls
311 lines (268 loc) · 10.1 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
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
311
"""Python package treeplot vizualizes a tree based on a randomforest or xgboost model."""
# --------------------------------------------------
# Name : treeplot.py
# Author : E.Taskesen
# Contact : [email protected]
# github : https://github.com/erdogant/treeplot
# Licence : See Licences
# --------------------------------------------------
# %% Libraries
import numpy as np
from sklearn.tree import export_graphviz
from subprocess import call
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from graphviz import Source
from setgraphviz import setgraphviz
# %% Plot tree
def plot(model, featnames=None, num_trees=None, plottype='horizontal', figsize=(25,25), verbose=3):
"""Make tree plot for the input model.
Parameters
----------
model : model
xgboost or randomforest model.
featnames : list, optional
list of feature names. The default is None.
num_trees : int, default None
The best performing tree is choosen. Specify any other ordinal number for another target tree
plottype : str, (default : 'horizontal')
Works only in case of xgb model.
* 'horizontal'
* 'vertical'
figsize: tuple, default (25,25)
Figure size, (height, width)
verbose : int, optional
Print progress to screen. The default is 3.
0: NONE, 1: ERROR, 2: WARNING, 3: INFO (default), 4: DEBUG, 5: TRACE
Returns
-------
ax : Figure axis
Figure axis of the input model.
"""
modelname = str(model).lower()[0:30]
if ('xgb' in modelname):
if verbose>=4: print('xgboost plotting pipeline.')
ax = xgboost(model, featnames=featnames, num_trees=num_trees, figsize=figsize, plottype=plottype, verbose=verbose)
elif ('lgb' in modelname):
ax = lgbm(model, featnames=featnames, num_trees=num_trees, figsize=figsize, verbose=verbose)
elif ('tree' in modelname) or ('forest' in modelname) or ('gradientboosting' in modelname):
if verbose>=4: print('tree plotting pipeline.')
ax = randomforest(model, featnames=featnames, num_trees=num_trees, figsize=figsize, verbose=verbose)
else:
print('[treeplot] >Model not recognized: %s' %(modelname))
ax = None
return ax
# %% Plot tree
def lgbm(model, featnames=None, num_trees=None, figsize=(25,25), verbose=3):
try:
from lightgbm import plot_tree, plot_importance
except:
if verbose>=1: raise ImportError('lightgbm must be installed. Try to: <pip install lightgbm>')
return None
# Check model
_check_model(model, 'lgb')
# Set env
# _set_graphviz_path()
setgraphviz()
if (num_trees is None) and hasattr(model, 'best_iteration_'):
num_trees = model.best_iteration_
elif num_trees is None:
num_trees = 0
ax1 = None
try:
fig, ax1 = plt.subplots(1, 1, figsize=figsize)
plot_tree(model, dpi=200, ax=ax1)
except:
pass
# if _get_platform() != "windows":
# print('[treeplot] >Install graphviz first: <sudo apt install python-pydot python-pydot-ng graphviz>')
# Plot importance
ax2 = None
try:
fig, ax2 = plt.subplots(1, 1, figsize=figsize)
plot_importance(model, max_num_features=50, ax=ax2)
except:
print('[treeplot] >Error: importance can not be plotted. Booster.get_score() results in empty. This maybe caused by having all trees as decision dumps.')
return(ax1, ax2)
# %% Plot tree
def xgboost(model, featnames=None, num_trees=None, plottype='horizontal', figsize=(25,25), verbose=3):
"""Plot tree based on a xgboost.
Parameters
----------
model : model
xgboost model.
featnames : list, optional
list of feature names. The default is None.
num_trees : int, default None
The best performing tree is choosen. Specify any other ordinal number for another target tree
plottype : str, optional
Make 'horizontal' or 'vertical' plot. The default is 'horizontal'.
figsize: tuple, default (25,25)
Figure size, (height, width)
verbose : int, optional
Print progress to screen. The default is 3.
0: NONE, 1: ERROR, 2: WARNING, 3: INFO (default), 4: DEBUG, 5: TRACE
Returns
-------
ax : Figure axis
Figure axis of the input model.
"""
try:
from xgboost import plot_tree, plot_importance
except:
if verbose>=1: raise ImportError('xgboost must be installed. Try to: <pip install xgboost>')
_check_model(model, 'xgb')
# Set env
# _set_graphviz_path()
setgraphviz()
if plottype=='horizontal': plottype='UD'
if plottype=='vertical': plottype='LR'
if (num_trees is None) and hasattr(model, 'best_iteration'):
num_trees = model.best_iteration
if verbose>=3: print('[treeplot] >Best detected tree: %.0d' %(num_trees))
elif num_trees is None:
num_trees = 0
ax1 = None
try:
fig, ax1 = plt.subplots(1, 1, figsize=figsize)
plot_tree(model, num_trees=num_trees, rankdir=plottype, ax=ax1)
except:
pass
# if _get_platform() != "windows":
# print('[treeplot] >Install graphviz first: <sudo apt install python-pydot python-pydot-ng graphviz>')
# Plot importance
ax2 = None
try:
fig, ax2 = plt.subplots(1, 1, figsize=figsize)
plot_importance(model, max_num_features=50, ax=ax2)
except:
print('[treeplot] >Error: importance can not be plotted. Booster.get_score() results in empty. This maybe caused by having all trees as decision dumps.')
return(ax1, ax2)
# %% Plot tree
def randomforest(model, featnames=None, num_trees=None, filepath='tree', export='png', resolution=100, figsize=(25,25), verbose=3):
"""Plot tree based on a randomforest.
Parameters
----------
model : model
randomforest model.
featnames : list, optional
list of feature names. The default is None.
num_trees : int, default 0
Specify the ordinal number of target tree
filepath : str, optional
filename to export. The default is 'tree'.
export : list of str, optional
Export type. The default is 'png'.
Alternatives: 'pdf', 'png'
resolution : int, optional
resolution of the png file. The default is 100.
figsize: tuple, default (25,25)
Figure size, (height, width)
verbose : int, optional
Print progress to screen. The default is 3.
0: NONE, 1: ERROR, 2: WARNING, 3: INFO (default), 4: DEBUG, 5: TRACE
Returns
-------
ax : Figure axis
Figure axis of the input model.
"""
ax=None
dotfile = None
pngfile = None
if num_trees is None: num_trees = 0
# Check model
_check_model(model, 'randomforest')
# Set env
# _set_graphviz_path()
setgraphviz()
if export is not None:
dotfile = filepath + '.dot'
pngfile = filepath + '.png'
if featnames is None:
featnames = np.arange(0,len(model.feature_importances_)).astype(str)
# Get model parameters
if ('gradientboosting' in str(model).lower()):
estimator = model.estimators_[num_trees][0]
else:
if hasattr(model, 'estimators_'):
estimator = model.estimators_[num_trees]
else:
estimator = model
# Make dot file
dot_data = export_graphviz(estimator,
out_file=dotfile,
feature_names=featnames,
class_names=model.classes_.astype(str),
rounded=True,
proportion=False,
precision=2,
filled=True,
)
# Save to pdf
if export == 'pdf':
s = Source.from_file(dotfile)
s.view()
# Save to png
elif export == 'png':
try:
call(['dot', '-Tpng', dotfile, '-o', pngfile, '-Gdpi=' + str(resolution)])
fig, ax = plt.subplots(1, 1, figsize=figsize)
img = mpimg.imread(pngfile)
plt.imshow(img)
plt.axis('off')
plt.show()
except:
pass
# if _get_platform() != "windows":
# print('[treeplot] >Install graphviz first: <sudo apt install python-pydot python-pydot-ng graphviz>')
else:
graph = Source(dot_data)
plt.show()
return(ax)
# %% Import example dataset from github.
def import_example(data='random', n_samples=1000, n_feat=10):
"""Import example dataset from sklearn.
Parameters
----------
data : str
'random' : str, two-class
'breast' : str, two-class
'titanic': str, two-class
'iris' : str, multi-class
n_samples : int, optional
Number of samples to generate. The default is 1000.
n_feat : int, optional
Number of features to generate. The default is 10.
Returns
-------
tuple (X,y).
X is the dataset and y the response variable.
"""
try:
from sklearn import datasets
except:
print('This requires: <pip install sklearn>')
return None, None
if data=='iris':
X, y = datasets.load_iris(return_X_y=True)
elif data=='breast':
X, y = datasets.load_breast_cancer(return_X_y=True)
elif data=='titanic':
X, y = datasets.fetch_openml("titanic", version=1, as_frame=True, return_X_y=True)
elif data=='random':
X, y = datasets.make_classification(n_samples=n_samples, n_features=n_feat)
return X, y
# %% Check input model
def _check_model(model, expected):
modelname = str(model).lower()
if (expected=='randomforest'):
if ('forest' in modelname) or ('tree' in modelname) or ('gradientboosting' in modelname):
pass
else:
print('[treeplot] >>Warning: The input model seems not to be a tree-based model?')
if (expected=='xgb'):
if ('xgb' not in modelname):
print('[treeplot] >Warning: The input model seems not to be a xgboost model?')
if (expected=='lgb'):
if ('lgb' not in modelname):
print('[treeplot] >Warning: The input model seems not to be a lightgbm model?')