Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
import matplotlib.pyplot as plt
2
from sklearn.datasets import make_blobs
3
from sklearn.tree import DecisionTreeClassifier, export_graphviz
4
from .tools import discrete_scatter
5
from .plot_2d_separator import plot_2d_separator
6
7
8
def plot_tree_not_monotone():
9
import graphviz
10
# make a simple 2d dataset
11
X, y = make_blobs(centers=4, random_state=8)
12
y = y % 2
13
plt.figure()
14
discrete_scatter(X[:, 0], X[:, 1], y)
15
plt.legend(["Class 0", "Class 1"], loc="best")
16
17
# learn a decision tree model
18
tree = DecisionTreeClassifier(random_state=0).fit(X, y)
19
plot_2d_separator(tree, X, linestyle="dashed")
20
21
# visualize the tree
22
export_graphviz(tree, out_file="mytree.dot", impurity=False, filled=True)
23
with open("mytree.dot") as f:
24
dot_graph = f.read()
25
print("Feature importances: %s" % tree.feature_importances_)
26
return graphviz.Source(dot_graph)
27
28