コード例 #1
0
ファイル: featureTree.py プロジェクト: MateiV30/Disertation
CameraView -> D;
D -> Menu [style=dotted, label="<requires>"];
PlanetOrbits -> Menu [style=dotted, label="<requires>"];
D -> TopCamera;
D -> EarthCamera;
D -> E;
D -> F;
F -> KeyboardControl [style=dotted, label="<requires>"];
Axes -> Menu [style=dotted, label="<requires>"];
StarField -> Menu [style=dotted, label="<requires>"];
A -> G;
A -> H;
}
"""
#
model = Source(source, filename="feature_model", format="png")
model.view()


# feature_model_solar2
# CameraView [style=dotted];
# A [label="Reading planet data", style=dotted];
# B [label="Ability to toggle features"];
# C [label="Single camera view"];
# D [label="Multiple camera views", style=dotted];
# E [label="Camera moving in a pattern"];
# F [label="Camera moving from user input"];
# G [label="Reading from file"];
# H [label="Reading from command line"];
# SolarSystem -> KeyboardControl;
# SolarSystem -> A [style=bold];
コード例 #2
0
	def graficar(self):
		grafo = "digraph G {\n" + "rankdir = TB;\n" + "rank = min;\n" + "node[style=filled,shape=box, label=\"Inicio\", rankdir=UD];\n"

		tempX = self.ejeX.inicio
		tempXinterno = None
		tempY = self.ejeY.inicio

		j = 0;
		i = 0;

		while tempY != None:
			if tempY == self.ejeY.inicio:
				grafo += "\"" + str(i) + "," + str(j) + "\"[label=\"raiz\", style=filled];\n"
				i += 1
				while tempX != None:
					grafo += "\"" + str(i) + "," + str(j) + "\"[label=\""+tempX.getIndice()+"\", style=filled];\n"
					i += 1
					tempX = tempX.siguiente
				i = 0
				j += 1
			tempXinterno = tempY.listaNodos.inicio
			tempX = self.ejeX.inicio
			grafo += "\"" + str(i) + "," + str(j) + "\"[label=\""+tempY.getIndice()+"\", style=filled];\n"
			i += 1
			while tempX != None:
				if tempXinterno != None:
					if tempXinterno.padreX == tempX:
						grafo += "\"" + str(i) + "," + str(j) + "\"[label=\"Dia: "+ str(tempXinterno.getDia()) + "\", style=filled];\n"
						tempXinterno = tempXinterno.derecha
					else:
						grafo += "\"" + str(i) + "," + str(j) + "\"[label=\"no existe\", style=filled];\n"
				else:
					grafo += "\"" + str(i) + "," + str(j) + "\"[label=\"no existe\", style=filled];\n"
				i += 1
				tempX = tempX.siguiente
			i = 0
			j += 1
			tempY = tempY.siguiente
		print(str(i))
		print(str(j))
		tempX = self.ejeX.inicio
		while tempX != None:
			i += 1
			tempX = tempX.siguiente
		print(str(i))
		print(str(j))
		i += 1
		for y in range(0,j):
		    for x in range (0,i-1):
		        grafo += "\"" + str(x) + "," + str(y) + "\" -> \"" + str(x + 1) + "," + str(y) + "\"[constraint=false];\n"
		        grafo += "\"" + str(x + 1) + "," + str(y) + "\" -> \"" + str(x) + "," + str(y) + "\"[constraint=false];\n"
		        grafo += "{rank=same;\"" + str(x) + "," + str(y) + "\" \"" + str(x + 1) + "," + str(y) + "\"}\n"
		        grafo += "{rank=same;\"" + str(x + 1) + "," + str(y) + "\" \"" + str(x) + "," + str(y) + "\"}\n"

		for y in range(0,j-1):
		    for x in range (0,i):
		        grafo += "\"" + str(x) + "," + str(y) + "\" -> \"" + str(x) + "," + str(y + 1) + "\"[rankdir=UD];\n";
		        grafo += "\"" + str(x) + "," + str(y + 1) + "\" -> \"" + str(x) + "," + str(y) + "\"[rankdir=UD];\n";

		grafo += "labelloc=\"t\"; label=\" MATRIZ DISPERSA CALENDARIO\";}"
		print(grafo)
		
		src = Source(grafo)
		src.format = "png"
		src.render('test-output/MatrizDispersa', view = True)
コード例 #3
0
 def on_stack_size_change(self, change):
     self.stack_size = change['new']
     with self.stack_display:
         clear_output(wait=True)
         display(Source(self.set_stack_display()))
コード例 #4
0
def generar_img(temp : str):
    if temp != "" and temp is not None:
        from graphviz import Source
        s = Source(temp, filename="src\\Reports\\tree_report", format="png")
        s.view()
コード例 #5
0
from sklearn.datasets import load_iris
from sklearn import tree
from graphviz import Source

clf = tree.DecisionTreeClassifier()
iris = load_iris()

clf = clf.fit(iris.data, iris.target)
dot_str = tree.export_graphviz(clf, out_file=None)
print(dot_str)
src = Source(dot_str)
# print(help(src.render))
src.render(view=True, cleanup=True)

コード例 #6
0
    def __init__(self,
                 m_desc,
                 FuseEdges=False,
                 pick_start=False,
                 max_width=10.0,
                 accept_color='chartreuse3',
                 reject_color='red',
                 neutral_color='dodgerblue2'):
        # Options
        self.color_accept = accept_color
        self.color_reject = reject_color
        self.color_neutral = neutral_color
        self.max_width = max_width
        self.fuse = FuseEdges
        # DFA specific option

        self.valid_input = True
        self.machine = m_desc
        self.machine_obj = dotObj_nfa(self.machine, FuseEdges=FuseEdges)
        self.copy_source = reformat_edge_labels(
            set_graph_size(self.machine_obj.source, max_width),
            additional=' color=black arrowsize=1 penwidth=1')

        # Set things we need for the animation
        self.machine_steps = []
        self.feed_steps = []
        self.from_nodes = self.machine['Q0']
        self.to_nodes = self.machine['Q0']
        self.animated = False

        # Setup the widgets
        # Top row for user input
        self.user_input = widgets.Text(value='',
                                       placeholder='Sigma: {{{}}}'.format(
                                           ','.join(
                                               sorted(self.machine['Sigma']))),
                                       description='Input:',
                                       layout=Layout(width='500px'))
        self.user_input.observe(self.on_input_change, names='value')
        self.alternate_start = widgets.SelectMultiple(
            options=sorted(self.machine['Q']),
            value=tuple(self.machine['Q0']),
            rows=5,
            description='Start States:',
            disabled=False,
            layout=Layout(width='200px'))
        self.generate_button = widgets.Button(description="Animate",
                                              button_style='primary',
                                              disabled=False)
        self.generate_button.on_click(self.generate_animation)

        # Bottom row for player controls
        self.play_controls = widgets.Play(interval=950,
                                          value=0,
                                          min=0,
                                          max=100,
                                          step=1,
                                          description="Press play")
        self.play_controls.observe(self.on_play_step, names='value')

        self.speed_control = widgets.IntSlider(value=1,
                                               min=1,
                                               max=10,
                                               step=1,
                                               description='Speed:',
                                               disabled=False,
                                               continuous_update=False,
                                               orientation='horizontal',
                                               readout=True,
                                               readout_format='d')
        self.speed_control.observe(self.on_speed_change, names='value')

        # Create the controls for stepping through the animation
        self.backward = widgets.Button(icon='step-backward',
                                       layout=Layout(width='40px'),
                                       disabled=True)
        self.forward = widgets.Button(icon='step-forward',
                                      layout=Layout(width='40px'),
                                      disabled=True)
        self.backward.on_click(self.on_backward_click)
        self.forward.on_click(self.on_forward_click)

        # set the widget to display the machine
        self.machine_display = widgets.Output()
        with self.machine_display:
            display(Source(self.copy_source))

        # set the widget to display the feed
        self.feed_display = widgets.Output()

        # arrange the widgets in the display area
        row1 = widgets.HBox([self.user_input, self.generate_button])
        if pick_start:
            row1 = widgets.HBox(
                [self.user_input, self.alternate_start, self.generate_button])
        row2 = widgets.HBox([
            self.play_controls, self.backward, self.forward, self.speed_control
        ])
        w = widgets.VBox([row1, self.machine_display, self.feed_display, row2])
        display(w)

        self.play_controls.disabled = True
        self.forward.disabled = True
        self.backward.disabled = True
        self.speed_control.disabled = True
コード例 #7
0
from graphviz import Source
from IPython.display import Image

# In[8]:

get_ipython().system(
    'manage.py graph_models music news shows -o models.png  2>/dev/null')
Image('models.png')

# Alternatively, capture the output, and render it as SVG:

# In[9]:

dot = get_ipython().getoutput('manage.py graph_models shows 2>/dev/null')
Source(dot.n)

# ---

# Learn more about IPython's magic functions:

# In[10]:

get_ipython().magic('quickref')

# ---
#
# ## Answering questions
#
# ### How often do we play gigs?
コード例 #8
0
ファイル: test4.py プロジェクト: minghsu0107/python-note
from graphviz import Source

temp = """
digraph { 

  node [shape=circle,fontsize=8,fixedsize=true,width=0.9]; 
  edge [fontsize=8]; 
  rankdir=LR;

  "low-priority" [shape="doublecircle" color="orange"];
  "high-priority" [shape="doublecircle" color="orange"];

  "s1" -> "low-priority";
  "s2" -> "low-priority";
  "s3" -> "low-priority";

  "low-priority" -> "s4";
  "low-priority" -> "s5";
  "low-priority" -> "high-priority" [label="wait-time exceeded"];

  "high-priority" -> "s4";
  "high-priority" -> "s5";

}
"""
Source(temp).view()
コード例 #9
0
# 7: 0.9817324690630524
# 8: 0.9564958283671037
# 9: 0.9397590361445783

#Average Accuracy: 0.9677
#This model performs slightly better than the gradient boosting model and the SVM

#%%

#Look at feature importances. Must specify the digit in modellist[i]
feature_importances = pd.DataFrame(modellist[9].feature_importances_,
                                   index = X_train.columns,
                                    columns=['importance'])\
                                   .sort_values('importance',
                                                ascending=False)

#Plot top twenty important features
plt.plot(feature_importances[:20])

#Plot one tree
from graphviz import Source
from sklearn import tree as treemodule

treePlot = Source(
    treemodule.export_graphviz(modellist[1].estimators_[1],
                               out_file=None,
                               feature_names=X_train.columns,
                               filled=True,
                               proportion=True))

#%%
コード例 #10
0
data[["preds", "medv"]]

# In[ ]:

from sklearn.tree import export_graphviz
with open("/content/python-ml-course/notebooks/resources/boston_rtree.dot",
          "w") as dotfile:
    export_graphviz(regtree, out_file=dotfile, feature_names=predictors)
    dotfile.close()

import os
from graphviz import Source
file = open("/content/python-ml-course/notebooks/resources/boston_rtree.dot",
            "r")
text = file.read()
Source(text)

# In[ ]:

from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error, make_scorer
import numpy as np

# In[ ]:

cv = KFold(n_splits=10, shuffle=True, random_state=1)
cv.get_n_splits(X)
scores = cross_val_score(regtree,
                         X,
                         Y,
コード例 #11
0
ファイル: single.py プロジェクト: yoannlgd1/pm4py-core
def apply(fp, parameters=None):
    """
    Visualize a footprints table

    Parameters
    ---------------
    fp
        Footprints
    parameters
        Parameters of the algorithm, including:
            - Parameters.FORMAT => Format of the visualization

    Returns
    ---------------
    gviz
        Graphviz object
    """
    if parameters is None:
        parameters = {}

    if type(fp) is list:
        raise Exception("footprints visualizer does not work on list of footprints!")

    activities = sorted(list(set(x[0] for x in fp["sequence"]).union(set(x[1] for x in fp["sequence"])).union(
        set(x[0] for x in fp["parallel"])).union(set(x[1] for x in fp["parallel"]))))
    fp_table = {}

    for a1 in activities:
        fp_table[a1] = {}
        for a2 in activities:
            fp_table[a1][a2] = XOR_SYMBOL

    for x in fp["sequence"]:
        if x not in fp["parallel"]:
            fp_table[x[0]][x[1]] = SEQUENCE_SYMBOL
            fp_table[x[1]][x[0]] = PREV_SYMBOL

    for x in fp["parallel"]:
        fp_table[x[0]][x[1]] = PARALLEL_SYMBOL

    image_format = exec_utils.get_param_value(Parameters.FORMAT, parameters, "png")

    filename = tempfile.NamedTemporaryFile(suffix='.gv')

    footprints_table = ["digraph {\n", "tbl [\n", "shape=plaintext\n", "label=<\n"]
    footprints_table.append("<table border='0' cellborder='1' color='blue' cellspacing='0'>\n")
    footprints_table.append("<tr><td></td>")
    for act in activities:
        footprints_table.append("<td><b>"+act+"</b></td>")
    footprints_table.append("</tr>\n")
    for a1 in activities:
        footprints_table.append("<tr><td><b>"+a1+"</b></td>")
        for a2 in activities:
            footprints_table.append("<td>"+fp_table[a1][a2]+"</td>")
        footprints_table.append("</tr>\n")

    footprints_table.append("</table>\n")
    footprints_table.append(">];\n")
    footprints_table.append("}\n")

    footprints_table = "".join(footprints_table)

    gviz = Source(footprints_table, filename=filename.name)
    gviz.format = image_format

    return gviz
コード例 #12
0
def write_graph(fields, containments, nests, matched_references, clusters):

    # Start a digraph
    graph = "digraph UML {\n"

    # Set the image's size, in inches
    graph += "size= \"33,33\";\n"

    # Add a title
    graph += "labelloc=\"t\";\n"
    graph += "label=<<FONT POINT-SIZE=\"45\">GA4GH Schema Diagram</FONT>>;\n"

    # Define node properties: shaped like UML items.
    graph += "node [\n"
    graph += "\tshape=plaintext\n"
    graph += "]\n\n"

    # Draw each node/type/record as a table
    for type_name, field_list in fields.items():

        graph += "{} [label=<\n".format(type_name)
        graph += "<TABLE BORDER='0' CELLBORDER='1' CELLSPACING='0' CELLPADDING='4' bgcolor='#002060' color='#002060'>\n"
        graph += "\t<TR>\n"
        graph += "\t\t<TD COLSPAN='2' bgcolor='#79A6FF' border='3'><FONT POINT-SIZE='20' color='white'>{}</FONT>".format(
            type_name)

        graph += "</TD>\n"
        graph += "\t</TR>\n"

        # Now draw the rows of fields for the type. A field_list of
        # [a, b, c, d, e, f, g] will have [a, e] in row 1, [b, f] in
        # row 2, [c, g] in row 3, and just [d] in row 4
        num_fields = len(field_list)
        for i in range(0, num_fields // 2 + num_fields % 2):
            # Draw one row.
            graph += "\t<TR>\n"
            # Port number and displayed text will be the i'th field's
            # name
            graph += "\t\t<TD align='left' port='{}'><FONT color='white'>- {}</FONT></TD>\n".format(
                field_list[i][0], field_list[i][0])
            if (num_fields % 2) == 1 and (i == num_fields // 2 +
                                          num_fields % 2 - 1):
                # Don't draw the second cell in the row if you have an
                # odd number of fields and it is the last row
                pass
            else:
                graph += "\t\t<TD align='left' port='{}'><FONT color='white'>- {}</FONT></TD>\n".format(
                    field_list[num_fields // 2 + num_fields % 2 + i][0],
                    field_list[num_fields // 2 + num_fields % 2 + i][0])
            graph += "\t</TR>\n"

        # Finish the table
        graph += "</TABLE>>];\n\n"

    # Now define the clusters/subgraphs
    for cluster_name, cluster_types in clusters.items():
        graph += "subgraph cluster_{} {{\n".format(
            cluster_name.replace(".", "_").replace("/", "_"))
        graph += "\tstyle=\"rounded, filled\";\n"
        graph += "\tcolor=lightgrey;\n"
        graph += "\tnode [style=filled,color=white];\n"
        graph += "\tlabel = \"{}\";\n".format(cluster_name.replace(".", "_"))

        # After all the cluster formatting, define the cluster types
        for cluster_type in cluster_types:
            # cluster_type should match up with a type_name from fields
            graph += "\t{};\n".format(cluster_type)
        graph += "}\n\n"

    # Define edge properties for containments
    graph += "edge [\n"
    graph += "\tdir=both\n"
    graph += "\tarrowtail=odiamond\n"
    graph += "\tarrowhead=none\n"
    graph += "\tcolor=\"#C55A11\"\n"
    graph += "\tpenwidth=2\n"
    graph += "]\n\n"

    for container, containee, container_field_name in containments:
        # Now do the containment edges
        # Only write the edge if the containee is a top-level field in fields.
        if containee in fields:
            graph += "{}:{}:w -> {}\n".format(container, container_field_name,
                                              containee)

    # Define edge properties for references
    graph += "\nedge [\n"
    graph += "\tdir=both\n"
    graph += "\tarrowtail=none\n"
    graph += "\tarrowhead=vee\n"
    graph += "\tstyle=dashed\n"
    graph += "\tcolor=\"darkgreen\"\n"
    graph += "\tpenwidth=2\n"
    graph += "]\n\n"

    for referencer, referencer_field, referencee in matched_references:
        # Now do the reference edges
        graph += "{}:{}:w -> {}:id:w\n".format(referencer, referencer_field,
                                               referencee)

    # Close the digraph off.
    graph += "}\n"
    graph = graph.replace("\n", " ").replace("\t", " ")

    src = Source(graph, format='svg')
    src.render('_build/generated_images/schema_uml')
コード例 #13
0
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,
コード例 #14
0
#pip install graphviz
from graphviz import Source
from sklearn import tree
tree.plot_tree(decision_tree=clsModel)
tree.plot_tree(decision_tree=clsModel,
               feature_names=['Var', 'Skew', ' Kur', 'Ent'],
               class_names=['Org', 'Fake'],
               fontsize=12)
#not a good way to draw graphs.. other methods to be experimented
tree.plot_tree(decision_tree=clsModel,
               max_depth=2,
               feature_names=['Var', 'Skew', ' Kur', 'Ent'],
               class_names=['Org', 'Fake'],
               fontsize=12)

Source(tree.export_graphviz(clsModel))
Source(tree.export_graphviz(clsModel, max_depth=3))
dot_data1 = tree.export_graphviz(clsModel,
                                 max_depth=3,
                                 out_file=None,
                                 filled=True,
                                 rounded=True,
                                 special_characters=True,
                                 feature_names=['Var', 'Skew', ' Kur', 'Ent'],
                                 class_names=['Org', 'Fake'])
#check the folder location after installing the graphviz
import os
os.environ["PATH"] += os.pathsep + 'c:/Program Files (x86)/Graphviz2.38/bin/'
import graphviz
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
コード例 #15
0
def main(args: Any = None) -> None:
    mctx, *_, migrators = initialize_migrators()
    if not os.path.exists("./status"):
        os.mkdir("./status")
    total_status = {}

    for migrator in migrators:
        if isinstance(migrator, GraphMigrator) or isinstance(
                migrator, Replacement):
            if hasattr(migrator, "name"):
                assert isinstance(migrator.name, str)
                migrator_name = migrator.name.lower().replace(" ", "")
            else:
                migrator_name = migrator.__class__.__name__.lower()
            total_status[migrator_name] = f"{migrator.name} Migration Status"
            status, build_order, gv = graph_migrator_status(
                migrator, mctx.graph)
            with open(os.path.join(f"./status/{migrator_name}.json"),
                      "w") as fp:
                json.dump(status, fp, indent=2)

            d = gv.pipe("dot")
            with tempfile.NamedTemporaryFile(suffix=".dot") as ntf:
                ntf.write(d)
                # make the graph a bit more compact
                d = Source(
                    subprocess.check_output([
                        "unflatten", "-f", "-l", "5", "-c", "10", f"{ntf.name}"
                    ], ).decode("utf-8"), ).pipe("svg")
            with open(os.path.join(f"./status/{migrator_name}.svg"),
                      "wb") as fb:
                fb.write(d or gv.pipe("svg"))
        elif isinstance(migrator, Version):
            write_version_migrator_status(migrator, mctx)

    with open("./status/total_status.json", "w") as f:
        json.dump(total_status, f, sort_keys=True)

    lst = [
        k for k, v in mctx.graph.nodes.items() if len([
            z for z in v.get("payload", {}).get("PRed", [])
            if z.get("PR", {}).get("state", "closed") == "open"
            and z.get("data", {}).get("migrator_name", "") == "Version"
        ], ) >= Version.max_num_prs
    ]
    with open("./status/could_use_help.json", "w") as f:
        json.dump(
            sorted(
                lst,
                key=lambda z: (len(nx.descendants(mctx.graph, z)), lst),
                reverse=True,
            ),
            f,
            indent=2,
        )

    lm = LicenseMigrator()
    lst = [
        k for k, v in mctx.graph.nodes.items()
        if not lm.filter(v.get("payload", {}))
    ]
    with open("./status/unlicensed.json", "w") as f:
        json.dump(
            sorted(
                lst,
                key=lambda z: (len(nx.descendants(mctx.graph, z)), lst),
                reverse=True,
            ),
            f,
            indent=2,
        )
コード例 #16
0
ファイル: plotting.py プロジェクト: rserran/xgboost
def to_graphviz(
    booster: Booster,
    fmap: PathLike = "",
    num_trees: int = 0,
    rankdir: Optional[str] = None,
    yes_color: Optional[str] = None,
    no_color: Optional[str] = None,
    condition_node_params: Optional[dict] = None,
    leaf_node_params: Optional[dict] = None,
    **kwargs: Any
) -> GraphvizSource:
    """Convert specified tree to graphviz instance. IPython can automatically plot
    the returned graphviz instance. Otherwise, you should call .render() method
    of the returned graphviz instance.

    Parameters
    ----------
    booster : Booster, XGBModel
        Booster or XGBModel instance
    fmap: str (optional)
       The name of feature map file
    num_trees : int, default 0
        Specify the ordinal number of target tree
    rankdir : str, default "UT"
        Passed to graphviz via graph_attr
    yes_color : str, default '#0000FF'
        Edge color when meets the node condition.
    no_color : str, default '#FF0000'
        Edge color when doesn't meet the node condition.
    condition_node_params : dict, optional
        Condition node configuration for for graphviz.  Example:

        .. code-block:: python

            {'shape': 'box',
             'style': 'filled,rounded',
             'fillcolor': '#78bceb'}

    leaf_node_params : dict, optional
        Leaf node configuration for graphviz. Example:

        .. code-block:: python

            {'shape': 'box',
             'style': 'filled',
             'fillcolor': '#e48038'}

    \\*\\*kwargs: dict, optional
        Other keywords passed to graphviz graph_attr, e.g. ``graph [ {key} = {value} ]``

    Returns
    -------
    graph: graphviz.Source

    """
    try:
        from graphviz import Source
    except ImportError as e:
        raise ImportError('You must install graphviz to plot tree') from e
    if isinstance(booster, XGBModel):
        booster = booster.get_booster()

    # squash everything back into kwargs again for compatibility
    parameters = 'dot'
    extra = {}
    for key, value in kwargs.items():
        extra[key] = value

    if rankdir is not None:
        kwargs['graph_attrs'] = {}
        kwargs['graph_attrs']['rankdir'] = rankdir
    for key, value in extra.items():
        if kwargs.get("graph_attrs", None) is not None:
            kwargs['graph_attrs'][key] = value
        else:
            kwargs['graph_attrs'] = {}
        del kwargs[key]

    if yes_color is not None or no_color is not None:
        kwargs['edge'] = {}
    if yes_color is not None:
        kwargs['edge']['yes_color'] = yes_color
    if no_color is not None:
        kwargs['edge']['no_color'] = no_color

    if condition_node_params is not None:
        kwargs['condition_node_params'] = condition_node_params
    if leaf_node_params is not None:
        kwargs['leaf_node_params'] = leaf_node_params

    if kwargs:
        parameters += ':'
        parameters += json.dumps(kwargs)
    tree = booster.get_dump(
        fmap=fmap,
        dump_format=parameters)[num_trees]
    g = Source(tree)
    return g
コード例 #17
0
def show_cfg(fn, **kwargs):
    return Source(to_graph(gen_cfg(inspect.getsource(fn)), **kwargs))
コード例 #18
0
dot_src = Source("""// DataCamp_Charts
digraph {
	node [rankdir=LR]
	rankdir=LR
	node [shape=ellipse]
	PDF [label="PDF Files" shape=box]
	Certs [label=Certificates shape=ellipse]
	HTML [label="HTML File" shape=box]
	Courses_s [label="Scraped
Courses" shape=ellipse]
	Topics [label="Scraped
Topics" shape=ellipse]
	Tracks_s [label="Scraped
Tracks" shape=ellipse]
	Tracks [label="Tracks
Certs" shape=ellipse]
	Courses [label="Courses
Certs" shape=ellipse]
	Excel [label=Excel shape=box]
	Chart [label=Chart shape=box]
	Document [label="Word or
PDF Document" shape=box]
	Dataset [label="Data set
for BI Tool" shape=box]
	PDF -> Certs [label="Extract Text"]
	HTML -> Courses_s [label=Scrape]
	HTML -> Tracks_s [label=Scrape]
	HTML -> Topics [label=Scrape]
	Courses_s -> Courses [label=fuzzy]
	Certs -> Courses [label=clean]
	Tracks_s -> Tracks [label=""]
	Certs -> Tracks [label="Fix missing letters"]
	Tracks -> Excel [label=""]
	Courses -> Excel [label=filter]
	Topics -> Excel [label=""]
	Tracks -> Chart [label=reshape]
	Courses -> Chart [label=reshape]
	Tracks -> Document [label=TODO]
	Courses -> Document [label=TODO]
	Chart -> Document [label=TODO]
	Tracks -> Dataset [label=TODO]
	Courses -> Dataset [label=TODO]
}""")
コード例 #19
0
#dt.score(X,bos['PRICE'])

from sklearn.externals.six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
import pydotplus
import pydot
from graphviz import Source
dot_data = StringIO()
export_graphviz(dt,
                out_file=dot_data,
                filled=True,
                rounded=True,
                special_characters=True)

graph = Source(tree.export_graphviz(dt, out_file=None,
                                    feature_names=X.columns))
graph.format = 'png'
graph.render('dtree_render', view=True)

#Below line outputs text file tree. Paste the contents to Webgraphviz.
#tree.export_graphviz(dt,out_file='tree.dot')
'''
plt.scatter(bos['RM'],bos['PRICE'])
plt.xlabel ('Rooms')
plt.ylabel ('Price')
plt.title ('Rooms vs Price')
plt.show()


X_train,  X_test, Y_train, Y_test = sklearn.cross_validation.train_test_split(X, bos.PRICE,test_size=0.33,random_state=5)
コード例 #20
0
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)
clf.score(X, y)

#评估分类器性能,计算混淆矩阵,Precision 和 Recall
predicted_y = clf.predict(X)
from sklearn import metrics

print(metrics.classification_report(y, predicted_y))
print('Confusion matrix:')
print(metrics.confusion_matrix(y, predicted_y))

#生成并显示决策树图
featureName = ['House', 'Marital', 'Income']
className = ['Cheat', 'Not Cheat']
#生成图
from graphviz import Source

graph = Source(
    tree.export_graphviz(clf,
                         out_file=None,
                         feature_names=featureName,
                         class_names=className))
#保存到文件中并显示
png_bytes = graph.pipe(format='png')
with open('dectree.png', 'wb') as f:
    f.write(png_bytes)
from IPython.display import Image

Image(png_bytes)
コード例 #21
0
import sys
from graphviz import Source

f = open("./cache/" + sys.argv[1], "r")
text = f.read()
f.close()

a = Source(text, filename=sys.argv[1], directory="./cache/")

a.view()
コード例 #22
0
def train_model():

    print("-----------------")
    print("LOADING DATA...")

    importer = Importer()
    xtrain, ytrain = filter_features(importer.xtrain), importer.ytrain
    xval, yval = filter_features(importer.xval), importer.yval
    xtest, xtest_passenger_ids = filter_features(
        importer.xtest), importer.xtest_passenger_ids
    print("TRAINING:", xtrain.shape, ytrain.shape)
    print("VALIDATION:", xval.shape, yval.shape)
    print("TEST:", xtest.shape, len(xtest_passenger_ids))

    print("-----------------")
    print("CONFIGURING SEARCH PARAMETERS...")

    pipeline = Pipeline(steps=[
        ("one_hot",
         OneHotEncoder(use_cat_names=True, cols=["gender", "salutation"])),
        ("ordinal",
         OrdinalEncoder(cols=["ticket_class"],
                        mapping=[{
                            "col": "ticket_class",
                            "mapping": {
                                "UPPER": 3,
                                "MIDDLE": 2,
                                "LOWER": 1
                            }
                        }])),
        ("imputer", SimpleImputer()),
        #("scaler", StandardScaler()),
        ("classifier", DecisionTreeClassifier(random_state=89))
    ])
    print(type(pipeline))

    search = GridSearchCV(
        estimator=pipeline,
        cv=5,
        verbose=10,
        return_train_score=True,
        n_jobs=-1,  # -1 means using all processors
        scoring="accuracy",  #"f1",
        param_grid={
            "imputer__strategy": ["mean", "median"],
            "classifier__min_samples_leaf": (0.02, 0.03, 0.06),
            "classifier__max_depth": (1, 2, 3, 4, 5),
        })
    print(type(search))

    print("-----------------")
    print("TRAINING...")

    search.fit(xtrain, ytrain)

    best_estimator = search.best_estimator_  #> Pipeline
    one_hot = best_estimator.named_steps["one_hot"]
    ordinal = best_estimator.named_steps["ordinal"]
    model = search.best_estimator_.named_steps["classifier"]

    encoded_feature_names = ordinal.transform(one_hot.transform(xval)).columns
    encoded_feature_importances = Series(
        model.feature_importances_,
        encoded_feature_names).sort_values(ascending=False)
    encoded_feature_importances.name = "encoded_feature"

    print("-----------------")
    print("BEST SEARCH PARAMS:", search.best_params_)
    print("BEST SEARCH SCORE:", search.best_score_)

    print("-----------------")
    training_score = search.score(xtrain, ytrain)
    print("TRAINING SCORE:", training_score)

    print("-----------------")
    validation_score = search.score(xval, yval)
    print("VALIDATION SCORE:", validation_score)

    print("-----------------")
    print("VALIDATION REPORT:")
    yval_predicted = search.predict(xval)
    validation_report = classification_report(yval, yval_predicted)
    print(validation_report)

    print("-----------------")
    print("FEATURE IMPORTANCES:")
    print(encoded_feature_importances)

    print("-----------------")
    print("FEATURE IMPORTANCES VIZ:")
    print(os.path.abspath(FEATURES_GRAPH_FILEPATH))
    fig = px.bar(
        orientation="h",  # horizontal flips x and y params below, but not labels
        x=encoded_feature_importances.values,
        y=encoded_feature_importances.keys(),
        labels={
            "x": "Relative Importance",
            "y": "Feature Name"
        })
    #fig.show()
    fig.write_image(
        FEATURES_GRAPH_FILEPATH
    )  # requires "orca" dependency. see: https://plot.ly/python/static-image-export/

    print("-----------------")
    print("DECISION TREE VIZ:")
    print(os.path.abspath(TREE_GRAPH_FILEPATH))
    class_names = [{
        0: "Not Survive",
        1: "Survive"
    }[class_name] for class_name in search.classes_]  # [0, 1]
    dot_data = export_graphviz(
        model,
        out_file=None,
        max_depth=3,
        feature_names=encoded_feature_names,
        class_names=class_names,  # expects class names to be strings
        impurity=False,
        filled=True,
        proportion=True,
        rounded=True)
    graph = Source(dot_data)
    png_bytes = graph.pipe(format="png")
    with open(TREE_GRAPH_FILEPATH, "wb") as f:
        f.write(png_bytes)

    print("-----------------")
    print("SAVING RESULTS:")
    print(os.path.abspath(SEARCH_RESULTS_FILEPATH))
    results = {
        "pipeline_steps":
        [step_name for step_name, obj in best_estimator.steps],
        "best_score": search.best_score_,
        "best_params": search.best_params_,
        "training_score": training_score,
        "validation_score": validation_score,
        "encoded_feature_importances": encoded_feature_importances.to_dict(),
    }
    with open(SEARCH_RESULTS_FILEPATH, "w") as f:
        f.write(json.dumps(results))

    print("-----------------")
    print("SAVING MODEL:")
    print(os.path.abspath(LATEST_MODEL_FILEPATH))
    with open(LATEST_MODEL_FILEPATH, "wb") as f:
        pickle.dump(search, f)

    print("-----------------")
    print("GENERATING SUBMISSION FILE:")
    print(os.path.abspath(PREDICTIONS_FILEPATH))

    ytest_predictions = search.predict(xtest)
    ytest_predictions_df = DataFrame({
        "PassengerId": xtest_passenger_ids,
        "Survived": ytest_predictions
    })
    ytest_predictions_df.to_csv(PREDICTIONS_FILEPATH, index=False)

    return search
コード例 #23
0
regtree.fit(X,Y)

preds = regtree.predict(data[predictors])

data['preds'] = preds

# print(data[['preds', 'medv']])

with open('resources/boston_rtree.dot', 'w') as dotfile:
    export_graphviz(regtree, out_file=dotfile, feature_names=predictors)
    dotfile.close()

file = open('resources/boston_rtree.dot', 'r')
text = file.read()

Source(text, filename='resources/boston_rtree').view()

cv = KFold(shuffle=True, random_state=1)
scores = cross_val_score(regtree, X, Y, scoring='neg_mean_squared_error', cv=cv, n_jobs=1)
score = np.mean(scores)
print(scores)
print(score) # error

print(list(zip(predictors, regtree.feature_importances_)))

# RANDOM FORESTS

forest = RandomForestRegressor(n_jobs=2, oob_score=True, n_estimators=500) # n_estimators => number of trees
forest.fit(X,Y)

data['rforest_pred'] = forest.oob_prediction_
コード例 #24
0
# pip install graphviz
from graphviz import Source
import os
import sys 

#/home/Automation/dotty-0.9.0-RC1/bin
sys.path.append("/usr/bin/dot")

directorypath = './output/'#./java-callgraph-master/output


dirList = next(os.walk(directorypath))[1]
print dirList
for d in dirList:
    print d;
    fileList = os.listdir(directorypath + d)
    for f in fileList:
        abpath = directorypath + d + '/'+ f
        text_from_file = str()
        with open (abpath) as file:
            text_from_file = file.read()
        src = Source(text_from_file)
        src.render(abpath+'.gv',view=False)
コード例 #25
0
import os
from graphviz import Source

os.chdir('D:\stories\Glutamine\matlab\displayNetwork\linkEnergy')
file = open('test.dot', 'r')  #READING DOT FILE
text = file.read()
s = Source(text, filename='test.dot', format="pdf")
s.view()
file.close()

file = open('test_legend.dot', 'r')  #READING DOT FILE
text = file.read()
s = Source(text, filename='test_legend.dot', format="pdf")
s.view()
file.close()
コード例 #26
0
print("Decision Tree:")
print("Accuracy: ", dtree_accuracy, ", Precision: ",
      round(dtree_prf_support[0], 5), ", Recall: ",
      round(dtree_prf_support[1], 5), ", F-Score: ",
      round(dtree_prf_support[2], 5))
print("Confusion matrix:")
print(cf_matrix)

#print("Accuracy for decision tree after cross validation is: ",acc/100)

# Visualizing decision tree using model
feat_names = list(data.drop('y', axis=1).columns.values)
tree.export_graphviz(d_tree, out_file='d_tree.dot', feature_names=feat_names)
with open('d_tree.dot', 'r') as content_file:
    temp = content_file.read()
s = Source(temp, filename="tree_img", format="png")
s.view()
os.remove('tree_img')
os.remove('d_tree.dot')

# One hot encoding
nb_data = pd.get_dummies(data, columns=categorical_cols[:-1])
nb_data = data[:]
# Standardize continuous values
nb_data[[
    "age", "cons.price.idx", "euribor3m", "campaign", "previous", "nr.employed"
]] /= nb_data[[
    "age", "cons.price.idx", "euribor3m", "campaign", "previous", "nr.employed"
]].max()
nb_data["emp.var.rate"] = preprocessing.scale(nb_data["emp.var.rate"])
nb_data["cons.conf.idx"] = preprocessing.scale(nb_data["cons.conf.idx"])
コード例 #27
0
    def __init__(self, m_desc, FuseEdges=False, show_rejected=False, max_width=10.0):
        # Options
        self.color_accept = 'chartreuse3'
        self.color_reject = 'red'
        self.color_neutral = 'dodgerblue2'
        self.max_width = max_width
        self.fuse = FuseEdges
        # TM specific option
        self.show_rejected = show_rejected
        
        # initialize
        self.valid_input = True
        self.machine = m_desc
        self.machine_obj = dotObj_tm(self.machine, FuseEdges=FuseEdges)
        self.copy_source = reformat_edge_labels(set_graph_size(self.machine_obj.source, max_width))
        
        # Set things we need for the animation
        self.machine_steps = []
        self.tape_steps = []
        self.from_nodes = self.machine['q0']
        self.to_nodes = self.machine['q0']
        self.animated = False
        self.is_back_step = False
        
        # Top row for user input
        self.user_input = widgets.Text(value='',
                                       placeholder='Sigma: {{{}}}'.format(','.join(sorted(self.machine['Sigma']))),
                                       description='Input:',
                                       layout=Layout(width='500px')
                                       )
        self.user_input.observe(self.on_input_change, names='value')
        self.generate_button = widgets.Button(description="Animate", 
                                              button_style='primary', 
                                              disabled=False
                                              )
        self.generate_button.on_click(self.generate_animation)
        self.start_fuel = widgets.BoundedIntText(value=10,
                                                 min=0,
                                                 max=1000,
                                                 step=1,
                                                 description='Fuel:',
                                                 layout=Layout(width='160px'),
                                                 disabled=False
                                                 )

        # Bottom row for player controls
        self.play_controls = widgets.Play(interval=950,
                                          value=0,
                                          min=0,
                                          max=100,
                                          step=1,
                                          description="Press play",
                                          disabled=True
                                          )
        self.play_controls.observe(self.on_play_step, names='value')

        self.speed_control = widgets.IntSlider(value=1,
                                               min=1,
                                               max=10,
                                               step=1,
                                               description='Speed:',
                                               disabled=False,
                                               continuous_update=False,
                                               orientation='horizontal',
                                               readout=True,
                                               readout_format='d'
                                               )
        self.speed_control.observe(self.on_speed_change, names='value')
                
        # Create the controls for stepping through the animation
        self.backward = widgets.Button(icon='step-backward', 
                                       layout=Layout(width='40px'), 
                                       disabled=True
                                       )
        self.forward = widgets.Button(icon='step-forward', 
                                      layout=Layout(width='40px'),
                                      disabled=True
                                      )
        self.backward.on_click(self.on_backward_click)
        self.forward.on_click(self.on_forward_click)
        
        # set the widget to display the machine
        self.machine_display = widgets.Output()
        with self.machine_display:
            display(Source(self.copy_source))
            
        # set the widget to display the tape
        self.tape_display = widgets.Output()
        with self.tape_display:
            display(Source(self.generate_tape('........', 0, '')))

        self.path_dropdown = widgets.Dropdown(options={},
                                              value=None,
                                              description='',
                                              disabled=True,
                                              layout=Layout(width='200px')
                                              )
        self.path_dropdown.observe(self.on_path_change, names='value')

        # arrange the widgets in the display area
        row1 = widgets.HBox([self.user_input, self.start_fuel, self.generate_button]) # not displaying alternate start state
        ms_disp = widgets.HBox([self.machine_display])
        tp_disp = widgets.HBox([self.tape_display])
        play_row = widgets.HBox([self.path_dropdown, self.play_controls, self.backward, self.forward, self.speed_control])
        w = widgets.VBox([row1, ms_disp, tp_disp, play_row])
        display(w)
        
        self.play_controls.disabled = True
        self.forward.disabled = True
        self.backward.disabled = True
        self.speed_control.disabled = True
コード例 #28
0
plt.scatter(tcp[:,0], tcp[:,1], color='red', label='tcp')
plt.legend(loc='upper right')
plt.show()
"""
############### Part(7-8): Split 70% ##############
data = pd.read_excel('traceDMA.xlsx', index_col=None, header=None)
X_train = data.values[:7000, :2]
Y_train = data.values[:7000, 2:]
X_test = data.values[7000:, :2]
Y_test = data.values[7000:, 2:]
#Train
clf_70 = DecisionTreeClassifier(criterion="entropy", max_depth=4)
clf_70.fit(X_train, Y_train)
acc_70 = clf_70.score(X_train, Y_train, sample_weight=None)
#Tree
graph = Source(export_graphviz(clf_70, out_file=None))
graph.format = 'png'
graph.render('tree_70%', view=False)
#Test/Predict
Y_predict = clf_70.predict(X_test)
acc_30 = clf_70.score(X_test, Y_test, sample_weight=None)

udp = list()
tcp = list()
udp_pred = list()
tcp_pred = list()
udp_train = list()
tcp_train = list()
data = pd.Series.tolist(data)
for i in range(len(Y_predict)):
    if Y_predict[i] == 'udp':
コード例 #29
0
 def generate_animation(self, change):
     # switching to input mode
     if self.animated:
         # enable the input controls
         self.play_controls._playing = False
         self.animated = False
         self.user_input.disabled = False
         self.start_fuel.disabled = False
         # update the button to switch between modes
         self.generate_button.description='Animate'
         self.generate_button.button_style = 'primary'
         # disable the play controls
         self.play_controls.disabled = True
         self.forward.disabled = True
         self.backward.disabled = True
         self.speed_control.disabled = True
         self.path_dropdown.disabled = True
         self.path_dropdown.index = None
         # clean the machine display
         with self.machine_display:
             clear_output(wait=True)
             display(Source(self.copy_source))
         with self.tape_display:
             clear_output(wait=True)
             display(Source(self.generate_tape('........', 0, '')))
     
     # switching to play mode
     else:
         # ignore invalid input
         if not self.valid_user_input():
             return
         # disable the input controls
         self.animated = True
         self.user_input.disabled = True
         self.start_fuel.disabled = True
         self.generate_button.description='Change Input'
         # clean the current play displays
         self.tape_steps = []
         self.machine_steps = []
         
         # find the acceptance paths
         result = ()
         with io.capture_output() as captured:
             result = run_tm(self.machine, self.user_input.value, self.start_fuel.value)
         paths = result[1]
         
         new_dropdown_options = {}          
         # generate all the display steps for each path
         path_count = 0
         for p in paths:
             # check if this is a rejected path
             if not self.show_rejected and p[0][0] not in self.machine['F']:
                 continue
                 
             path_states = p[1]
             max_steps = (len(path_states))*2+1
             path_states.append(p[0])
             
             # generate the feed display
             path_tape_steps = []
             inspecting = ''
             for step in range(max_steps):
                 tape_contents = path_states[step//2][2]
                 header_pos = path_states[step//2][1]
                 while (len(tape_contents) <= header_pos):
                     tape_contents += '.'
                     
                 # make a header message
                 header_message = ''
                 if step == max_steps - 1:
                     header_message = 'STOP'
                 elif step%2 == 0:
                     header_message = 'READ: {}'.format(tape_contents[header_pos])
                 else:
                     write_val = ''
                     move_dir = ''
                     destinations = self.machine['Delta'][(path_states[step//2][0],'{}'.format(tape_contents[header_pos]))]
                     for d in destinations:
                         if d[0] == path_states[step//2+1][0]:
                             write_val = d[1]
                             move_val = d[2]
                             break
                     header_message = 'WRITE: {}'.format(write_val)
                     tape_contents = path_states[step//2+1][2]
                 tape_step = self.generate_tape(tape_contents, header_pos, header_message)
                 path_tape_steps.append(tape_step)
                 
             # generate the machine steps
             path_obj_steps = []
             for step in range(max_steps):
                 path_obj_steps.append(self.generate_machine_steps(path_states, step, max_steps))
             
             # add the path as an option in the dropdown
             path_count += 1
             path_name = 'Path {}: {}'.format(path_count, 'Accepted' if p[0][0] in self.machine['F'] else 'Rejected')
             new_dropdown_options[path_name] = (max_steps-1, path_obj_steps, path_tape_steps)
             
         # if there are no paths we don't have any animations to build (this should never happen for TM)
         if path_count == 0:
             self.generate_button.button_style = 'danger'
             rejected_machine = set_graph_color(self.copy_source, self.color_reject)
             rejected_machine = set_graph_label(rejected_machine, "< <font color='{}'><b>No paths found for '{}'</b></font>>".format(self.color_reject,self.user_input.value))
             with self.machine_display:
                 clear_output(wait=True)
                 display(Source(rejected_machine))
             return
         
         # update the dropdown
         self.path_dropdown.options = new_dropdown_options
         self.path_dropdown.index = 0
         # display the machine for this step
         with self.machine_display:
             clear_output(wait=True)
             display(Source(self.machine_steps[0]))
         # display the feed for this step
         with self.tape_display:
             clear_output(wait=True)
             display(Source(self.tape_steps[0]))
     
         # enable the controls
         self.backward.disabled = True
         if len(self.user_input.value) == 0:
             self.forward.disabled = True
         else:
             self.forward.disabled = False
         self.play_controls.disabled = False
         self.speed_control.disabled = False
         self.path_dropdown.disabled = False
コード例 #30
0
ファイル: plot_tree.py プロジェクト: chenwen1226/data-science
def plot_tree(dt, feature_names, max_depth):
    return Source(
        tree.export_graphviz(dt,
                             out_file=None,
                             feature_names=feature_names,
                             max_depth=max_depth))