-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecisionTree.py
68 lines (60 loc) · 1.33 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
from sklearn import tree
from sklearn.model_selection import train_test_split
import numpy as np
# 9个女孩和8只猫的数据,对应7个feature,yes取值为1,no为0
features = np.array([
[1, 1, 0, 0, 1, 0, 1],
[1, 1, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 1],
[1, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0],
[0, 0, 1, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1, 0]
])
# 1 表示是女孩,0表示是猫
labels = np.array([
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
])
# 从数据集中取20%作为测试集,其他作为训练集
X_train, X_test, y_train, y_test = train_test_split(
features,
labels,
test_size=0.2,
random_state=0,
)
# 训练分类树模型
clf = tree.DecisionTreeClassifier()
clf.fit(X=X_train, y=y_train)
# 测试
print(clf.predict(X_test))
# 对比测试结果和预期结果
print(clf.score(X=X_test, y=y_test))
# 预测HelloKitty
HelloKitty = np.array([[1, 1, 1, 1, 1, 1, 1]])
print(clf.predict(HelloKitty))