-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasplit.py
296 lines (251 loc) · 10.6 KB
/
datasplit.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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 14 12:33:50 2015
@author: thalita
"""
import abc
import pickle as pkl
import numpy as np
from sklearn.cross_validation import KFold
from databases import HiddenRatingsDatabase
from collections import defaultdict
from bisect import bisect
from utils import to_gzpickle, read_gzpickle
class Split(object):
def __init__(self, train, tuning=None, valid=None, test=None, config=None):
self.train = train
self.tuning = tuning
self.valid = valid
self.test = test
self.config = config
class Splitter(object):
__metaclass__ = abc.ABCMeta
@property
def per_user(self):
return self._per_user
@per_user.setter
def per_user(self, val):
self._per_user = val
@property
def train(self):
return self._train
@train.setter
def train(self, val):
self._train = val
@property
def test(self):
return self._test
@test.setter
def test(self, val):
self._test = val
@abc.abstractmethod
def split(self, database):
pass
def save(self, filepath):
fname_prefix = filepath + self.suffix
if self.nfolds == 1:
split = Split(train=self.train, test=self.test, config=self.config)
to_gzpickle(split, fname_prefix + '_split.pkl')
else:
for i in range(self.nfolds):
config = self.config.copy()
config['fold'] = i
split = Split(train=self.train[i], test=self.test[i], config=config)
fname = fname_prefix + '_split_%d.pkl' % i
to_gzpickle(split, fname)
return fname_prefix
def split_save(self, database, filepath):
self.split(database)
return self.save(filepath)
class CVTestRatingSplitter(Splitter):
def __init__(self, nfolds=5, per_user=True, pct_hidden=0.2, threshold=0):
self.nfolds = nfolds
self.per_user = per_user
self.pct_hidden = pct_hidden
self.config = {'nfolds': nfolds, 'pct_hidden': pct_hidden,
'per_user': per_user, 'threshold': threshold}
self.ensemble_splitter = HoldoutRatingSplitter(pct_hidden/2, per_user,
threshold)
if nfolds <= 1:
self.CV_splitter = HoldoutRatingSplitter(pct_hidden, per_user,
threshold)
else:
self.CV_splitter = kFoldRatingSplitter(nfolds, per_user)
self.Test_splitter = HoldoutRatingSplitter(pct_hidden, per_user,
threshold)
self.train = None
self.tuning = None
self.test = None
self.valid = None
self.suffix = '_CV' + self.CV_splitter.suffix \
+ '_test' + self.Test_splitter.suffix
def split_save(self, database, filepath):
self.Test_splitter.split(database)
self.test = self.Test_splitter.test
train_valid = self.Test_splitter.train
if self.nfolds > 1:
if self.per_user:
for u in range(database.n_users()):
item_ratings = database.get_rating_list(u)
size = len(item_ratings)
splits = list(KFold(size, n_folds=self.nfolds, shuffle=True))
#splits are in format [...,(train_i,test_i),...]
for i in range(self.nfolds):
for idx in splits[i][1]:
self.CV_splitter.hidden_coord[i].append((u, item_ratings[idx][0]))
else:
raise NotImplementedError()
fname_prefix = filepath + self.suffix
for fold in range(self.nfolds):
split = self.CV_splitter.hidden_coord[fold]
train = HiddenRatingsDatabase(database.get_matrix(sparse=True), split)
self.ensemble_splitter.split(train)
train = self.ensemble_splitter.train
tuning = self.ensemble_splitter.test
valid = defaultdict(list)
for u, i in split:
r = database.get_rating(u, i)
valid[u].append((i, r))
config = self.CV_splitter.config.copy()
config['fold'] = fold
split = Split(train, tuning, valid, self.test, config)
fname = fname_prefix + '_split_%d.pkl' % fold
to_gzpickle(split, fname)
return fname_prefix
else:
self.CV_splitter.split(train_valid)
self.ensemble_splitter.split(self.CV_splitter.train)
self.train = self.ensemble_splitter.train
self.tuning = self.ensemble_splitter.test
self.valid = self.CV_splitter.test
return self.save(filepath)
def split(self, database):
self.Test_splitter.split(database)
self.CV_splitter.split(self.Test_splitter.train)
self.train = []
self.tuning = []
if self.nfolds > 1:
for fold in range(self.nfolds):
self.ensemble_splitter.split(self.CV_splitter.train[fold])
self.train.append(self.ensemble_splitter.train)
self.tuning.append(self.ensemble_splitter.test)
else:
self.ensemble_splitter.split(self.CV_splitter.train)
self.train = self.ensemble_splitter.train
self.tuning = self.ensemble_splitter.test
self.valid = self.CV_splitter.test
self.test = self.Test_splitter.test
def save(self, filepath):
fname_prefix = filepath + self.suffix
if self.nfolds == 1:
split = Split(self.train, self.tuning, self.valid, self.test, self.config)
to_gzpickle(split, fname_prefix + '_split.pkl')
else:
for i in range(self.nfolds):
config = self.config.copy()
config['fold'] = i
split = Split(self.train[i], self.tuning[i], self.valid[i], self.test, config)
fname = fname_prefix + '_split_%d.pkl' % i
to_gzpickle(split, fname)
return fname_prefix
class kFoldRatingSplitter(Splitter):
def __init__(self, nfolds=5, per_user=True):
self.per_user = per_user
self.nfolds = nfolds
self.suffix = '_%dfold' % self.nfolds
self.config = {'nfolds': nfolds, 'per_user': per_user}
self.hidden_coord = [[] for i in range(nfolds)]
self.train = None
self.test = None
def split(self, database):
if self.per_user:
for u in range(database.n_users()):
item_ratings = database.get_rating_list(u)
size = len(item_ratings)
splits = list(KFold(size, n_folds=self.nfolds, shuffle=True))
#splits are in format [...,(train_i,test_i),...]
for i in range(self.nfolds):
for idx in splits[i][1]:
self.hidden_coord[i].append((u, item_ratings[idx][0]))
else:
raise NotImplementedError()
self.train = \
[HiddenRatingsDatabase(database.get_matrix(sparse=True), split)
for split in self.hidden_coord]
self.test = []
for split in self.hidden_coord:
users = defaultdict(list)
for u, i in split:
r = database.get_rating(u, i)
users[u].append((i, r))
self.test.append(users)
class HoldoutRatingSplitter(Splitter):
def __init__(self, pct_hidden=0.2, per_user=True, threshold=0):
self.per_user = per_user
self.pct_hidden = pct_hidden
self.threshold = threshold
self.nfolds = 1
self.suffix = '_%d_%d_holdout' % (int((1-pct_hidden)*100),
int(pct_hidden*100))
self.config = {'nfolds': self.nfolds, 'per_user': per_user,
'pct_hidden': pct_hidden, 'threshold': threshold}
self.hidden_coord = []
self.train = None
self.test = None
def _get_hidden(self, matrix):
raise NotImplementedError()
# TODO should not use randoint as it may return repeated indices
# get positions equal to or above threshold (ratings)
row, col = np.where(matrix > self.threshold)
# len(row)== total number of ratings>=threshold
n_hidden = np.ceil(self.pct_hidden*len(row))
if n_hidden > 0:
# pick n_hidden random positions
hidden_idx = np.random.randint(0, len(row), n_hidden)
good = (row[hidden_idx].tolist(), col[hidden_idx].tolist())
else:
good = ([], [])
# get positions equal to or above threshold (ratings)
row, col = np.where(matrix <= self.threshold)
# len(row)== total number of ratings>=threshold
n_hidden = np.ceil(self.pct_hidden*len(row))
if n_hidden > 0:
# pick n_hidden random positions
hidden_idx = np.random.randint(0, len(row), n_hidden)
bad = (row[hidden_idx].tolist(), col[hidden_idx].tolist())
else:
bad = ([],[])
hidden = tuple([g+b for g,b in zip(good, bad)])
return hidden
def split(self, database):
if self.per_user:
for u in range(database.n_users()):
user_ratings = database.get_rating_list(u) # is sorted
ratings = [r for i, r in user_ratings]
n_good = idx = bisect(ratings, self.threshold)
n_hidden = int(np.ceil(self.pct_hidden*n_good))
# get good
if n_hidden > 0:
hidden = np.random.choice(n_good, n_hidden,
replace=False).tolist()
else:
hidden = []
# get bad
n_bad = len(user_ratings)-n_good
n_hidden = int(np.ceil(self.pct_hidden*n_bad))
if n_hidden > 0:
hidden += (idx+np.random.choice(n_bad, n_hidden,
replace=False)).tolist()
hidden = set(hidden)
self.hidden_coord += \
[(u, i) for i, r in user_ratings if i in hidden]
else:
raise NotImplementedError()
rows, cols = self._get_hidden(database.get_matrix())
self.hidden_coord = list(zip(rows, cols))
self.train = \
HiddenRatingsDatabase(database.get_matrix(sparse=True), self.hidden_coord)
self.test = defaultdict(list)
for u, i in self.hidden_coord:
r = database.get_rating(u, i)
self.test[u].append((i, r))