-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
36 lines (30 loc) · 1.19 KB
/
utils.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
import numpy as np
import pandas as pd
def transform_indices(data, users, items):
data_index = {}
for entity, field in zip(['users', 'items'], [users, items]):
idx, idx_map = to_numeric_id(data, field)
data_index[entity] = idx_map
data.loc[:, field] = idx
return data, data_index
def to_numeric_id(data, field):
idx_data = data[field].astype("category")
idx = idx_data.cat.codes
idx_map = idx_data.cat.categories.rename(field)
return idx, idx_map
def topidx(a, topn):
parted = np.argpartition(a, -topn)[-topn:]
return parted[np.argsort(-a[parted])]
def topn_recommendations(scores, topn=10):
recommendations = np.apply_along_axis(topidx, 1, scores, topn)
return recommendations
def downvote_seen_items(scores, data, data_description):
userid = data_description['users']
itemid = data_description['items']
# get indices of observed data
user_idx = data[userid].values
item_idx = data[itemid].values
# downvote scores at the corresponding positions
user_idx, _ = pd.factorize(user_idx, sort=True)
seen_idx_flat = np.ravel_multi_index((user_idx, item_idx), scores.shape)
np.put(scores, seen_idx_flat, -np.inf)