forked from csuwu/Machine-Learning-Basic-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTreeSimple.py
More file actions
25 lines (18 loc) · 774 Bytes
/
Copy pathDecisionTreeSimple.py
File metadata and controls
25 lines (18 loc) · 774 Bytes
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
from sklearn import tree
import pydotplus
'''
0 : sunny, 1 : windy, 2 : rainy
1 : yes, 0 : no
0 : poor, 1: rich
'''
clf = tree.DecisionTreeClassifier(criterion="entropy",min_samples_split=2)
X=[[0,1,1],[0,0,1],[1,1,1],[2,1,0],[2,0,1],[2,1,0],[1,0,0],[1,0,1],[1,1,1],[0,0,1]]
Y=["Cinema","Tennis","Cinema","Cinema","Stay in","Cinema","Cinema","Shopping","Cinema","Tennis"]
clf=clf.fit(X,Y)
featureNames=["Weather","Parents","Money"]
classNames=["Cinema","Shopping","Stay in","Tennis"]
dot_data=tree.export_graphviz(clf, out_file=None,feature_names=featureNames,class_names=classNames)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("DecisionTreeSimple.pdf")
print(clf.classes_)
print(clf.predict([[1,0,1]]))