-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
74 lines (63 loc) · 2.13 KB
/
preprocessing.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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import numpy as np
import pandas as pd
import os
import json
from numba import types
from numba.typed import Dict
# In[1]:
# load origin data and shuffle
def shuffle_data():
ratings = pd.DataFrame(pd.read_csv('./ml-latest/ratings.csv'))
ratings = ratings.sample(frac=1).reset_index(drop=True)
ratings.to_csv('shuffled_ratings.csv', index=False)
# user-item matrix
def build_adjacency_matrix(df, userMapper, movieMapper):
user_item = Dict.empty(
key_type=types.int64,
value_type=types.float64[:, :]
)
if os.path.isfile('./model/user_item.json'):
user_item_json = json.loads(open('./model/user_item.json').read())
for key, value in user_item_json.items():
user_item[key] = np.array(value, dtype=np.float64)
else:
for row in df.index:
userId = userMapper[int(df.iloc[row].userId)]
movieId = movieMapper[int(df.iloc[row].movieId)]
rating = df.iloc[row].rating
if userId not in user_item:
user_item[userId] = np.array([[movieId, rating]], dtype=np.float64)
else:
user_item[userId] = np.concatenate((user_item[userId], np.array([[movieId, rating]], dtype=np.float64)), axis=0)
return user_item
# movieId convert to new index
def movieIdMapper(ratings):
if os.path.isfile('./model/movieMapper.json'):
mapper = open('./model/movieMapper.json').read()
return json.loads(mapper)
else:
movieIdSet = ratings.movieId.unique()
movieIdSet.sort()
mapper = {}
idx = 0
for id in movieIdSet:
mapper[int(id)] = idx
idx = idx + 1
return mapper
# userId convert to new index
def userIdMapper(ratings):
if os.path.isfile('./model/userMapper.json'):
mapper = open('./model/userMapper.json').read()
return json.loads(mapper)
else:
userIdSet = ratings.userId.unique()
userIdSet.sort()
mapper = {}
idx = 0
for id in userIdSet:
mapper[int(id)] = idx
idx = idx + 1
return mapper