-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate_aoa.py
318 lines (249 loc) · 10.2 KB
/
evaluate_aoa.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import argparse
import json
import os
import sys
from enum import Enum
import keras
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import spacy
import tensorflow as tf
from keras.wrappers.scikit_learn import KerasClassifier
from nltk.corpus import wordnet as wn
from scipy.stats import pearsonr
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.linear_model import LassoLarsIC, LinearRegression
from sklearn.metrics import confusion_matrix, make_scorer, mean_absolute_error
from sklearn.model_selection import (KFold, cross_val_score, cross_validate,
train_test_split)
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.svm import SVR
from sklearn.utils.class_weight import compute_class_weight
from statsmodels.stats.outliers_influence import variance_inflation_factor
from tqdm import tqdm
from utils import *
nlp = spacy.load('en_core_web_lg')
AOA_LISTS = ["Bird", "Bristol", "Cortese", "Kuperman", "Shock",
"Objective AoA (LR) (months)",
"Objective AoA (75%) (months)"]
TERM_FREQUENCIES_FILE = 'term_frequencies.json'
class FeatureSelection(Enum):
LEXICAL = 0
LEXICAL_AND_WORDNET = 1
ALL = 2
ALL_WITHOUT_WORDNET = 3
ONLY_WORD_TRAJECTORY = 4
ONLY_WORDNET = 5
def lemmatize(word):
for tok in nlp(word):
return tok.lemma_
def get_word_features(word, use_wordnet=False, use_lexical=True):
global term_frequency
if not use_wordnet:
return [
get_no_syllables(word),
len(word),
*term_frequency.get(word, [0] * 4)
]
elif not use_lexical:
return [
*get_hypnonymy_tree_sizes(word),
len(wn.synsets(word)),
]
else:
return [
get_no_syllables(word),
*get_hypnonymy_tree_sizes(word),
len(word),
len(wn.synsets(word)),
*term_frequency.get(word, [0] * 4)
]
def calculate_vif_(X, thresh=5.0, return_variables=False):
variables = list(range(X.shape[1]))
dropped = True
while dropped and len(variables) > 1:
dropped = False
vif = [variance_inflation_factor(X[:, variables], ix)
for ix in range(X[:, variables].shape[1])]
maxloc = vif.index(max(vif))
if max(vif) > thresh:
del variables[maxloc]
dropped = True
if not return_variables:
return X[:, variables]
else:
return X[:, variables], variables
def get_selected_features(data_columns, feature_selection):
features = []
if feature_selection == FeatureSelection.ALL:
features = data_columns + [
'# syllables', 'avg. hypernym eccentricities',
'avg. hyponym eccentricities', '# chars', '# synset',
'term frequency']
elif feature_selection == FeatureSelection.LEXICAL:
features = ['# syllables', '# chars', 'term frequency']
elif feature_selection == FeatureSelection.LEXICAL_AND_WORDNET:
features = ['# syllables', 'avg. hypernym eccentricities',
'avg. hyponym eccentricities', '# chars', '# synset',
'term frequency']
return features
def get_data_columns(indices):
data_columns = indices.columns[1:].tolist()
data_columns.remove('highest cosine word')
data_columns.remove('2nd highest cosine word')
data_columns.remove('3rd highest cosine word')
return data_columns
def partial_vif_analysis(indices):
new_indices = indices.copy()
template = 'continuous index above'
selected_columns = []
for col in indices.columns:
if template not in col:
continue
selected_columns.append(col)
X = np.array(indices[selected_columns])
_, filtered_col = calculate_vif_(X, return_variables=True)
print('Continuous indices filtered down to:',
', '.join([selected_columns[i] for i in filtered_col]))
for i, col in enumerate(selected_columns):
if i not in filtered_col:
del new_indices[col]
return new_indices
def get_data(aoa_scores, indices_file, feature_selection=FeatureSelection.ALL):
indices = pd.read_csv(indices_file)
indices.drop(columns='Unnamed: 0', inplace=True)
indices = partial_vif_analysis(indices)
data_columns = get_data_columns(indices)
intermediate_models = len(
[index for index in data_columns if 'intermediate' in index])
print(f"Model with {intermediate_models} intermediate steps.")
features = get_selected_features(data_columns, feature_selection)
X = []
y = []
y_words = []
for _, row in indices.iterrows():
word = row['lemmatized word']
if word not in aoa_scores:
continue
if feature_selection == FeatureSelection.ALL:
X.append(np.array(row[data_columns]).tolist() +
get_word_features(word, use_wordnet=True))
elif feature_selection == FeatureSelection.LEXICAL:
X.append(get_word_features(word, use_wordnet=False))
elif feature_selection == FeatureSelection.ALL_WITHOUT_WORDNET:
X.append(np.array(row[data_columns]).tolist() +
get_word_features(word, use_wordnet=False))
elif feature_selection == FeatureSelection.LEXICAL_AND_WORDNET:
X.append(get_word_features(word, use_wordnet=True))
elif feature_selection == FeatureSelection.ONLY_WORD_TRAJECTORY:
X.append(np.array(row[data_columns]).tolist())
elif feature_selection == FeatureSelection.ONLY_WORDNET:
X.append(get_word_features(word, use_wordnet=True, use_lexical=False))
else:
raise ValueError('Invalid feature set:', feature_selection)
y.append(aoa_scores[word])
y_words.append(word)
print(f'{len(X)} / {len(aoa_scores)} words are used from kuperman list')
X = np.nan_to_num(np.array(X))
y = np.array(y)
print('Using', X.shape[1], 'features')
return X, y
def evaluate_model(X, y, make_model):
cv_mae = -cross_val_score(make_model(), X, y,
scoring='neg_mean_absolute_error',
cv=KFold(10, shuffle=True))
print('MAE:', np.mean(cv_mae), 'norm:', np.mean(cv_mae) / np.max(y))
print('R2:', np.mean(
cross_val_score(make_model(), X, y, scoring='r2',
cv=KFold(10, shuffle=True))))
def evaluate(X, X_no_vif, y):
print('\tLinear Regression')
evaluate_model(
X_no_vif, y,
lambda: LinearRegression(normalize=True))
print('\tLasso Lars AIC')
evaluate_model(
X_no_vif, y,
lambda: LassoLarsIC(criterion='aic', normalize=True))
print('\tLasso Lars BIC')
evaluate_model(
X_no_vif, y,
lambda: LassoLarsIC(criterion='bic', normalize=True))
print('\tRF')
evaluate_model(
X, y,
lambda: RandomForestRegressor(n_estimators=100, n_jobs=16))
print('\tSVR')
evaluate_model(
X, y,
lambda: make_pipeline(StandardScaler(), SVR(C=1.0, epsilon=0.2)))
def make_scatterplot(X, y, plot_filename):
train, test = train_test_split(
list(range(len(X))), test_size=.1, random_state=42)
train_X, train_y = X[train], y[train]
test_X, test_y = X[test], y[test]
model = make_pipeline(
StandardScaler(),
RandomForestRegressor(n_estimators=100, n_jobs=16))
model.fit(train_X, train_y)
pred_train = model.predict(train_X)
pred_test = model.predict(test_X)
scatterplot(train_y, pred_train,
test_y, pred_test,
filepath=plot_filename)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--indices_file', '-i', type=str,
default='quantiles_tasa_coca_cds.csv')
parser.add_argument('--tf_file', '-tf', type=str,
default='models/tasa_coca_cds_linear/term_frequency.json')
parser.add_argument('--output_name', '-o', type=str,
default='results')
parser.add_argument('--results_directory', '-r', type=str,
default='results')
return parser.parse_args()
def run_on_scores(aoa_scores, aoa_list, indices_file):
results_dir = os.path.join(args.results_directory, args.output_name)
if not os.path.exists(results_dir):
os.mkdir(results_dir)
sys.stdout = open(os.path.join(
results_dir, f'{aoa_list}_results.txt'), 'w')
scores = aoa_scores[aoa_list]
for feature_selection in [FeatureSelection.LEXICAL,
FeatureSelection.LEXICAL_AND_WORDNET,
FeatureSelection.ALL_WITHOUT_WORDNET,
FeatureSelection.ALL,
FeatureSelection.ONLY_WORDNET,
FeatureSelection.ONLY_WORD_TRAJECTORY]:
print('\n------', feature_selection, '\n')
X, y = get_data(scores, indices_file, feature_selection)
X_no_vif = calculate_vif_(X)
evaluate(X, X_no_vif, y)
scatterplot_file = f'{aoa_list}_{feature_selection}_scatter.png'
make_scatterplot(X, y, os.path.join(results_dir, scatterplot_file))
sys.stdout.close()
def get_term_frequency(tf_file):
with open(tf_file, 'rt') as fin:
all_term_frequency = json.load(fin)
term_frequency = {}
for k in all_term_frequency[-1]:
for i in range(len(all_term_frequency)):
if k not in term_frequency:
term_frequency[k] = []
step_tf = all_term_frequency[i].get(k, 0) / 1_000_000
term_frequency[k].append(step_tf)
words = list(term_frequency.keys())
tf_X = np.array([term_frequency[w] for w in words])
tf_X_vif = calculate_vif_(tf_X)
for i, w in enumerate(words):
term_frequency[w] = tf_X_vif[i].tolist() + [np.mean(tf_X[i]), np.std(tf_X[i])]
return term_frequency
if __name__ == '__main__':
args = parse_args()
term_frequency = get_term_frequency(args.tf_file)
aoa_scores = load_scores(AOA_LISTS)
for aoa_list in AOA_LISTS:
run_on_scores(aoa_scores, aoa_list, args.indices_file)