本文整理汇总了Python中pydotplus.graph_from_dot_data方法的典型用法代码示例。如果您正苦于以下问题:Python pydotplus.graph_from_dot_data方法的具体用法?Python pydotplus.graph_from_dot_data怎么用?Python pydotplus.graph_from_dot_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydotplus
的用法示例。
在下文中一共展示了pydotplus.graph_from_dot_data方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: show_pdf
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def show_pdf(clf):
'''
可视化输出
把决策树结构写入文件: http://sklearn.lzjqsdd.com/modules/tree.html
Mac报错: pydotplus.graphviz.InvocationException: GraphViz's executables not found
解决方案: sudo brew install graphviz
参考写入: http://www.jianshu.com/p/59b510bafb4d
'''
# with open("testResult/tree.dot", 'w') as f:
# from sklearn.externals.six import StringIO
# tree.export_graphviz(clf, out_file=f)
import pydotplus
from sklearn.externals.six import StringIO
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("../../../output/3.DecisionTree/tree.pdf")
# from IPython.display import Image
# Image(graph.create_png())
示例2: show_pdf
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def show_pdf(clf):
'''
可视化输出
把决策树结构写入文件: http://sklearn.lzjqsdd.com/modules/tree.html
Mac报错: pydotplus.graphviz.InvocationException: GraphViz's executables not found
解决方案: sudo brew install graphviz
参考写入: http://www.jianshu.com/p/59b510bafb4d
'''
# with open("testResult/tree.dot", 'w') as f:
# from sklearn.externals.six import StringIO
# tree.export_graphviz(clf, out_file=f)
import pydotplus
from sklearn.externals.six import StringIO
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("output/3.DecisionTree/tree.pdf")
# from IPython.display import Image
# Image(graph.create_png())
示例3: test_unicode_ids
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def test_unicode_ids(self):
node1 = '"aánñoöüé€"'
node2 = '"îôø®çßΩ"'
g = pydotplus.Dot()
g.set_charset('latin1')
g.add_node(pydotplus.Node(node1))
g.add_node(pydotplus.Node(node2))
g.add_edge(pydotplus.Edge(node1, node2))
self.assertEqual(g.get_node(node1)[0].get_name(), node1)
self.assertEqual(g.get_node(node2)[0].get_name(), node2)
self.assertEqual(g.get_edges()[0].get_source(), node1)
self.assertEqual(g.get_edges()[0].get_destination(), node2)
g2 = pydotplus.graph_from_dot_data(g.to_string())
self.assertEqual(g2.get_node(node1)[0].get_name(), node1)
self.assertEqual(g2.get_node(node2)[0].get_name(), node2)
self.assertEqual(g2.get_edges()[0].get_source(), node1)
self.assertEqual(g2.get_edges()[0].get_destination(), node2)
示例4: read_dot
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def read_dot(path):
"""Return a NetworkX MultiGraph or MultiDiGraph from a dot file on path.
Parameters
----------
path : filename or file handle
Returns
-------
G : NetworkX multigraph
A MultiGraph or MultiDiGraph.
Notes
-----
Use G = nx.Graph(read_dot(path)) to return a Graph instead of a MultiGraph.
"""
import pydotplus
data = path.read()
P = pydotplus.graph_from_dot_data(data)
return from_pydot(P)
示例5: visualize
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def visualize(tree, save_path):
"""Generate PDF of a decision tree.
@param tree: DecisionTreeClassifier instance
@param save_path: string
where to save tree PDF
"""
dot_data = export_graphviz(tree, out_file=None,
filled=True, rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)
graph = make_graph_minimal(graph) # remove extra text
if not save_path is None:
graph.write_pdf(save_path)
示例6: test_attribute_with_implicit_value
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def test_attribute_with_implicit_value(self):
d = 'digraph {\na -> b[label="hi", decorate];\n}'
g = pydotplus.graph_from_dot_data(d)
attrs = g.get_edges()[0].get_attributes()
self.assertEqual('decorate' in attrs, True)
示例7: test_graph_with_shapefiles
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def test_graph_with_shapefiles(self):
shapefile_dir = os.path.join(TEST_DIR, 'from-past-to-future')
dot_file = os.path.join(shapefile_dir, 'from-past-to-future.dot')
pngs = [
os.path.join(shapefile_dir, fname)
for fname in os.listdir(shapefile_dir)
if fname.endswith('.png')
]
f = open(dot_file, 'rt')
graph_data = f.read()
f.close()
g = pydotplus.graph_from_dot_data(graph_data)
g.set_shape_files(pngs)
jpe_data = g.create(format='jpe')
hexdigest = sha256(jpe_data).hexdigest()
hexdigest_original = self._render_with_graphviz(dot_file)
self.assertEqual(hexdigest, hexdigest_original)
示例8: test_multiple_graphs
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def test_multiple_graphs(self):
graph_data = 'graph A { a->b };\ngraph B {c->d}'
graphs = pydotplus.graph_from_dot_data(graph_data)
self.assertEqual(len(graphs), 2)
self.assertEqual([g.get_name() for g in graphs], ['A', 'B'])
示例9: dt_classification
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def dt_classification():
iris = datasets.load_iris()
X = iris.data[:, 0:2]
y = iris.target
clf = tree.DecisionTreeClassifier()
clf.fit(X, y)
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True, rounded=True,
special_characters=True
)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_png("./tree_iris.png")
# plot result
xmin, xmax = X[:, 0].min() - 1, X[:, 0].max() + 1
ymin, ymax = X[:, 1].min() - 1, X[:, 1].max() + 1
plot_step = 0.02
xx, yy = np.meshgrid(np.arange(xmin, xmax, plot_step),
np.arange(ymin, ymax, plot_step))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
# Plot the training points
n_classes = 3
plot_colors = "bry"
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color,
label=iris.target_names[i],
cmap=plt.cm.Paired)
示例10: train_tree_classifer
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def train_tree_classifer(features, labels, model_output_path):
"""
train_tree_classifer will train a DecisionTree and write it out to a pdf file
features: 2D array of each input feature for each sample
labels: array of string labels classifying each sample
model_output_path: path for storing the trained tree model
"""
# save 20% of data for performance evaluation
X_train, X_test, y_train, y_test = cross_validation.train_test_split(features, labels, test_size=0.2)
param = [
{
"max_depth": [None, 10, 100, 1000, 10000]
}
]
dtree = tree.DecisionTreeClassifier(random_state=0)
# 10-fold cross validation, use 4 thread as each fold and each parameter set can be train in parallel
clf = grid_search.GridSearchCV(dtree, param,
cv=10, n_jobs=20, verbose=3)
clf.fit(X_train, y_train)
if os.path.exists(model_output_path):
joblib.dump(clf.best_estimator_, model_output_path)
else:
print("Cannot save trained tree model to {0}.".format(model_output_path))
dot_data = tree.export_graphviz(clf.best_estimator_, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf('best_tree.pdf')
print("\nBest parameters set:")
print(clf.best_params_)
y_predict=clf.predict(X_test)
labels=sorted(list(set(labels)))
print("\nConfusion matrix:")
print("Labels: {0}\n".format(",".join(labels)))
print(confusion_matrix(y_test, y_predict, labels=labels))
print("\nClassification report:")
print(classification_report(y_test, y_predict))
示例11: pydot_layout
# 需要导入模块: import pydotplus [as 别名]
# 或者: from pydotplus import graph_from_dot_data [as 别名]
def pydot_layout(G,prog='neato',root=None, **kwds):
"""Create node positions using Pydot and Graphviz.
Returns a dictionary of positions keyed by node.
Examples
--------
>>> G = nx.complete_graph(4)
>>> pos = nx.nx_pydot.pydot_layout(G)
>>> pos = nx.nx_pydot.pydot_layout(G, prog='dot')
"""
import pydotplus
P=to_pydot(G)
if root is not None :
P.set("root",make_str(root))
D=P.create_dot(prog=prog)
if D=="": # no data returned
print("Graphviz layout with %s failed"%(prog))
print()
print("To debug what happened try:")
print("P=pydot_from_networkx(G)")
print("P.write_dot(\"file.dot\")")
print("And then run %s on file.dot"%(prog))
return
Q=pydotplus.graph_from_dot_data(D)
node_pos={}
for n in G.nodes():
pydot_node = pydotplus.Node(make_str(n)).get_name()
node=Q.get_node(pydot_node)
if isinstance(node,list):
node=node[0]
pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
if pos != None:
xx,yy=pos.split(",")
node_pos[n]=(float(xx),float(yy))
return node_pos
# fixture for nose tests