-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.py
61 lines (51 loc) · 1.93 KB
/
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
#!/usr/bin/env python
from pyblique import error_rate, get_data, ObliqueClassifier
from sklearn.cross_validation import KFold
import argparse
import os
import sys
import time
class Tee:
def __init__(self, *args):
self.outputs = args
def __call__(self, s="", end="\n"):
for o in self.outputs:
o.write(s + end)
def run(fname, folds):
st = time.clock()
data = get_data("Data/{}.data".format(fname))
with open("Results/{}_{}folds.txt".format(fname, folds), "a") as f:
tee = Tee(sys.stdout, f)
tee("Validating classifier with {}-fold test...".format(folds))
kf = KFold(len(data), n_folds=folds)
avg_error = 0
it = 1
for train, test in kf:
start = time.clock()
tee("Iteration #{}".format(it))
oc = ObliqueClassifier()
oc.fit(data[train])
predictions = [oc.predict(r) for r in data[test]]
actual_labels = data[test][:, -1]
error = error_rate(predictions, actual_labels)
tee("Error: {:.3f}".format(error))
tee("Elapsed time: {:.3f} seconds".format(time.clock() - start))
tee()
avg_error += error
it += 1
totaltime = time.clock() - st
tee("Average error: {:.3f}".format(avg_error/folds))
tee("Total elapsed time: {:.3f} seconds.".format(totaltime))
tee("Average elapsed time: {:.3f} seconds.".format(totaltime/folds))
if __name__ == "__main__":
files = os.listdir("Data")
files = [f.split(".")[0] for f in files]
if not os.path.exists("Results"):
os.makedirs("Results")
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folds", default=5, type=int,
help="Amount of folds")
parser.add_argument("data", type=str, help="Name of dataset",
choices=files)
args = parser.parse_args()
run(args.data, args.folds)