-
Notifications
You must be signed in to change notification settings - Fork 78
/
RF_GBDT_test.py
175 lines (148 loc) · 5.16 KB
/
RF_GBDT_test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# coding = <utf-8>
'''
@author: PY131
'''
# for visualization
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
# for data
from sklearn.datasets import load_iris, load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
# for classifier model
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
########## 1. getting data set ##########
ds_iris = load_iris()
ds_breast_cancer = load_breast_cancer()
datasets = [[ds_iris.data, ds_iris.target],
[ds_breast_cancer.data, ds_breast_cancer.target]]
datasets_names = ['iris',
'breast_cancer']
fig_num = 0
# draw sactter
f1 = plt.figure(fig_num)
for i, ds in enumerate(datasets):
X, y = ds
cm_bright = ListedColormap(['r', 'b', 'g'])
ax = plt.subplot(1, 2, i+1)
ax.set_title(datasets_names[i])
ax.scatter(X[:, 0], X[:, 1], c=y, cmap=cm_bright, edgecolors='k')
plt.show()
########## 2. training and testing ##########
##### 2.1 Random Forest #####
### 2.1.1 Parameter testing: the different max_depth for each based tree
fig_num +=1
f2 = plt.figure(fig_num)
for i, ds in enumerate(datasets):
#split the data set
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
x = []
scores = []
for md in range(1, 50, 1):
x.append(md)
# test
clf = RandomForestClassifier(n_estimators=10,
max_depth=md,
max_features='log2',
bootstrap=True)
# training and testing
clf.fit(X_train, y_train)
scores.append(clf.score(X_test, y_test))
# plot the result as
ax = plt.subplot(1, len(datasets), i+1)
ax.set_title('RF for %s' % datasets_names[i])
ax.set_xlabel('max_depth')
ax.set_ylabel('accuracy_scores')
ax.set_ylim([0, 1])
ax.plot(x, scores)
plt.show()
### 2.1.2 Parameter testing: the different n_estimators for each based tree
fig_num += 1
f = plt.figure(fig_num)
for i, ds in enumerate(datasets):
#split the data set
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
x = []
scores = []
for nt in range(1, 200, 1):
x.append(nt)
# test
clf = RandomForestClassifier(n_estimators=nt,
max_depth=2,
max_features='log2',
bootstrap=True)
# training and testing
clf.fit(X_train, y_train)
scores.append(clf.score(X_test, y_test))
# plot the result as
ax = plt.subplot(1, len(datasets), i+1)
ax.set_title('RF for %s' % datasets_names[i])
ax.set_xlabel('n_estimators')
ax.set_ylabel('accuracy_scores')
ax.set_ylim([0, 1])
ax.plot(x, scores)
plt.show()
##### 2.2 GBDT #####
### 2.2.1 Parameter testing: the different max_depth for each base tree
fig_num += 1
f = plt.figure(fig_num)
for i, ds in enumerate(datasets):
#split the data set
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
x = []
scores = []
for md in range(1, 20, 1):
x.append(md)
clf = GradientBoostingClassifier(n_estimators=50,
max_depth=md,
learning_rate=0.01,
subsample=0.5,
min_samples_leaf=1)
# training and testing
clf.fit(X_train, y_train)
scores.append(clf.score(X_test, y_test))
# plot the result as
ax = plt.subplot(1, len(datasets), i+1)
ax.set_title('GBDT for %s' % datasets_names[i])
ax.set_xlabel('max_depth')
ax.set_ylabel('accuracy_scores')
ax.set_ylim([0, 1])
ax.plot(x, scores)
plt.show()
### 2.2.2 Parameter testing: the different n_estimators for iterative epochs
fig_num += 1
f = plt.figure(fig_num)
for i, ds in enumerate(datasets):
#split the data set
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
x = []
scores = []
for nt in range(1, 100, 1):
x.append(nt)
clf = GradientBoostingClassifier(n_estimators=nt,
max_depth=1,
learning_rate=0.01,
subsample=0.5,
min_samples_leaf=1)
# training and testing
clf.fit(X_train, y_train)
scores.append(clf.score(X_test, y_test))
# plot the result as
ax = plt.subplot(1, len(datasets), i+1)
ax.set_title('GBDT for %s' % datasets_names[i])
ax.set_xlabel('n_estimators')
ax.set_ylabel('accuracy_scores')
ax.set_ylim([0, 1])
ax.plot(x, scores)
plt.show()
print(' - PY131 -')