-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
83 lines (63 loc) · 3.37 KB
/
metrics.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
from sklearn.metrics import ndcg_score
import numpy as np
import pandas as pd
import pickle
def get_ndcg(uk_jobs, bios_test, id, dicts):
ndcg = ndcg_score(np.asarray(
[uk_jobs.iloc[dicts['corpus_id']]['title'].apply(lambda x: 1 if x == bios_test['raw_title'][id] else 0)]),
[dicts['scores']], k=10)
return ndcg
def get_gap(uk_jobs, bios_test,dicts):
ndcg = []
for i in range(len(bios_test)):
ndcg.append(ndcg_score(np.asarray([uk_jobs.iloc[dicts[i]['corpus_id']]['title'].apply(lambda x: 1 if x==bios_test['raw_title'][i] else 0)]),[dicts[i]['scores']],k=10))
male_ndcg = [ndcg[i] for i in bios_test[bios_test.gender =='M'].index]
female_ndcg = [ndcg[i] for i in bios_test[bios_test.gender =='F'].index]
if len(male_ndcg)>0:
male_ndcg =sum(male_ndcg)/len(male_ndcg)
else:
male_ndcg = 0
if len(female_ndcg)>0:
female_ndcg =sum(female_ndcg)/len(female_ndcg)
else:
female_ndcg = 0
return abs(np.mean(np.array(male_ndcg))-np.mean(np.array(female_ndcg)))
def get_counterfactual_gap(pth,dicts,dicts_counter):
with open(f'{pth}share/hel/datasets/jobiqo/talent.com/JobRec/uk_jobs.pkl', 'rb') as file:
uk_jobs = pd.DataFrame(pickle.load(file)).reset_index()
with open( f'{pth}share/hel/datasets/jobiqo/talent.com/JobRec/unbalanced_test.pkl', 'rb') as file:
bios_test = pd.DataFrame(pickle.load(file))
uk_jobs = uk_jobs.drop('index', axis=1)
ndcg = []
ndcg_counter = []
for i in range(len(bios_test)):
ndcg.append(ndcg_score(np.asarray([uk_jobs.iloc[dicts[i]['corpus_id']]['title'].apply(lambda x: 1 if x==bios_test['raw_title'][i] else 0)]),[dicts[i]['scores']],k=10))
ndcg_counter.append(ndcg_score(np.asarray([uk_jobs.iloc[dicts_counter[i]['corpus_id']]['title'].apply(lambda x: 1 if x==bios_test['raw_title'][i] else 0)]),[dicts_counter[i]['scores']],k=10))
ndcg_separation = [abs(ndcg[i]-ndcg_counter[i]) for i in range(len(bios_test))]
if len(ndcg_separation)>0:
ndcg_separation =sum(ndcg_separation)/len(ndcg_separation)
else:
ndcg_separation = 0
return ndcg_separation
def SDR(bios_test,dicts):
male_item_ids=[]
female_item_ids=[]
for male in [dicts[i] for i in bios_test[bios_test.gender =='M'].index]:
male_df = pd.DataFrame(male)
male_item_ids = male_item_ids+list(male_df[male_df['scores']>sorted(male_df['scores'],reverse=True)[10]]['corpus_id'])
for female in [dicts[i] for i in bios_test[bios_test.gender =='F'].index]:
female_df = pd.DataFrame(female)
female_item_ids = female_item_ids+list(female_df[female_df['scores']>sorted(female_df['scores'],reverse=True)[10]]['corpus_id'])
output = (len(male_item_ids)+len(female_item_ids)-2*len(set(male_item_ids).intersection(female_item_ids)))/(len(male_item_ids)+len(female_item_ids)-len(set(male_item_ids).intersection(female_item_ids)))
return output
def LDR(pth,dicts,dicts_counter):
with open( f'{pth}share/hel/datasets/jobiqo/talent.com/JobRec/unbalanced_test.pkl', 'rb') as file:
bios_test = pd.DataFrame(pickle.load(file))
output = []
for i in range(len(bios_test)):
output.append((sum(((dicts[i]['corpus_id']!=dicts_counter[i]['corpus_id'])*1)[:10]))/10)
if len(output)>0:
ldr =sum(output)/len(output)
else:
ldr = 0
return ldr