-
Notifications
You must be signed in to change notification settings - Fork 0
/
irisjustglowingpython.py
55 lines (38 loc) · 1.25 KB
/
irisjustglowingpython.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
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 5 14:48:53 2020
@author: Gold
"""
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import graphviz
#from dtreeviz.trees import dtreeviz
iris=datasets.load_iris()
x=iris.data
y=iris.target
print(x[:9])
print(y[17:30])
clf=DecisionTreeClassifier(random_state=1234)
clf.fit(x,y)
#print (clf)
text_rep=tree.export_text(clf)
#print(text_rep)
#to save the text_rep to text, do the following
#with open("decision_tree.log","w") as fout:
# fout.write(text_rep)
#To plot the tree
fig=plt.figure(figsize=(25,20))
graph=tree.plot_tree(clf,feature_names=iris.feature_names, class_names=iris.target_names, filled=True)
#To save the image to png
#fig.savefig("decision_tree.png")
#using graphviz to visualize the tree
#dot_dat=tree.export_graphviz(clf,out_file=None,feature_names=iris.feature_names,class_names=iris.target_names, filled=True)
#graph=graphviz.Source(dot_dat, format="png")
#graph
#plotting using dtreeviz
#viz=dtreeviz(clf,x,y,target_names="target", feature_names=iris.feature_names, class_names=list(iris.target_names))
#viz
accuracy=clf.score(x, y)
print (accuracy)