-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmturk_classifier_agreement_labels.py
67 lines (48 loc) · 2.42 KB
/
mturk_classifier_agreement_labels.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
import sys
import codecs
import data
from models import *
import numpy as np
from sklearn import cross_validation
from scipy.sparse import coo_matrix
class OptionAwareNaiveBayesLeftRightMTurk(OptionAwareNaiveBayesLeftRight):
def train(self, annotations, ylabels):
X = self.vectorizer.fit_transform(annotations)
self.classifier.partial_fit(X, ylabels, classes=range(10))
def get_mturk_classifier_agreement_label(mturk_vote_file_path, classifier_class, **kwargs):
# train a classifier on ambiguous annotations
ambig_annotations, labels = data.load_ambiguous_annotations_labeled(mturk_vote_file_path)
classifier = classifier_class(**kwargs)
classifier.train(ambig_annotations,labels)
# classify annotations and output the agreement
predicted_group_numbers = classifier.predict(ambig_annotations)
voted_group_numbers = [data.Annotation.GROUP_MAPPING[label] for label in labels]
agreement = [int(predicted == voted) for predicted, voted in zip(predicted_group_numbers, voted_group_numbers)]
return np.mean(agreement)
#print get_mturk_classifier_agreement_label(sys.argv[1], OptionAwareNaiveBayesLeftRightMTurk)
class CountPrinter:
def __init__(self, total):
self.total = total
self.current = 0
def count(self):
print '%s/%s' % (self.current, self.total)
self.current += 1
def crossvalidation(mturk_vote_file_path, classifier_class, n_folds = 2, verbose=False, **kwargs):
# train a classifier on ambiguous annotations
ambig_annotations, labels = data.load_ambiguous_annotations_labeled(mturk_vote_file_path)
ambig_annotations = np.array(ambig_annotations)
labels = np.array(labels)
folds = cross_validation.KFold(len(ambig_annotations), n_folds=n_folds, indices=True)
counter = CountPrinter(n_folds)
fold_errors = []
for train_indices, test_indices in folds:
if verbose: counter.count()
classifier = classifier_class(**kwargs)
classifier.train(ambig_annotations[train_indices], labels[train_indices])
predicted_group_numbers = classifier.predict(ambig_annotations[test_indices])
voted_group_numbers = [data.Annotation.GROUP_MAPPING[label] for label in labels]
agreement = [int(predicted == voted) for predicted, voted in zip(predicted_group_numbers, voted_group_numbers)]
fold_errors.append(np.mean(agreement))
return np.mean(fold_errors)
print crossvalidation(sys.argv[1], OptionAwareNaiveBayesLeftRightMTurk, n_folds=3, verbose = True)
#return np.mean(agreement)