-
Notifications
You must be signed in to change notification settings - Fork 103
/
data.py
168 lines (107 loc) · 3.75 KB
/
data.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
from __future__ import print_function
import itertools
import os
import zipfile
import numpy as np
import requests
import scipy.sparse as sp
def _get_movielens_path():
"""
Get path to the movielens dataset file.
"""
return os.path.join(os.path.dirname(os.path.abspath(__file__)),
'movielens.zip')
def _download_movielens(dest_path):
"""
Download the dataset.
"""
url = 'http://files.grouplens.org/datasets/movielens/ml-100k.zip'
req = requests.get(url, stream=True)
print('Downloading MovieLens data')
with open(dest_path, 'wb') as fd:
for chunk in req.iter_content():
fd.write(chunk)
def _get_raw_movielens_data():
"""
Return the raw lines of the train and test files.
"""
path = _get_movielens_path()
if not os.path.isfile(path):
_download_movielens(path)
with zipfile.ZipFile(path) as datafile:
return (datafile.read('ml-100k/ua.base').decode().split('\n'),
datafile.read('ml-100k/ua.test').decode().split('\n'))
def _parse(data):
"""
Parse movielens dataset lines.
"""
for line in data:
if not line:
continue
uid, iid, rating, timestamp = [int(x) for x in line.split('\t')]
yield uid, iid, rating, timestamp
def _build_interaction_matrix(rows, cols, data):
mat = sp.lil_matrix((rows, cols), dtype=np.int32)
for uid, iid, rating, timestamp in data:
# Let's assume only really good things are positives
if rating >= 4.0:
mat[uid, iid] = 1.0
return mat.tocoo()
def _get_movie_raw_metadata():
"""
Get raw lines of the genre file.
"""
path = _get_movielens_path()
if not os.path.isfile(path):
_download_movielens(path)
with zipfile.ZipFile(path) as datafile:
return datafile.read('ml-100k/u.item').decode(errors='ignore').split('\n')
def get_movielens_item_metadata(use_item_ids):
"""
Build a matrix of genre features (no_items, no_features).
If use_item_ids is True, per-item feeatures will also be used.
"""
features = {}
genre_set = set()
for line in _get_movie_raw_metadata():
if not line:
continue
splt = line.split('|')
item_id = int(splt[0])
genres = [idx for idx, val in
zip(range(len(splt[5:])), splt[5:])
if int(val) > 0]
if use_item_ids:
# Add item-specific features too
genres.append(item_id)
for genre_id in genres:
genre_set.add(genre_id)
features[item_id] = genres
mat = sp.lil_matrix((len(features) + 1,
len(genre_set)),
dtype=np.int32)
for item_id, genre_ids in features.items():
for genre_id in genre_ids:
mat[item_id, genre_id] = 1
return mat
def get_dense_triplets(uids, pids, nids, num_users, num_items):
user_identity = np.identity(num_users)
item_identity = np.identity(num_items)
return user_identity[uids], item_identity[pids], item_identity[nids]
def get_triplets(mat):
return mat.row, mat.col, np.random.randint(mat.shape[1], size=len(mat.row))
def get_movielens_data():
"""
Return (train_interactions, test_interactions).
"""
train_data, test_data = _get_raw_movielens_data()
uids = set()
iids = set()
for uid, iid, rating, timestamp in itertools.chain(_parse(train_data),
_parse(test_data)):
uids.add(uid)
iids.add(iid)
rows = max(uids) + 1
cols = max(iids) + 1
return (_build_interaction_matrix(rows, cols, _parse(train_data)),
_build_interaction_matrix(rows, cols, _parse(test_data)))