-
Notifications
You must be signed in to change notification settings - Fork 4
/
DataInput.py
68 lines (54 loc) · 1.83 KB
/
DataInput.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
import pandas as pd
import random
import pickle
class DataIterator:
def __init__(self,
data,
batch_size = 128,
max_seq_length = 5,
neg_count = 1,
all_items = None,
user_all_items = None,
shuffle = True):
self.data = data
self.datasize = data.shape[0]
self.neg_count = neg_count
self.batch_size = batch_size
self.msl = max_seq_length
self.user_all_items = user_all_items
self.all_items = all_items
self.shuffle = shuffle
self.seed = 0
self.idx=0
def __iter__(self):
return self
def reset(self):
self.idx = 0
if self.shuffle:
self.data= self.data.sample(frac=1).reset_index(drop=True)
self.seed = self.seed + 1
random.seed(self.seed)
def __next__(self):
if self.idx >= self.datasize:
self.reset()
raise StopIteration
nums = self.batch_size
if self.datasize - self.idx < self.batch_size:
nums = self.datasize - self.idx
cur = self.data.iloc[self.idx:self.idx+nums]
user = cur['user'].values
#train = pd.DataFrame({'user': train_users, 'seq': train_seqs, 'target': train_targets})
target = []
for t in cur['target'].values:
target.append(t)
user_seq = []
sl = []
for seq in cur['seq'].values:
user_seq.append(seq)
sl.append(len(seq))
neg_seq = []
for u in cur['user']:
user_item_set = set(self.all_items) - set(self.user_all_items[u])
neg_seq.append(random.sample(user_item_set,self.neg_count))
self.idx += self.batch_size
return (user, target, user_seq, sl, neg_seq)