-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.py
48 lines (34 loc) · 1.05 KB
/
viz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
import pydotplus
# load data
iris = load_iris()
# create test holdout
test_idx = [0, 50, 100]
# training data
train_target = np.delete(iris.target, test_idx)
train_data = np.delete(iris.data, test_idx, axis=0)
# testing data
test_target = iris.target[test_idx]
test_data = iris.data[test_idx]
# create tree
clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)
# run test
print(test_target)
print(clf.predict(test_data))
# viz code
from sklearn.externals.six import StringIO
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data, feature_names=iris.feature_names, class_names=iris.target_names, filled=True, rounded=True, impurity=False)
# create PDF
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
# writing pdf takes a lot of time, commenting it out for rest of scriptiong
#graph.write_pdf('iris.pdf')
#print test data
print(test_data[1], test_target[1])
# print feature names
print(iris.feature_names)
print(iris.target_names)
print('end')