-
Notifications
You must be signed in to change notification settings - Fork 0
/
决策树分类预测.py
94 lines (73 loc) · 2.73 KB
/
决策树分类预测.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
tree_clf = DecisionTreeClassifier()
x_test = np.array([[1,1],[2,2],[0,3],[4,5],[6,8],[4,6]])
y_test = np.array([1,1,0,1,1,0])
tree_clf_class = tree_clf.fit(x_test,y_test)
plt.scatter(x_test[:,0],x_test[:,1],c=y_test)
x_predict = np.array([[2,3]])
y_label_predict = tree_clf.predict(x_predict)
print(y_label_predict)
plt.scatter(x_predict[:,0],x_predict[:,1],c=y_label_predict)
plt.show()
#三维决策树
x_train_3D = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]])
y_train_label = np.array([1,1,1,0,0])
#开始训练
tree_classify_3D = tree_clf.fit(x_train_3D,y_train_label)
#开始模型预测
x_test_3D = np.array([[5,6,4]])
tree_classify_test = tree_clf.predict(x_test_3D)
print(tree_classify_test)
#三维画图
x_data = x_train_3D[:,0]
y_data = x_train_3D[:,1]
z_data = x_train_3D[:,2]
ax3 = plt.axes(projection='3d')
ax3.scatter(x_data,y_data,z_data,c=y_train_label)
ax3.scatter(x_test_3D[:,0],x_test_3D[:,1],x_test_3D[:,2],color='red')
plt.show()
#高纬度特征决策树
d_x_test = np.array([[1,1,1,1,1,1],
[2,1,2,1,1,1],
[2,1,1,1,1,1],
[1,1,2,1,1,1],
[3,1,1,1,1,1],
[1,2,1,1,2,2],
[4,2,1,1,2,2],
[4,2,1,1,2,1],
[4,2,2,1,2,1],
[1,3,3,3,3,2],
[3,3,3,2,3,1],
[3,1,2,2,3,2],
[1,2,2,3,1,1],
[3,2,2,3,1,1],
[2,2,1,1,2,2],
[3,1,1,2,3,1],
[1,1,2,2,2,1]])
d_y_test_label = np.array([1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0])
plt.scatter(d_x_test[:,0],d_x_test[:,1],c=d_y_test_label)
plt.show()
tree_model_test1 = tree_clf.fit(d_x_test,d_y_test_label)
x_test_111 = np.array([[2,4,3,2,4,5]])
new_predict = tree_clf.predict(x_test_111)
print(new_predict)
from sklearn import tree
import graphviz
dot_data = tree.export_graphviz(tree_model_test1,out_file=None)
graph = graphviz.Source(dot_data)
graph.render('决策树画图',view=False)
#机器学习分类
from sklearn.datasets import make_blobs
np.random.seed(48782)
X, y = make_blobs(n_samples=1000, n_features=1, centers=[[-1,-1], [1,1]], cluster_std=[0.4, 0.5])
plt.scatter(X[:, 0], X[:, 1],c=y)
plt.title('make-blobs')
plt.show()
x_data_1_d = X
y_1_d_label = y
tree_model_test2 = tree_clf.fit(x_data_1_d,y_1_d_label)
dot_data_ML = tree.export_graphviz(tree_model_test2,out_file=None)
graph_ML = graphviz.Source(dot_data_ML)
graph_ML.render('机器学习决策树画图sklearn',view=True)