-
Notifications
You must be signed in to change notification settings - Fork 13
/
validation.py
263 lines (210 loc) · 11.1 KB
/
validation.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""
Description: This is our experimental code. We provide 4 test modes for experiments:
10-fold cross-validations on CBA (M1) without pruning
10-fold cross-validations on CBA (M1) with pruning
10-fold cross-validations on CBA (M2) without pruning
10-fold cross-validations on CBA (M2) with pruning
Input: the relative directory path of data file and scheme file
Output: the experimental results (similar to Table 1: Experiment Results in this paper)
Author: CBA Studio
"""
from read import read
from pre_processing import pre_process
from cba_rg import rule_generator
from cba_cb_m1 import classifier_builder_m1
from cba_cb_m1 import is_satisfy
from cba_cb_m2 import classifier_builder_m2
import time
import random
# calculate the error rate of the classifier on the dataset
def get_error_rate(classifier, dataset):
size = len(dataset)
error_number = 0
for case in dataset:
is_satisfy_value = False
for rule in classifier.rule_list:
is_satisfy_value = is_satisfy(case, rule)
if is_satisfy_value == True:
break
if is_satisfy_value == False:
if classifier.default_class != case[-1]:
error_number += 1
return error_number / size
# 10-fold cross-validations on CBA (M1) without pruning
def cross_validate_m1_without_prune(data_path, scheme_path, minsup=0.01, minconf=0.5):
data, attributes, value_type = read(data_path, scheme_path)
random.shuffle(data)
dataset = pre_process(data, attributes, value_type)
block_size = int(len(dataset) / 10)
split_point = [k * block_size for k in range(0, 10)]
split_point.append(len(dataset))
cba_rg_total_runtime = 0
cba_cb_total_runtime = 0
total_car_number = 0
total_classifier_rule_num = 0
error_total_rate = 0
for k in range(len(split_point)-1):
print("\nRound %d:" % k)
training_dataset = dataset[:split_point[k]] + dataset[split_point[k+1]:]
test_dataset = dataset[split_point[k]:split_point[k+1]]
start_time = time.time()
cars = rule_generator(training_dataset, minsup, minconf)
end_time = time.time()
cba_rg_runtime = end_time - start_time
cba_rg_total_runtime += cba_rg_runtime
start_time = time.time()
classifier_m1 = classifier_builder_m1(cars, training_dataset)
end_time = time.time()
cba_cb_runtime = end_time - start_time
cba_cb_total_runtime += cba_cb_runtime
error_rate = get_error_rate(classifier_m1, test_dataset)
error_total_rate += error_rate
total_car_number += len(cars.rules)
total_classifier_rule_num += len(classifier_m1.rule_list)
print("CBA's error rate without pruning: %.1lf%%" % (error_rate * 100))
print("No. of CARs without pruning: %d" % len(cars.rules))
print("CBA-RG's run time without pruning: %.2lf s" % cba_rg_runtime)
print("CBA-CB M1's run time without pruning: %.2lf s" % cba_cb_runtime)
print("No. of rules in classifier of CBA-CB M1 without pruning: %d" % len(classifier_m1.rule_list))
print("\nAverage CBA's error rate without pruning: %.1lf%%" % (error_total_rate / 10 * 100))
print("Average No. of CARs without pruning: %d" % int(total_car_number / 10))
print("Average CBA-RG's run time without pruning: %.2lf s" % (cba_rg_total_runtime / 10))
print("Average CBA-CB M1's run time without pruning: %.2lf s" % (cba_cb_total_runtime / 10))
print("Average No. of rules in classifier of CBA-CB M1 without pruning: %d" % int(total_classifier_rule_num / 10))
# 10-fold cross-validations on CBA (M1) with pruning
def cross_validate_m1_with_prune(data_path, scheme_path, minsup=0.01, minconf=0.5):
data, attributes, value_type = read(data_path, scheme_path)
random.shuffle(data)
dataset = pre_process(data, attributes, value_type)
block_size = int(len(dataset) / 10)
split_point = [k * block_size for k in range(0, 10)]
split_point.append(len(dataset))
cba_rg_total_runtime = 0
cba_cb_total_runtime = 0
total_car_number = 0
total_classifier_rule_num = 0
error_total_rate = 0
for k in range(len(split_point)-1):
print("\nRound %d:" % k)
training_dataset = dataset[:split_point[k]] + dataset[split_point[k+1]:]
test_dataset = dataset[split_point[k]:split_point[k+1]]
start_time = time.time()
cars = rule_generator(training_dataset, minsup, minconf)
cars.prune_rules(training_dataset)
cars.rules = cars.pruned_rules
end_time = time.time()
cba_rg_runtime = end_time - start_time
cba_rg_total_runtime += cba_rg_runtime
start_time = time.time()
classifier_m1 = classifier_builder_m1(cars, training_dataset)
end_time = time.time()
cba_cb_runtime = end_time - start_time
cba_cb_total_runtime += cba_cb_runtime
error_rate = get_error_rate(classifier_m1, test_dataset)
error_total_rate += error_rate
total_car_number += len(cars.rules)
total_classifier_rule_num += len(classifier_m1.rule_list)
print("CBA's error rate with pruning: %.1lf%%" % (error_rate * 100))
print("No. of CARs with pruning: %d" % len(cars.rules))
print("CBA-RG's run time with pruning: %.2lf s" % cba_rg_runtime)
print("CBA-CB M1's run time with pruning: %.2lf s" % cba_cb_runtime)
print("No. of rules in classifier of CBA-CB M1 with pruning: %d" % len(classifier_m1.rule_list))
print("\nAverage CBA's error rate with pruning: %.1lf%%" % (error_total_rate / 10 * 100))
print("Average No. of CARs with pruning: %d" % int(total_car_number / 10))
print("Average CBA-RG's run time with pruning: %.2lf s" % (cba_rg_total_runtime / 10))
print("Average CBA-CB M1's run time with pruning: %.2lf s" % (cba_cb_total_runtime / 10))
print("Average No. of rules in classifier of CBA-CB M1 with pruning: %d" % int(total_classifier_rule_num / 10))
# 10-fold cross-validations on CBA (M2) without pruning
def cross_validate_m2_without_prune(data_path, scheme_path, minsup=0.01, minconf=0.5):
data, attributes, value_type = read(data_path, scheme_path)
random.shuffle(data)
dataset = pre_process(data, attributes, value_type)
block_size = int(len(dataset) / 10)
split_point = [k * block_size for k in range(0, 10)]
split_point.append(len(dataset))
cba_rg_total_runtime = 0
cba_cb_total_runtime = 0
total_car_number = 0
total_classifier_rule_num = 0
error_total_rate = 0
for k in range(len(split_point)-1):
print("\nRound %d:" % k)
training_dataset = dataset[:split_point[k]] + dataset[split_point[k+1]:]
test_dataset = dataset[split_point[k]:split_point[k+1]]
start_time = time.time()
cars = rule_generator(training_dataset, minsup, minconf)
end_time = time.time()
cba_rg_runtime = end_time - start_time
cba_rg_total_runtime += cba_rg_runtime
start_time = time.time()
classifier_m2 = classifier_builder_m2(cars, training_dataset)
end_time = time.time()
cba_cb_runtime = end_time - start_time
cba_cb_total_runtime += cba_cb_runtime
error_rate = get_error_rate(classifier_m2, test_dataset)
error_total_rate += error_rate
total_car_number += len(cars.rules)
total_classifier_rule_num += len(classifier_m2.rule_list)
print("CBA's error rate without pruning: %.1lf%%" % (error_rate * 100))
print("No. of CARs without pruning: %d" % len(cars.rules))
print("CBA-RG's run time without pruning: %.2lf s" % cba_rg_runtime)
print("CBA-CB M2's run time without pruning: %.2lf s" % cba_cb_runtime)
print("No. of rules in classifier of CBA-CB M2 without pruning: %d" % len(classifier_m2.rule_list))
print("\nAverage CBA's error rate without pruning: %.1lf%%" % (error_total_rate / 10 * 100))
print("Average No. of CARs without pruning: %d" % int(total_car_number / 10))
print("Average CBA-RG's run time without pruning: %.2lf s" % (cba_rg_total_runtime / 10))
print("Average CBA-CB M2's run time without pruning: %.2lf s" % (cba_cb_total_runtime / 10))
print("Average No. of rules in classifier of CBA-CB M2 without pruning: %d" % int(total_classifier_rule_num / 10))
# 10-fold cross-validations on CBA (M2) with pruning
def cross_validate_m2_with_prune(data_path, scheme_path, minsup=0.01, minconf=0.5):
data, attributes, value_type = read(data_path, scheme_path)
random.shuffle(data)
dataset = pre_process(data, attributes, value_type)
block_size = int(len(dataset) / 10)
split_point = [k * block_size for k in range(0, 10)]
split_point.append(len(dataset))
cba_rg_total_runtime = 0
cba_cb_total_runtime = 0
total_car_number = 0
total_classifier_rule_num = 0
error_total_rate = 0
for k in range(len(split_point)-1):
print("\nRound %d:" % k)
training_dataset = dataset[:split_point[k]] + dataset[split_point[k+1]:]
test_dataset = dataset[split_point[k]:split_point[k+1]]
start_time = time.time()
cars = rule_generator(training_dataset, minsup, minconf)
cars.prune_rules(training_dataset)
cars.rules = cars.pruned_rules
end_time = time.time()
cba_rg_runtime = end_time - start_time
cba_rg_total_runtime += cba_rg_runtime
start_time = time.time()
classifier_m2 = classifier_builder_m2(cars, training_dataset)
end_time = time.time()
cba_cb_runtime = end_time - start_time
cba_cb_total_runtime += cba_cb_runtime
error_rate = get_error_rate(classifier_m2, test_dataset)
error_total_rate += error_rate
total_car_number += len(cars.rules)
total_classifier_rule_num += len(classifier_m2.rule_list)
print("CBA's error rate with pruning: %.1lf%%" % (error_rate * 100))
print("No. of CARs without pruning: %d" % len(cars.rules))
print("CBA-RG's run time with pruning: %.2lf s" % cba_rg_runtime)
print("CBA-CB M2's run time with pruning: %.2lf s" % cba_cb_runtime)
print("No. of rules in classifier of CBA-CB M2 with pruning: %d" % len(classifier_m2.rule_list))
print("\nAverage CBA's error rate with pruning: %.1lf%%" % (error_total_rate / 10 * 100))
print("Average No. of CARs with pruning: %d" % int(total_car_number / 10))
print("Average CBA-RG's run time with pruning: %.2lf s" % (cba_rg_total_runtime / 10))
print("Average CBA-CB M2's run time with pruning: %.2lf s" % (cba_cb_total_runtime / 10))
print("Average No. of rules in classifier of CBA-CB M2 with pruning: %d" % int(total_classifier_rule_num / 10))
# test entry goes here
if __name__ == "__main__":
# using the relative path, all data sets are stored in datasets directory
test_data_path = 'datasets/iris.data'
test_scheme_path = 'datasets/iris.names'
# just choose one mode to experiment by removing one line comment and running
cross_validate_m1_without_prune(test_data_path, test_scheme_path)
# cross_validate_m1_with_prune(test_data_path, test_scheme_path)
# cross_validate_m2_without_prune(test_data_path, test_scheme_path)
# cross_validate_m1_with_prune(test_data_path, test_scheme_path)