88# --------------------------------------------------
99
1010# %% Libraries
11- # import os
12- # import sys
13- # import zipfile
1411import numpy as np
1512from sklearn .tree import export_graphviz
16- # from sklearn.tree import export_text
1713from subprocess import call
1814import matplotlib .image as mpimg
1915import matplotlib .pyplot as plt
2016from graphviz import Source
2117from setgraphviz import setgraphviz
2218
23- URL = 'https://erdogant.github.io/datasets/graphviz-2.38.zip'
24-
25-
2619# %% Plot tree
2720def plot (model , featnames = None , num_trees = None , plottype = 'horizontal' , figsize = (25 ,25 ), verbose = 3 ):
2821 """Make tree plot for the input model.
@@ -302,59 +295,6 @@ def import_example(data='random', n_samples=1000, n_feat=10):
302295 return X , y
303296
304297
305- # %% Get graphiz path and include into local PATH
306- # def _set_graphviz_path(verbose=3):
307- # finPath=''
308- # if _get_platform()=="windows":
309- # # Download from github
310- # [gfile, curpath] = _download_graphviz(URL, verbose=verbose)
311-
312- # # curpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'RESOURCES')
313- # # filesindir = os.listdir(curpath)[0]
314- # idx = gfile[::-1].find('.') + 1
315- # dirname = gfile[:-idx]
316- # getPath = os.path.abspath(os.path.join(curpath, dirname))
317- # getZip = os.path.abspath(os.path.join(curpath, gfile))
318- # # Unzip if path does not exists
319- # if not os.path.isdir(getPath):
320- # if verbose>=3: print('[treeplot] >Extracting graphviz files..')
321- # [pathname, _] = os.path.split(getZip)
322- # # Unzip
323- # zip_ref = zipfile.ZipFile(getZip, 'r')
324- # zip_ref.extractall(pathname)
325- # zip_ref.close()
326- # getPath = os.path.join(pathname, dirname)
327-
328- # # Point directly to the bin
329- # finPath = os.path.abspath(os.path.join(getPath, 'release', 'bin'))
330- # else:
331- # pass
332- # # sudo apt install python-pydot python-pydot-ng graphviz
333- # # dpkg -l | grep graphviz
334- # # call(['dpkg', '-l', 'grep', 'graphviz'])
335- # # call(['dpkg', '-s', 'graphviz'])
336-
337- # # Add to system
338- # if finPath not in os.environ["PATH"]:
339- # if verbose>=3: print('[treeplot] >Set path in environment.')
340- # os.environ["PATH"] += os.pathsep + finPath
341-
342- # return(finPath)
343-
344-
345- # %%
346- # def _get_platform():
347- # platforms = {
348- # 'linux1':'linux',
349- # 'linux2':'linux',
350- # 'darwin':'osx',
351- # 'win32':'windows'
352- # }
353- # if sys.platform not in platforms:
354- # return sys.platform
355- # return platforms[sys.platform]
356-
357-
358298# %% Check input model
359299def _check_model (model , expected ):
360300 modelname = str (model ).lower ()
@@ -369,36 +309,3 @@ def _check_model(model, expected):
369309 if (expected == 'lgb' ):
370310 if ('lgb' not in modelname ):
371311 print ('[treeplot] >Warning: The input model seems not to be a lightgbm model?' )
372-
373- # %% Import example dataset from github.
374- # def _download_graphviz(url, verbose=3):
375- # """Import example dataset from github.
376-
377- # Parameters
378- # ----------
379- # url : str, optional
380- # url-Link to graphviz. The default is 'https://erdogant.github.io/datasets/graphviz-2.38.zip'.
381- # verbose : int, optional
382- # Print message to screen. The default is 3.
383-
384- # Returns
385- # -------
386- # tuple : (gfile, curpath).
387- # gfile : filename
388- # curpath : currentpath
389-
390- # """
391- # curpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'RESOURCES')
392- # gfile = wget.filename_from_url(url)
393- # PATH_TO_DATA = os.path.join(curpath, gfile)
394- # if not os.path.isdir(curpath):
395- # if verbose>=3: print('[treeplot] >Downloading graphviz..')
396- # os.makedirs(curpath, exist_ok=True)
397-
398- # # Check file exists.
399- # if not os.path.isfile(PATH_TO_DATA):
400- # # Download data from URL
401- # if verbose>=3: print('[treeplot] >Downloading graphviz..')
402- # wget.download(url, curpath)
403-
404- # return(gfile, curpath)
0 commit comments