-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
80 lines (71 loc) · 2.88 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
from argparse import ArgumentParser
import math
import os
from sklearn.metrics import accuracy_score, confusion_matrix, mean_squared_error
from pan import ProfilingDataset
from sklearn.externals import joblib
import pprint
def test_data(dataset, model, task):
""" evaluate model on test data
:dataset: The dataset to evaluate model on
:model: The trained model to use for prediction
:task: The task this is for
"""
X, y = dataset.get_data(feature=task)
predict = model.predict(X)
print('\n-- Predictions for %s --' % task)
try:
# if it's classification we measure micro and macro scores
acc = accuracy_score(y, predict)
conf = confusion_matrix(y, predict, labels=list(set(y)))
print('Accuracy : {}'.format(acc))
print('Confusion matrix :\n {}'.format(conf))
except ValueError:
# if it's not, we measure mean square root error (regression)
sqe = mean_squared_error(y, predict)
print('mean squared error : {}'.format(math.sqrt(sqe)))
# import pprint
# pprint.pprint(predict)
dataset.set_labels(task, predict)
if __name__ == '__main__':
parser = ArgumentParser(description='Test trained model on pan dataset')
parser.add_argument('-i', '--input', type=str,
required=True, dest='infolder',
help='path to folder with pan dataset for a language')
parser.add_argument('-o', '--output', type=str,
required=True, dest='outfolder',
help='path to folder where results should be written')
parser.add_argument('-m', '--model', type=str,
required=True, dest='model',
help='path to learned model to use for predictions')
args = parser.parse_args()
model = args.model
infolder = args.infolder
outfolder = args.outfolder
print('Loading dataset...')
dataset = ProfilingDataset(infolder)
print('Loaded {} users...\n'.format(len(dataset.entries)))
config = dataset.config
tasks = config.tasks
# This part for tira-io
modelfile = os.path.join(model, '%s.bin' % dataset.lang)
print('InputRun: %s' % model)
print('ModelPath: %s' % modelfile)
#pprint.pprint('Directory:')
#pprint.pprint(os.listdir(model))
#all_models = joblib.load(modelfile)
######
# This part for home usage
all_models = joblib.load(model)
######
if not all(task in tasks for task in all_models.keys()):
print("The models you are using aren't all specified in config file")
print('Did you change the config file after training???!')
print('Exiting.. try training again.')
exit(1)
print('\n--------------- Thy time of Judgement ---------------')
for task in tasks:
test_data(dataset, all_models[task], task)
# write output to file
dataset.write_data(outfolder)