-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataset.py
More file actions
120 lines (102 loc) · 4.23 KB
/
Dataset.py
File metadata and controls
120 lines (102 loc) · 4.23 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
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
import scipy.sparse as sp
import numpy as np
import collections
class Dataset(object):
def __init__(self, path):
self.path = path
self.num_users, self.num_items = self.get_train_data_shape()
def get_train_data_shape(self):
filename = self.path + ".train.rating"
num_users=0
num_items = 0
with open(filename, "r") as f:
line = f.readline()
while line is not None and line != "":
arr = line.split("\t")
u, i = int(arr[0]), int(arr[1])
num_users = max(num_users, u)
num_items = max(num_items, i)
line = f.readline()
num_items += 1
num_users += 1
return num_users, num_items
def load_test_file(self):
filename = self.path + ".test.rating"
ratingList = []
with open(filename, "r") as f:
line = f.readline()
while line is not None and line != "":
arr = line.split("\t")
user, item = int(arr[0]), int(arr[1])
ratingList.append([user, item])
line = f.readline()
return ratingList
def load_negative_file(self):
filename = self.path + ".test.negative"
negativeList = []
with open(filename, "r") as f:
line = f.readline()
while line is not None and line != "":
arr = line.split("\t")
negatives = []
for x in arr[1:]:
negatives.append(int(x))
negativeList.append(negatives)
line = f.readline()
return negativeList
def load_train_file(self):
filename = self.path + ".train.rating"
num_users, num_items = self.get_train_data_shape()
mat = sp.dok_matrix((num_users, num_items), dtype=np.float32)
with open(filename, "r") as f:
line = f.readline()
while line is not None and line != "":
arr = line.split("\t")
user, item, rating = int(arr[0]), int(arr[1]), float(arr[2])
#print("usr:{} item:{} score:{}".format(user,item,rating))
if rating > 0:
mat[user, item] = 1.0
line = f.readline()
train_datas=[[],[],[]]
with open(filename, "r") as f:
for (usr, item) in mat.keys():
train_datas[0].append(usr)
train_datas[1].append(item)
train_datas[2].append(1)
line = f.readline()
for t in range(4):
nega_item = np.random.randint(num_items)
while (usr, nega_item) in mat.keys():
nega_item = np.random.randint(num_items)
train_datas[0].append(usr)
train_datas[1].append(nega_item)
train_datas[2].append(0)
return train_datas
def load_client_train_date(self):
filename = self.path + ".train.rating"
num_users, num_items = self.get_train_data_shape()
mat = sp.dok_matrix((num_users, num_items), dtype=np.float32)
with open(filename, "r") as f:
line = f.readline()
while line is not None and line != "":
arr = line.split("\t")
user, item, rating = int(arr[0]), int(arr[1]), float(arr[2])
#print("usr:{} item:{} score:{}".format(user,item,rating))
if rating > 0:
mat[user, item] = 1.0
line = f.readline()
client_datas=[[[],[],[]] for i in range(num_users)]
with open(filename, "r") as f:
for (usr, item) in mat.keys():
client_datas[usr][0].append(usr)
client_datas[usr][1].append(item)
client_datas[usr][2].append(1)
line = f.readline()
for t in range(4):
nega_item = np.random.randint(num_items)
while (usr, nega_item) in mat.keys():
nega_item = np.random.randint(num_items)
client_datas[usr][0].append(usr)
client_datas[usr][1].append(nega_item)
client_datas[usr][2].append(0)
return client_datas