-
Notifications
You must be signed in to change notification settings - Fork 1
/
svm_experiment_userdata.py
146 lines (106 loc) · 4.15 KB
/
svm_experiment_userdata.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
from preprocessing.reader import EvalitaDatasetReader, read_emoji_dist
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from utils.fileprovider import FileProvider
from scipy.sparse import csr_matrix, hstack
from operator import itemgetter
import argparse
import string
import re
import logging
import numpy as np
logging.getLogger().setLevel(logging.INFO)
def process_text(texts, exclude=set(string.punctuation)):
res = []
for text in texts:
text = re.sub(r'http\S+', '', text)
text = text.lower()
res.append(''.join(ch for ch in text if ch not in exclude))
return res
def normalize(val, min_val, max_val):
num = val - min_val
den = max_val - min_val
res = num /(float(den))
return res
parser = argparse.ArgumentParser(description='Train the emoji task')
parser.add_argument('--workdir', required=False, help='Work path', default='data')
parser.add_argument('--max-dict', type=int, default=100000, help='Maximum dictionary size')
parser.add_argument('--use-history', choices=["train", "userdata"], help='Use user history to assist prediction', default='userdata')
args = parser.parse_args()
files = FileProvider(args.workdir)
logging.info("Collecting tweets")
raw_train, raw_test = EvalitaDatasetReader(files.evalita).split()
logging.info("Collecting user data")
user_data = None
if args.use_history:
if args.use_history == "userdata":
user_data, user_data_size = read_emoji_dist(files.evalita_emoji_dist)
user_data_size = len(user_data_size)
else:
user_data = {}
user_data_size = len(raw_train.Y_dictionary)
for i in range(len(raw_train.Y)):
uid = raw_train.X[i][1]
if uid not in user_data:
user_data[uid] = np.zeros([len(raw_train.Y_dictionary)], dtype=np.float16)
user_data[uid][raw_train.Y[i]] += 1
logging.info("Normalizing")
minimum = user_data[raw_train.X[0][1]][0]
maximum = user_data[raw_train.X[0][1]][0]
for key, value in user_data.items():
for elem in value:
if elem < minimum:
minimum = elem
if elem > maximum:
maximum = elem
for key, value in user_data.items():
temp_list = []
for elem in value:
temp_list.append(normalize(elem, minimum, maximum))
new_vector = np.array(temp_list)
user_data[key] = new_vector
logging.info("Extracting data")
texts_train = []
texts_test = []
labels_train = []
labels_test = []
user_ids_train = []
user_ids_test = []
for elem in raw_train.X:
texts_train.append(elem[0])
user_ids_train.append(elem[1])
for elem in raw_test.X:
texts_test.append(elem[0])
user_ids_test.append(elem[1])
for elem in raw_train.Y:
labels_train.append(elem)
for elem in raw_test.Y:
labels_test.append(elem)
del raw_train
texts_train = process_text(texts_train)
texts_test = process_text(texts_test)
vectorizer = TfidfVectorizer()
logging.info("Vectorizing")
vectorizer.fit(texts_train)
tfidf_matrix_train = vectorizer.transform(texts_train)
tfidf_matrix_test = vectorizer.transform(texts_test)
del texts_train
del texts_test
train_user_matrix = csr_matrix(list(itemgetter(*user_ids_train)(user_data)))
test_user_matrix = csr_matrix(list(itemgetter(*user_ids_test)(user_data)))
complete_matrix_train = csr_matrix(hstack([tfidf_matrix_train, train_user_matrix]))
complete_matrix_test = csr_matrix(hstack([tfidf_matrix_test, test_user_matrix]))
logging.info("Fitting")
clf = SVC(verbose=True)
clf.fit(complete_matrix_train, labels_train)
logging.info("Predicting")
prediction = clf.predict(complete_matrix_test)
logging.info("Dumping scores")
scores_file = open('scores_file_userdata.txt', 'w')
scores_file.write('Accuracy: ' + str(accuracy_score(prediction, labels_test)) + '\n')
scores_file.write('Precision: ' + str(precision_score(prediction, labels_test, average='macro')) + '\n')
scores_file.write('Recall: ' + str(recall_score(prediction, labels_test, average='macro')) + '\n')
scores_file.write('F1-score: ' + str(f1_score(prediction, labels_test, average='macro')) + '\n')
scores_file.close()
logging.info("Done!")