forked from officialgupta/MachineLearningRecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.py
42 lines (31 loc) · 1.11 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
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
import pydotplus
# COLLECT TRAINING DATA
iris = load_iris()
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]
# TRAIN CLASSIFIER
clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)
# MAKE PREDICTIONS
print(test_target) #expected outcome
print(clf.predict(test_data)) #model predicted outcome
# VISUALISE THE TREE
# non-coloured version
# dot_data = tree.export_graphviz(clf, out_file=None)
# graph = pydotplus.graph_from_dot_data(dot_data)
# graph.write_pdf("iris.pdf")
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_pdf("irisColoured.pdf")