def graph_to_file(graph, filename='graph.png', delete_single=False): """Exports a graph to a image file. Params: graph - The graph to export. filename - The destination of the output. The filename should include an extention, the format of the file will always be PNG no matter what the extention is. delete_single - If set to true then all nodes without any neighbours will be deleted prior to exporting the graph. """ logger = logging.getLogger('.'.join((__name__, 'graph_to_file'))) logger.info("Exporting a graph to %s", filename) # Delete nodes that don't have any neighbours if delete_single: del_nodes = [ node for node in graph.nodes() if not graph.neighbors(node) ] logger.info("Deleting %d nodes without neighbours", len(del_nodes)) for node in del_nodes: graph.del_node(node) # Write the graph dot = graphtodot.write(graph) gvgraph = graphviz.graph_from_dot_data(dot) gvgraph.write(filename, format='png')
def graph_to_file(graph, filename='graph.png', delete_single=False): """Exports a graph to a image file. Params: graph - The graph to export. filename - The destination of the output. The filename should include an extention, the format of the file will always be PNG no matter what the extention is. delete_single - If set to true then all nodes without any neighbours will be deleted prior to exporting the graph. """ logger = logging.getLogger('.'.join((__name__, 'graph_to_file'))) logger.info("Exporting a graph to %s", filename) # Delete nodes that don't have any neighbours if delete_single: del_nodes = [node for node in graph.nodes() if not graph.neighbors(node)] logger.info("Deleting %d nodes without neighbours", len(del_nodes)) for node in del_nodes: graph.del_node(node) # Write the graph dot = graphtodot.write(graph) gvgraph = graphviz.graph_from_dot_data(dot) gvgraph.write(filename, format='png')
def display_tree(classifier, headers, target): dot_data = tree.export_graphviz(classifier, out_file=None, feature_names=headers, class_names=target) graph = graphviz.graph_from_dot_data(dot_data) graph.write_png(os.path.join(out_path, model_name + '.png'))
def DrawPNG(root, out_file): ''' @param root: 根节点 @param out_file: ''' # generation of new dot g = graphviz.Dot() TreeToGraph(0, g, root) g2 = graphviz.graph_from_dot_data(g.to_string()) g2.write_png(out_file)
def DrawPNG(root, out_file): try: from pydotplus import graphviz except ImportError: print("module pydotplus.graphviz not found") g = graphviz.Dot() # generation of new dot TreeToGraph(0, g, root) g2 = graphviz.graph_from_dot_data(g.to_string()) g2.write_png(out_file)
def main(): with open('dataset/xigua_dataset_3_0.csv') as fd: df = pd.read_csv(fd) features = df[df.columns[1:-1]] labels = df[df.columns[-1]] clf = DecisionTreeClassifier() clf.fit(features, labels) g = graphviz.graph_from_dot_data(clf.graph(label_title="好瓜?")) g.write_png('tree.png')
def DrawPNG(root, out_file): '''visualization of decision tree from root @param root: the root node @param out_file: str,name and path of output file''' try: from pydotplus import graphviz except ImportError: print("module pydotplus.graphviz not found") g = graphviz.Dot() #generation of new dot TreeToGraph(0, g, root) g2 = graphviz.graph_from_dot_data(g.to_string()) g2.write_png(out_file)
def DrawPNG(root, out_file): ''' visualization of decision tree from root. @param root: Node, the root node for tree. @param out_file: str, name and path of output file ''' from pydotplus import graphviz g = graphviz.Dot() # generation of new dot TreeToGraph(0, g, root) g2 = graphviz.graph_from_dot_data(g.to_string()) g2.write_png(out_file)
def DrawPNG(root, out_file): ''' visualization of decision tree from root. @param root: Node, the root node for tree. @param out_file: str, name and path of output file ''' try: from pydotplus import graphviz except ImportError: print("module pydotplus.graphviz not found") g = graphviz.Dot() # generation of new dot TreeToGraph(0, g, root) g2 = graphviz.graph_from_dot_data( g.to_string() ) g2.write_png(out_file)
def draw_tree(root, out_file): ''' visualization of decision tree. Inputs: root: Node, the root node for tree. out_file: str, file path ''' try: from pydotplus import graphviz except ImportError: print("module pydotplus.graphviz not found") g = graphviz.Dot() # generation of new dot tree2graph(0, g, root) g2 = graphviz.graph_from_dot_data(g.to_string()) g2.write_png(out_file)
def vis_tree(cls, decision_tree_model, data, dependent_label): """ Description: visualize the decision tree using GraphViz. Requirement: pip3 install pydotplus apt install graphviz :param decision_tree_model: :param data: pandas.core.frame.DataFrame :param dependent_label: categorical label :return: """ categorical_values = np.unique(data[dependent_label].values) dot_data = export_graphviz(decision_tree_model, filled=True, rounded=True, class_names=categorical_values, feature_names=data.columns, out_file=None) graph = graph_from_dot_data(dot_data) # Create graph from dot data graph.write_png('decision_tree.png') # Write graph to PNG image
max_depth=4, random_state=1) tree.fit(X, y) #%% from pydotplus.graphviz import graph_from_dot_data from sklearn.tree import export_graphviz dot_data = export_graphviz( # Create dot data tree, filled=True, rounded=True, class_names=['Setosa', 'Versicolor', 'Virginica'], feature_names=['petal length', 'petal width'], out_file=None) graph = graph_from_dot_data(dot_data) # Create graph from dot data graph.write_jpg(r'C:\Users\leeko\Google Drive\Python notebook\tree.jpg' ) # Write graph to PNG image #%% """ run regression tree """ import pandas as pd from sklearn import datasets import matplotlib.pyplot as plt boston = datasets.load_boston() # Load Boston Dataset df = pd.DataFrame( boston.data[:, 12]) # Create DataFrame using only the LSAT feature df.columns = ['LSTAT'] df['MEDV'] = boston.target # Create new column with the target MEDV
acertos = 0 for i in range(len(testCrisis)): if(testCrisis[i] == predict[i]): acertos +=1 print("Acertos: %f%%" % (float(acertos/len(testCrisis))*100)) # Serve apenas para exportar a estrutura da arvore em formato de imagem dot_data = export_graphviz( tree, filled=True, rounded=True, class_names=['naoCrise', 'Crise'], feature_names=['criseSist','cambioDolar','dividaPublInt','dividaPubExt','dividaPib','inflAnual','indep','criseMone','criseInfl'], out_file=None ) graph = graph_from_dot_data(dot_data) graph.write_png('classifier.png') # DECISION TREE REGRESSOR # regressor = DecisionTreeRegressor(random_state=0) # regressor.fit(trainFeatures, trainCrisis) # pred = regressor.predict(testFeatures) # print(pred) # acertos = 0 # for i in range(len(testCrisis)): # if(testCrisis[i] == predict[i]): # acertos +=1
print(df) train_index = [0, 1, 2, 5, 6, 9, 13, 14, 15, 16] test_index = [3, 4, 7, 8, 10, 11, 12] df_train = df.loc[train_index, :].reset_index(drop=True) df_test = df.loc[test_index, :].reset_index(drop=True) print(df_test) # Tree = Preprune(df_train, df_test) Tree = TreeGenerate(df_train) Tree = postpurn(Tree, df_test) node_plies = node_plie({}, 0, Tree) from pydotplus import graphviz g = graphviz.Dot() # 创建一个Dot图对象 TreeToGraph(0, Tree, g) g2 = graphviz.graph_from_dot_data(g.to_string()) # 将Dot对象输出为字符串g.to_string() # 并通过graphviz解码 g2.write_png('D:\\Desktop\postpurn_test.png') # 第二种可视化实现方式(测试成功) ''' def TreeToGraph(i, father_node, dot): """ 给定起始节点的名字i(用数字记录节点名)、节点、和 标签名字 用i+1,和子节点及标签名作为递归输入 返回的是i和子节点的名称 将所有的节点遍历完成后返回 :param i: 为了避免迭代时子节点重新从零开始计,这里传入参数i用来累加迭代 :param node:根节点 :param df:根节点的数据 """
def Decisiontree(): # Lectura del archivo csv con el nombre 'data_set_train.csv' si se desea utilizar otro archivo se debe modificar este valor por el nombre del archivo data = pd.read_csv('data_set_train.csv') # Varibles del archivo csv que seran utilizadas en Indep y en Dep. variables = ['ph', 'soil_temperature', 'soil_moisture', 'illuminance','env_temperature', 'env_humidity'] # Indep y Dep son las variables independientes y dependientes que seran necesarias para entrenar el arbol Indep = data[variables].values Dep = data['label'].values # Implementacion del arbol de decision con el criterio de 'GINI', con una profundidad maxia de 4 nodos y finalmente un minimo de 5 hojas en el arbol Arbol = DecisionTreeClassifier(criterion="gini", random_state=100, max_depth=None, min_samples_leaf=5) # Entrenamento del arbol de decision para poder detectar cual sera la variable que mas parte el arbol y genera el indice de gini mas bajom # este requiere los valores independientes y dependientes para ser analizado. Arbol.fit(Indep, Dep) print("Generando imagen del arbol \n") # Implementacion de Grapviz para la graficacion del arbol. # filled y rounded se implementan para que el arbol tenga un color y detalle para diferenciar cuando hay o no hay roya, adicionalmente 'class_names' contiene los # valores que digitaran si hay o no hay roya. dot_data = export_graphviz( Arbol,filled=True, rounded=True,class_names=['yes', 'no'],feature_names=variables) # Importacion del arbol generado en graphviz por medio de un archivo de tipo '.png' el cual contendra el arbol generado. graph = graph_from_dot_data(dot_data) graph.write_png('tree.png') print("Imagen generada\n") ''' Funcion para determinar la precision que tiene el arbol previamente construido y entrenado, para determinar la precision se utilizo otro archivo csv que valores diferentes al los que fue entrenado el arbol previamente. ''' data1 = pd.read_csv('data_set_test.csv') Indep1 = data1[variables].values Dep1 = data1['label'].values Accuracy = Arbol.score(Indep1, Dep1)*100 print("Precision del arbol-> ",Accuracy,'%','\n') # Implementacion de try para evitar errores que pueden ser ocasionados al digitar valores erroneos como tipo string try: print(" Digite los valores separados por comas a evaluar: \n") print("ph soil_temperature soil_moisture illuminance env_temperature env_humidity") # Lectura de los valores digitados en la consola, estos seran procesados para luego ser evaluados. # Esta parte del codigo leera los valores digitados para despues almancenarlos en un arreglo que sera implementado futuramente. str = input("-> ") list = str.split (",") PredicVals = [] for i in list: PredicVals.append(float(i)) # Esta parte del codigo se implementara para predecir el valor previamente digitado y almacenado en un arreglo para detectar # si el valor digitado tiene o no roya por lo cual para realizar este paso es necesario primero entrenar al arbol para que pueda detectar el patron. Prediction = Arbol.predict([PredicVals]) print("\n Prediccion con: \n\n", PredicVals,end=" -> ") if Prediction == 'yes': print("El valor ingresado posee roya") else: print("El valor ingresado no posee roya") except ValueError: print ("Error! Valor no valido. Intentelo otra vez...")
def Exetime(): ''' La estructura general de este metodo es implementar time.time() que tomara el tiempo que se demore en ejecutar alguna de la funciones del metodo decision tree, por lo cual time.time() sera aplicado como Tfinal - Tinicial para conocer el tiempo total de ejecucion de cada funcion, y este resultado sera previamente mostrado en pantalla. ''' # Toma de tiempos para la lectura del archivo csv time1 = time.time() data = pd.read_csv('data_set.csv') time2 = time.time() print("Tiempo de lectura del archivo csv: \n -> ", time2-time1,"s \n") # Toma de tiempos para la creacion del arbol time3 = time.time() variables = ['ph', 'soil_temperature', 'soil_moisture', 'illuminance','env_temperature', 'env_humidity'] Indep = data[variables].values Dep = data['label'].values Arbol = DecisionTreeClassifier(criterion="gini", random_state=100, max_depth=None, min_samples_leaf=5) time4 = time.time() print("Tiempo de creacion del arbol: \n -> ", time4-time3,"s \n") # Toma de tiempos para el entrenamiento del arbol con los datos obtenidos en el csv time5 = time.time() Arbol.fit(Indep, Dep) time6 = time.time() print("Tiempo de entrenamiento del arbol: \n -> ", time6-time5,"s \n") # Toma de tiempos para la graficacion del arbol de decisiones time7 = time.time() dot_data = export_graphviz( Arbol,filled=True, rounded=True,class_names=['yes', 'no'],feature_names=variables) graph = graph_from_dot_data(dot_data) graph.write_png('time.png') time8 = time.time() print("Tiempo de graficacion del arbol: \n -> ", time8-time7,"s \n") # Toma de tiempos para la prediccion de un valor ingresado try: print(" Digite los valores separados por comas a evaluar: \n") print("ph soil_temperature soil_moisture illuminance env_temperature env_humidity") # Lectura de los valores digitados en la consola, estos seran procesados para luego ser evaluados. # Esta parte del codigo leera los valores digitados para despues almancenarlos en un arreglo que sera implementado futuramente. str = input("-> ") list = str.split (",") time9 = time.time() PredicVals = [] for i in list: PredicVals.append(float(i)) # Esta parte del codigo se implementara para predecir el valor previamente digitado y almacenado en un arreglo para detectar # si el valor digitado tiene o no roya por lo cual para realizar este paso es necesario primero entrenar al arbol para que pueda detectar el patron. Prediction = Arbol.predict([PredicVals]) print("\n Prediccion con: \n\n", PredicVals,end=" -> ") if Prediction == 'yes': print("El valor ingresado posee roya") else: print("El valor ingresado no posee roya") time10 = time.time() print("Tiempo de prediccion con los datos: \n -> ", time10-time9,"s \n") except ValueError: print ("Error! Valor no valido. Intentelo otra vez...")
sys.path.insert(0, os.path.abspath('.')) from tools import create_flow_docs # Generate our flow diagrams create_flow_docs.generate('tools/flow-list.txt', 'doc/source/contributor/devref/flow_diagrams') # Generate entity relationship diagram desc = sadisplay.describe( [getattr(models, attr) for attr in dir(models)], show_methods=True, show_properties=True, show_indexes=True, ) graph = graphviz.graph_from_dot_data(sadisplay.dot(desc).encode('utf-8')) graph.write('contributor/devref/erd.svg', format='svg') # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # sys.path.insert(0, os.path.abspath('.')) # -- General configuration ---------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
print2("{} accuracy score : {:.2f}".format("Test", accuracyscoreTest), "{} accuracy score : {:.2f}".format("Train", accuracyscoreTrain), "{} precision score : {:.2f}".format("Test", precisionscoreTest), "{} precision score : {:.2f}".format("Train", precisionscoreTrain), "{} recall score : {:.2f}".format("Test", recallscoresTest), "{} recall score : {:.2f}".format("Train", recallscoreTrain)) graphtreeObject = export_graphviz(model, filled=True, rounded=True, class_names=["Churn", "Not Churn"], feature_names=features.columns, out_file=None) graph = graph_from_dot_data(graphtreeObject) graph.write_pdf(os.path.join(mydir, 'Marketing.pdf')) graph.write_png(os.path.join(mydir, 'Marketing.png')) # find the best depth depth = list(range(2, 20)) depthList = np.zeros((len(depth), 4)) depthList[:, 0] = depth print2(depth, depthList) for indx in range(len(depth)): dtree = DecisionTreeClassifier(max_depth=depth[indx]) model = dtree.fit(X_train, Y_train) yhat = dtree.predict(X_test)