-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluatemodel.py
More file actions
56 lines (42 loc) · 1.77 KB
/
evaluatemodel.py
File metadata and controls
56 lines (42 loc) · 1.77 KB
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
import os
import scipy.sparse as sp
import functions
import pickle
import csv
def main():
# set these!
matrix_name = "reciprocal_pop_conf"
save_model = True
matrix_filename = f'data/matrices/{matrix_name}_matrix.npz'
matrix = sp.load_npz(matrix_filename)
# ratio = 65464776.0 / matrix.sum() # total interactions
# alpha, reg, factors = round(3360 * ratio)/40, 1.19, 128
alpha, reg, factors = 107480, 4, 128
# set this to true if you want to save the model
print(f'matrix: {matrix_name}, alpha: {alpha}, reg: {reg}, factors: {factors}')
os.environ['MKL_NUM_THREADS'] = '1'
os.environ['MKL_DEBUG_CPU_TYPE'] = '5'
sh, mb = 2641, 22530
train, test, masked = functions.get_train_test_masked(matrix)
print(f'alpha: {alpha}, reg: {reg}, factors: {factors}')
model_name = f'data/models/{matrix_name}_a{alpha}_r{reg}_f{factors}_model.pickle'
try:
with open(model_name, "rb") as input_file:
model = pickle.load(input_file)
except FileNotFoundError:
model = functions.get_model(train, alpha=alpha, reg=reg, factors=factors)
if save_model:
try:
with open(model_name, 'wb') as output_file:
pickle.dump(model, output_file)
except FileNotFoundError:
os.mkdir('data/models')
with open(model_name, 'wb') as output_file:
pickle.dump(model, output_file)
del train
pop_gap, auc, sh_auc, mb_auc, lt_auc = functions.score_model(model, test, masked, sh, mb)
results = [matrix_name, alpha, reg, factors, pop_gap, auc, sh_auc, mb_auc, lt_auc]
with open('data/results.csv', 'a') as result_file:
wr = csv.writer(result_file, dialect='excel')
wr.writerow(results)
main()