-
Notifications
You must be signed in to change notification settings - Fork 0
/
decisionTree.py
73 lines (63 loc) · 1.82 KB
/
decisionTree.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# X = [
# ['Young', 'No Work', 'No House', 'Credit'],
# ['Young', 'No Work', 'No House', 'Good Credit'],
# ['Young', 'Have Work', 'No House', 'Good Credit'],
# ['Young', 'Have Work', 'Have House', 'Credit'],
# ['Young', 'No Work', 'No House', 'Credit'],
#
# ['Middle age', 'No Work', 'No House', 'Credit'],
# ['Middle age', 'No Work', 'No House', 'Good Credit'],
# ['Middle age', 'Yes Work', 'Have House', 'Good Credit'],
# ['Middle age', 'No Work', 'Have House', 'Very Good Credit'],
# ['Middle age', 'No Work', 'Have House', 'Very Good Credit'],
#
# ['Old age', 'No Work', 'Have House', 'Very Good Credit'],
# ['Old age', 'No Work', 'Have House', 'Good Credit'],
# ['Old age', 'Have Work', 'No House', 'Good Credit'],
# ['Old age', 'Have Work', 'No House', 'Very Good Credit'],
# ['Old age', 'No Work', 'No House', 'Credit'],
# ]
#
X = [
[1, 0, 0, 1],
[1, 0, 0, 2],
[1, 1, 0, 2],
[1, 1, 1, 1],
[1, 0, 0, 1],
[2, 0, 0, 1],
[2, 0, 0, 2],
[2, 1, 1, 2],
[2, 0, 1, 3],
[2, 0, 1, 3],
[3, 0, 1, 3],
[3, 0, 1, 2],
[3, 1, 0, 2],
[3, 1, 0, 3],
[3, 0, 0, 1],
]
Y = [0, 0,1,1,0,0,0,1,1,1,1,1,1,1,0]
#
# from sklearn import tree
# import matplotlib.pyplot as plt
# clf = tree.DecisionTreeClassifier()
# clf.fit(X,Y)
#
# tree.plot_tree(clf)
# plt.show()
import xgboost as xgb
import numpy as np
import matplotlib.pyplot as plt
dtest = np.array([[1, 0, 1, 1], [2, 0, 1, 3]])
model = xgb.XGBClassifier()
model.fit(np.array(X), Y)
preds = model.predict(dtest)
print(preds)
dtrain = xgb.DMatrix(np.array(X), label=np.array(Y))
dtest = xgb.DMatrix(dtest)
params = {'max_depth': 2, 'eta': 1, 'objective': 'binary:logistic'}
bst = xgb.train(params, dtrain, 2)
result = bst.predict(dtest)
print(result)
xgb.plot_tree(model)
xgb.plot_tree(bst)
plt.show()