Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import graphviz
import pandas
from sklearn import tree
clf = tree.DecisionTreeClassifier()
input = pandas.read_csv("/home/glenn/git/clojure-news-feed/client/ml/etl/throughput.csv")
data = input[input.columns[6:9]]
target = input['cloud']
clf = clf.fit(data, target)
dot_data = tree.export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.render(view=True)
import glob
import tempfile
from graphviz import Source
from PIL import Image, ImageDraw, ImageFont
with open(filename) as dot, tempfile.TemporaryDirectory() as tempDirectory:
content = dot.read()
# find out subworkflows
subworkflows = [
line.split()[2].strip('"')
for line in content.splitlines()
if line.startswith('strict digraph')
]
# find out unique subgraphs
unique_subworkflows = list(dict.fromkeys(subworkflows))
#
src = Source(content)
src.format = 'png'
outfile = src.render(filename='sosDot', directory=tempDirectory)
# dot command can generate more than outfiles returned by the render function
pngFiles = glob.glob(os.path.join(tempDirectory, f'sosDot*.png'))
if len(pngFiles) == 1:
return b64_of(outfile)
# create a gif files from multiple png files
pngFiles.sort(
key=lambda x: int(os.path.basename(x)[:-3].split('.')[1] or 0))
# find maximum size for all graphs corresponding to their subgraphs
maxWidth = 0
wf_maxHeight = {}
images = {}
for subworkflow in unique_subworkflows:
wf_images = {
png: Image.open(png)
# Convert to png and show full picture
filename = log_path + '/monitor/' + name
extension = 'png'
import os
if os.path.exists(filename + '.' + extension):
os.remove(filename + '.' + extension)
s = Source(text, filename=filename, format=extension)
s.render()
from IPython.display import Image
image = Image(filename=filename + '.' + extension)
return image
except Exception:
print('Oops! Failed rendering the graph.')
raise
else:
return Source(text)
This is required for new converter path, which maintains and propagates shapes while converting operators.
"""
if not custom_conversion_functions:
custom_conversion_functions = dict()
if not custom_shape_functions:
custom_shape_functions = dict()
if not optional_inputs:
optional_inputs = list()
if outputs is not None:
ssa.extract_subgraph(outputs, name=top_func)
if DEBUG:
import graphviz
dot_string = ssa.get_dot_string(annotation=True, name_and_op_style=False, highlight_debug_nodes=[])
graphviz.Source(dot_string).view(filename='/tmp/ssa')
# apply passes on the ssa, prior to conversion
passes = [
constant_weight_link_removal,
fuse_bias_add,
onehot_matmul_to_embedding,
fuse_layer_norm,
fuse_gelu,
transform_nhwc_to_nchw,
remove_identity,
remove_no_ops_and_shift_control_dependencies,
remove_single_isolated_node,
fuse_conv_mul_add_into_batchnorm,
spatial_reduce_to_global_pool,
fuse_pad_into_conv,
remove_oneway_split,
def tree_legend():
"""Display a legend for the colors of the tree method"""
import graphviz
return graphviz.Source(tree_legend_dot())
def export_tree(clf, feature_names, name):
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=feature_names,
class_names=clf.classes_,
filled=True, rounded=True,
special_characters=True)
graph = graphviz.Source(dot_data)
graph.render(name, cleanup=True)
def visualize_graph(graph: WillumpGraph, mi_inputs: List[WillumpGraphNode] = (),
li_inputs: List[WillumpGraphNode] = (),
indices_to_costs_map: Mapping = None,
indices_to_importances_map: Mapping = None, model_node_inputs=None) -> None:
labels_string, graph_string = graph_to_dot_string(graph, mi_inputs, li_inputs,
indices_to_costs_map, indices_to_importances_map,
model_node_inputs)
dot_string = "digraph G {{\n {0} {1}}}".format(labels_string, graph_string)
print(dot_string)
source = graphviz.Source(dot_string)
if "microsoft" in platform.uname()[3].lower(): # Detect WSL
source.render(format="png", filename="output")
subprocess.run(["cmd.exe", "/C", "call", "output.png"])
else:
source.render(format="png", filename="output", view=True)
print(confusion_matrix)
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
#%%%
y_test.shape, y_pred.shape
y_test.head()
y_pred[0:6]
#%%%
from graphviz import Source
from sklearn import tree
from IPython.display import SVG
#libraries & path of graphviz
import os
os.environ["PATH"] += os.pathsep + 'c:/Program Files (x86)/Graphviz2.38/bin/'
#%%
graph1 = Source(tree.export_graphviz(clf, out_file=None, class_names= ['0', '1'] , filled = True))
display(SVG(graph1.pipe(format='svg')))
#change labels names
graph2 = Source( tree.export_graphviz(clf, out_file=None, feature_names=X.columns, filled=True, class_names=['NoDiabetis','Diabetis']))
graph2
#change max_depth : 1 to 4
Source(tree.export_graphviz(clf, out_file=None, max_depth=1, feature_names=X.columns, class_names=['NonDB','DB'], label='all', filled=True, leaves_parallel=True, impurity=True, node_ids=True, proportion=True, rotate=True, rounded=True, special_characters=False, precision=1))
#https://stackoverflow.com/questions/27817994/visualizing-decision-tree-in-scikit-learn
# This is for saving image in file system
#https://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html
#visualise using dotfile
#True should be returned. goto location and see the file
#%%% Create Decision Tree classifer object
#change max_depth at the time of creation and method