-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataProcess.py
341 lines (289 loc) · 11.5 KB
/
dataProcess.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import pickle
import numpy as np
import scipy.sparse as sp
import random
import os
import argparse
from create_adj import creatMultiItemUserAdj
import networkx as nx
def splitData(dataset, cv):
DIR = os.path.join(os.getcwd(), "dataset", dataset)
with open(DIR + "/category.pkl", 'rb') as fs:
category = pickle.load(fs)
with open(DIR + "/ratings.pkl", 'rb') as fs:
data = pickle.load(fs)
with open(DIR + "/times.pkl", 'rb') as fs:
time = pickle.load(fs)
with open(DIR + "/trust.pkl", 'rb') as fs:
trust = pickle.load(fs)
assert np.sum(data.tocoo().row != time.tocoo().row) == 0
assert np.sum(data.tocoo().col != time.tocoo().col) == 0
row, col = data.shape
print("user num = %d, item num = %d"%(row, col))
train_row, train_col, train_data, train_data_time = [], [], [], []
test_row, test_col = [], []
valid_row, valid_col = [], []
# userList = np.where(np.sum(data!=0, axis=1)>=2)[0]
for i in range(row):
tmp_data = data[i].toarray()[0]
if np.sum(tmp_data != 0) < 3:
continue
tmp_data_time = time[i].toarray()[0]
uid = [i] * col
num = data[i].nnz
#降序排序
idx = np.argsort(-tmp_data_time).tolist()
idx = idx[: num]
rating_data = tmp_data[idx].tolist()
time_data = tmp_data_time[idx].tolist()
assert np.sum(tmp_data[idx] == 0) == 0
assert np.sum(tmp_data_time[idx] == 0) == 0
test_num = 1
valid_num = 1
train_num = num - 2
test_row += [uid[0]]
test_col += [idx[0]]
valid_row += [uid[1]]
valid_col += [idx[1]]
train_row += uid[0: train_num]
train_col += idx[2:]
train_data += rating_data[2:]
train_data_time += time_data[2:]
assert (0 in train_data) == False
assert (0 in train_data_time) == False
train = sp.csc_matrix((train_data, (train_row, train_col)), shape=data.shape)
train_time = sp.csc_matrix((train_data_time, (train_row, train_col)), shape=data.shape)
test = sp.csc_matrix(([1]*len(test_row), (test_row, test_col)), shape=data.shape)
valid = sp.csc_matrix(([1]*len(valid_row), (valid_row, valid_col)), shape=data.shape)
print("train num = %d, train rate = %.2f"%(train.nnz, train.nnz/data.nnz))
print("test num = %d, test rate = %.2f"%(test.nnz, test.nnz/data.nnz))
print("valid num = %d, valid rate = %.2f"%(valid.nnz, valid.nnz/data.nnz))
with open(DIR + "/implicit/train.pkl", 'wb') as fs:
pickle.dump(train.tocsr(), fs)
with open(DIR + "/implicit/train_time.pkl", 'wb') as fs:
pickle.dump(train_time.tocsr(), fs)
with open(DIR + "/implicit/test.pkl", 'wb') as fs:
pickle.dump(test.tocsr(), fs)
with open(DIR + "/implicit/valid.pkl", 'wb') as fs:
pickle.dump(valid.tocsr(), fs)
with open(DIR + "/implicit/trust.pkl", 'wb') as fs:
pickle.dump(trust.tocsr(), fs)
with open(DIR + "/implicit/category.pkl", 'wb') as fs:
pickle.dump(category.tocsr(), fs)
def filterData(dataset, cv):
DIR = os.path.join(os.getcwd(), "dataset", dataset)
#filter
with open(DIR + "/implicit/train.pkl", 'rb') as fs:
train = pickle.load(fs)
with open(DIR + "/implicit/test.pkl", 'rb') as fs:
test = pickle.load(fs)
with open(DIR + "/implicit/valid.pkl", 'rb') as fs:
valid = pickle.load(fs)
with open(DIR + "/implicit/category.pkl", 'rb') as fs:
category = pickle.load(fs)
with open(DIR + "/implicit/train_time.pkl", 'rb') as fs:
train_time = pickle.load(fs)
with open(DIR + "/implicit/trust.pkl", 'rb') as fs:
trust = pickle.load(fs)
trust = trust + trust.transpose()
trust = (trust != 0)*1
a = np.sum(np.sum(train != 0, axis=1) ==0)
b = np.sum(np.sum(train != 0, axis=0) ==0)
c = np.sum(np.sum(trust, axis=1) == 0)
while a != 0 or b != 0 or c != 0:
if a != 0:
idx, _ = np.where(np.sum(train != 0, axis=1) != 0)
train = train[idx]
test = test[idx]
valid = valid[idx]
train_time = train_time[idx]
trust = trust[idx][:, idx]
elif b != 0:
_, idx = np.where(np.sum(train != 0, axis=0) != 0)
train = train[:, idx]
test = test[:, idx]
valid = valid[:, idx]
train_time = train_time[:, idx]
category = category[idx]
elif c != 0:
idx, _ = np.where(np.sum(trust, axis=1) != 0)
train = train[idx]
test = test[idx]
valid = valid[idx]
train_time = train_time[idx]
trust = trust[idx][:, idx]
a = np.sum(np.sum(train != 0, axis=1) ==0)
b = np.sum(np.sum(train != 0, axis=0) ==0)
c = np.sum(np.sum(trust, axis=1) == 0)
nums = train.nnz+test.nnz+valid.nnz
print("train num = %d, train rate = %.2f"%(train.nnz, train.nnz/nums))
print("test num = %d, test rate = %.2f"%(test.nnz, test.nnz/nums))
print("valid num = %d, valid rate = %.2f"%(valid.nnz, valid.nnz/nums))
with open(DIR + "/implicit/train.pkl", 'wb') as fs:
pickle.dump(train, fs)
with open(DIR + "/implicit/test.pkl", 'wb') as fs:
pickle.dump(test, fs)
with open(DIR + "/implicit/valid.pkl", 'wb') as fs:
pickle.dump(valid, fs)
with open(DIR + "/implicit/train_time.pkl", 'wb') as fs:
pickle.dump(train_time, fs)
with open(DIR + "/implicit/trust.pkl", 'wb') as fs:
pickle.dump(trust, fs)
with open(DIR + "/implicit/category.pkl", 'wb') as fs:
pickle.dump(category, fs)
def splitAgain(dataset, cv):
DIR = os.path.join(os.getcwd(), "dataset", dataset)
with open(DIR + "/implicit/train.pkl", 'rb') as fs:
train = pickle.load(fs)
with open(DIR + "/implicit/test.pkl", 'rb') as fs:
test = pickle.load(fs)
print(train.nnz)
print(test.nnz)
with open(DIR + "/implicit/train_time.pkl", 'rb') as fs:
train_time = pickle.load(fs)
train = train.tolil()
test = test.tolil()
train_time = train_time.tolil()
idx = np.where(np.sum(test!=0, axis=1).A == 0)[0]
for i in idx:
uid = i
tmp_data = train[i].toarray()[0]
if np.sum(tmp_data != 0) < 2:
continue
num = train[i].nnz
tmp_data_time = train_time[i].toarray()[0]
l = np.argsort(-tmp_data_time).tolist()
l = l[: num]
# test[uid, l[0]] = train[uid, l[0]]
test[uid, l[0]] = 1
train[uid, l[0]] = 0
train_time[uid, l[0]] = 0
train = train.tocsr()
train_time = train_time.tocsr()
test = test.tocsr()
assert np.sum(train.tocoo().data == 0)==0
assert np.sum(test.tocoo().data == 0)==0
assert (train+test).nnz == train.nnz+test.nnz
with open(DIR + "/implicit/train.pkl", 'wb') as fs:
pickle.dump(train, fs)
with open(DIR + "/implicit/test.pkl", 'wb') as fs:
pickle.dump(test, fs)
with open(DIR + "/implicit/train_time.pkl", 'wb') as fs:
pickle.dump(train_time, fs)
def generateGraph(dataset, cv):
DIR = os.path.join(os.getcwd(), "dataset", dataset)
with open(DIR + "/implicit/train.pkl", 'rb') as fs:
train = pickle.load(fs)
with open(DIR + "/implicit/trust.pkl", 'rb') as fs:
trustMat = pickle.load(fs)
with open(DIR + "/implicit/category.pkl", 'rb') as fs:
categoryMat= pickle.load(fs)
with open(DIR + "/implicit/categoryDict.pkl", 'rb') as fs:
categoryDict = pickle.load(fs)
userNum, itemNum = train.shape
assert categoryMat.shape[0] == train.shape[1]
mat = (trustMat.T + trustMat) + sp.eye(userNum)
UU_mat = (mat != 0)*1
ITI_mat = sp.dok_matrix((itemNum, itemNum))
categoryMat = categoryMat.toarray()
for i in range(categoryMat.shape[0]):
itemTypeList = np.where(categoryMat[i])[0]
for itemType in itemTypeList:
itemList = categoryDict[itemType]
itemList = np.array(itemList)
if itemList.size < 100:
rate = 0.1
elif itemList.size < 1000:
rate = 0.01
else:
rate = 0.001
itemList2 = np.random.choice(itemList, size=int(itemList.size*rate/2), replace=False)
itemList2 = itemList2.tolist()
tmp = [i for _ in range(len(itemList2))]
ITI_mat[tmp, itemList2] = 1
ITI_mat = ITI_mat.tocsr()
ITI_mat = ITI_mat + ITI_mat.T + sp.eye(itemNum)
ITI_mat = (ITI_mat != 0)*1
uu_vv_graph = {}
uu_vv_graph['UU'] = UU_mat
uu_vv_graph['II'] = ITI_mat
with open(DIR + '/implicit/uu_vv_graph.pkl', 'wb') as fs:
pickle.dump(uu_vv_graph, fs)
def createCategoryDict(dataset, cv):
DIR = os.path.join(os.getcwd(), "dataset", dataset)
with open(DIR + "/implicit/train.pkl", 'rb') as fs:
train = pickle.load(fs)
with open(DIR + "/implicit/category.pkl", 'rb') as fs:
category = pickle.load(fs)
assert category.shape[0] == train.shape[1]
categoryDict = {}
categoryData = category.toarray()
for i in range(categoryData.shape[0]):
iid = i
typeList = np.where(categoryData[i])[0]
# typeid = categoryData[i]
for typeid in typeList:
if typeid in categoryDict:
categoryDict[typeid].append(iid)
else:
categoryDict[typeid] = [iid]
with open(DIR + "/implicit/categoryDict.pkl", 'wb') as fs:
pickle.dump(categoryDict, fs)
def testNegSample(dataset, cv):
DIR = os.path.join(os.getcwd(), "dataset", dataset)
#filter
with open(DIR + "/implicit/train.pkl", 'rb') as fs:
train = pickle.load(fs)
with open(DIR + "/implicit/test.pkl", 'rb') as fs:
test = pickle.load(fs)
with open(DIR + "/implicit/valid.pkl", 'rb') as fs:
valid = pickle.load(fs)
train = train.todok()
test_u = test.tocoo().row
test_v = test.tocoo().col
valid_u = valid.tocoo().row
valid_v = valid.tocoo().col
assert test_u.size == test_v.size
assert valid_u.size == valid_v.size
n = test_u.size
test_data = []
for i in range(n):
u = test_u[i]
v = test_v[i]
test_data.append([u, v])
for t in range(100):
j = np.random.randint(test.shape[1])
while (u, j) in train or j == v:
j = np.random.randint(test.shape[1])
test_data.append([u, j])
n = valid_u.size
valid_data = []
for i in range(n):
u = valid_u[i]
v = valid_v[i]
valid_data.append([u, v])
for t in range(100):
j = np.random.randint(valid.shape[1])
while (u, j) in train or j == v:
j = np.random.randint(valid.shape[1])
valid_data.append([u, j])
with open(DIR + "/implicit/test_data.pkl", 'wb') as fs:
pickle.dump(test_data, fs)
with open(DIR + "/implicit/valid_data.pkl", 'wb') as fs:
pickle.dump(valid_data, fs)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
#dataset params
parser.add_argument('--dataset', type=str, default="Epinions", help="CiaoDVD,Epinions,Douban")
parser.add_argument('--cv', type=int, default=1, help="1,2,3,4,5")
args = parser.parse_args()
dataset = args.dataset+ "_time"
splitData(dataset, args.cv)
filterData(dataset, args.cv)
splitAgain(dataset, args.cv)
filterData(dataset, args.cv)
testNegSample(dataset, args.cv)
createCategoryDict(dataset, args.cv)
creatMultiItemUserAdj(dataset, args.cv)
generateGraph(dataset, args.cv)
print("Done")