forked from hexiangnan/neural_collaborative_filtering
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataset.py
More file actions
78 lines (70 loc) · 2.97 KB
/
Copy pathDataset.py
File metadata and controls
78 lines (70 loc) · 2.97 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
Created on Aug 8, 2016
Processing datasets.
@author: Xiangnan He (xiangnanhe@gmail.com)
'''
import numpy as np
import scipy.sparse as sp
class Dataset:
def __init__(self, path: str) -> None:
self.train_matrix = self._load_rating_file_as_matrix(
path + '.train.rating'
)
self.test_ratings = self._load_rating_file_as_list(
path + '.test.rating'
)
self.test_negatives = self._load_negative_file(
path + '.test.negative'
)
assert len(self.test_ratings) == len(self.test_negatives)
self.num_users, self.num_items = self.train_matrix.get_shape()
# The train matrix is sized to the max IDs seen in training only.
# Test ratings and negatives may reference items (or users) not present
# in training (common in sampled datasets). Extend the counts so that
# embedding matrices cover every ID that will be looked up at eval time.
if self.test_ratings:
max_test_user = max(r[0] for r in self.test_ratings)
max_test_item = max(r[1] for r in self.test_ratings)
self.num_users = max(self.num_users, max_test_user + 1)
self.num_items = max(self.num_items, max_test_item + 1)
if self.test_negatives:
max_neg_item = max(
max(negs) for negs in self.test_negatives if negs
)
self.num_items = max(self.num_items, max_neg_item + 1)
def _load_rating_file_as_list(self, filename: str) -> list[list[int]]:
rating_list: list[list[int]] = []
with open(filename, 'r') as f:
for line in f:
if not line.strip():
continue
arr = line.split('\t')
rating_list.append([int(arr[0]), int(arr[1])])
return rating_list
def _load_negative_file(self, filename: str) -> list[list[int]]:
negative_list: list[list[int]] = []
with open(filename, 'r') as f:
for line in f:
if not line.strip():
continue
arr = line.split('\t')
negative_list.append([int(x) for x in arr[1:]])
return negative_list
def _load_rating_file_as_matrix(self, filename: str) -> sp.dok_matrix:
'''Read .rating file and return dok matrix.'''
rows: list[tuple[int, int, float]] = []
num_users, num_items = 0, 0
with open(filename, 'r') as f:
for line in f:
if not line.strip():
continue
arr = line.split('\t')
user, item, rating = int(arr[0]), int(arr[1]), float(arr[2])
num_users = max(num_users, user)
num_items = max(num_items, item)
rows.append((user, item, rating))
mat = sp.dok_matrix((num_users + 1, num_items + 1), dtype=np.float32)
for user, item, rating in rows:
if rating > 0:
mat[user, item] = 1.0
return mat