-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocess_playlist.py
238 lines (171 loc) · 10.9 KB
/
preprocess_playlist.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
import numpy as np
import pandas as pd
from datetime import timezone, datetime, timedelta
import random
import time
from theano.tensor import inplace
#data config (all methods)
DATA_PATH = '../data/playlist/raw/'
DATA_PATH_PROCESSED = '../data/playlist/prepared/'
#DATA_FILE = 'yoochoose-clicks-10M'
DATA_FILE = 'music1000-0'
#filtering config (all methods)
MIN_SESSION_LENGTH = 2
MIN_ITEM_SUPPORT = 5
#days test default config
DAYS_TEST = 1
#slicing default config
NUM_SLICES = 10
DAYS_OFFSET = 0
DAYS_SHIFT = 5
DAYS_TRAIN = 9
DAYS_TEST = 1
#preprocessing from original gru4rec
def preprocess_org( path=DATA_PATH, file=DATA_FILE, path_proc=DATA_PATH_PROCESSED, min_item_support=MIN_ITEM_SUPPORT, min_session_length=MIN_SESSION_LENGTH ):
data = load_data( path+file )
data = filter_data( data, min_item_support, min_session_length )
split_data_org( data, path_proc+file )
#preprocessing adapted from original gru4rec
def preprocess_days_test( path=DATA_PATH, file=DATA_FILE, path_proc=DATA_PATH_PROCESSED, min_item_support=MIN_ITEM_SUPPORT, min_session_length=MIN_SESSION_LENGTH, days_test=DAYS_TEST ):
data = load_data( path+file )
data = filter_data( data, min_item_support, min_session_length )
split_data( data, path_proc+file, days_test )
#preprocessing to create data slices with a window
def preprocess_slices( path=DATA_PATH, file=DATA_FILE, path_proc=DATA_PATH_PROCESSED, min_item_support=MIN_ITEM_SUPPORT, min_session_length=MIN_SESSION_LENGTH,
num_slices = NUM_SLICES, days_offset = DAYS_OFFSET, days_shift = DAYS_SHIFT, days_train = DAYS_TRAIN, days_test=DAYS_TEST ):
data = load_data( path+file )
data = filter_data( data, min_item_support, min_session_length )
slice_data( data, path_proc+file, num_slices, days_offset, days_shift, days_train, days_test )
#just load and show info
def preprocess_info( path=DATA_PATH, file=DATA_FILE, path_proc=DATA_PATH_PROCESSED, min_item_support=MIN_ITEM_SUPPORT, min_session_length=MIN_SESSION_LENGTH ):
data = load_data( path+file )
data = filter_data( data, min_item_support, min_session_length )
def load_data( file ) :
#load csv
data = pd.read_csv( file+'.csv', sep=',', header=None, usecols=[0,1,2,3,4], dtype={0:np.int32, 1:str, 3:str, 4:str}, encoding='latin-1' )
#user_id,playlists_id,position,artist_name,track_name
#specify header names
data.columns = ['UserId', 'SessionIdStr', 'Position', 'ArtistStr', 'TrackStr']
data['Position'] = pd.to_numeric( data.Position, errors='coerce' )
data = data[np.isfinite( data.Position )]
data['SessionId'] = data.groupby( [data.SessionIdStr] ).grouper.group_info[0]
data['Track'] = data.ArtistStr+'/'+data.TrackStr
data['ItemId'] = data.groupby( [data.Track] ).grouper.group_info[0]
del( data['SessionIdStr'] )
del( data['ArtistStr'] )
del( data['TrackStr'] )
del( data['Track'] )
del( data['UserId'] )
data.sort_values( by=['SessionId', 'Position'], inplace=True )
data['Time'] = None
session = -1
times = 0
for index, row in data.iterrows():
if( row.SessionId != session ):
times = ( datetime.now() - timedelta( random.randint(1, 31) ) ).timestamp()
data.set_value( index, 'Time', times )
session = row.SessionId
data['Time'] = data.apply(lambda x: ( datetime.fromtimestamp( x['Time'] ) + timedelta(seconds=x['Position']) ).timestamp(), axis=1 )
del( data['Position'] )
#output
data_start = datetime.fromtimestamp( data.Time.min(), timezone.utc )
data_end = datetime.fromtimestamp( data.Time.max(), timezone.utc )
print('Loaded data set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}\n\tSpan: {} / {}\n\n'.
format( len(data), data.SessionId.nunique(), data.ItemId.nunique(), data_start.date().isoformat(), data_end.date().isoformat() ) )
return data;
def filter_data( data, min_item_support, min_session_length ) :
#y?
session_lengths = data.groupby('SessionId').size()
data = data[np.in1d(data.SessionId, session_lengths[ session_lengths>1 ].index)]
#filter item support
item_supports = data.groupby('ItemId').size()
data = data[np.in1d(data.ItemId, item_supports[ item_supports>= min_item_support ].index)]
#filter session length
session_lengths = data.groupby('SessionId').size()
data = data[np.in1d(data.SessionId, session_lengths[ session_lengths>= min_session_length ].index)]
#output
data_start = datetime.fromtimestamp( data.Time.min(), timezone.utc )
data_end = datetime.fromtimestamp( data.Time.max(), timezone.utc )
print('Filtered data set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}\n\tSpan: {} / {}\n\n'.
format( len(data), data.SessionId.nunique(), data.ItemId.nunique(), data_start.date().isoformat(), data_end.date().isoformat() ) )
return data;
def split_data_org( data, output_file ) :
tmax = data.Time.max()
session_max_times = data.groupby('SessionId').Time.max()
session_train = session_max_times[session_max_times < tmax-86400].index
session_test = session_max_times[session_max_times >= tmax-86400].index
train = data[np.in1d(data.SessionId, session_train)]
test = data[np.in1d(data.SessionId, session_test)]
test = test[np.in1d(test.ItemId, train.ItemId)]
tslength = test.groupby('SessionId').size()
test = test[np.in1d(test.SessionId, tslength[tslength>=2].index)]
print('Full train set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}'.format(len(train), train.SessionId.nunique(), train.ItemId.nunique()))
train.to_csv(output_file + '_train_full.txt', sep='\t', index=False)
print('Test set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}'.format(len(test), test.SessionId.nunique(), test.ItemId.nunique()))
test.to_csv(output_file + '_test.txt', sep='\t', index=False)
tmax = train.Time.max()
session_max_times = train.groupby('SessionId').Time.max()
session_train = session_max_times[session_max_times < tmax-86400].index
session_valid = session_max_times[session_max_times >= tmax-86400].index
train_tr = train[np.in1d(train.SessionId, session_train)]
valid = train[np.in1d(train.SessionId, session_valid)]
valid = valid[np.in1d(valid.ItemId, train_tr.ItemId)]
tslength = valid.groupby('SessionId').size()
valid = valid[np.in1d(valid.SessionId, tslength[tslength>=2].index)]
print('Train set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}'.format(len(train_tr), train_tr.SessionId.nunique(), train_tr.ItemId.nunique()))
train_tr.to_csv( output_file + '_train_tr.txt', sep='\t', index=False)
print('Validation set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}'.format(len(valid), valid.SessionId.nunique(), valid.ItemId.nunique()))
valid.to_csv( output_file + '_train_valid.txt', sep='\t', index=False)
def split_data( data, output_file, days_test ) :
data_end = datetime.fromtimestamp( data.Time.max(), timezone.utc )
test_from = data_end - timedelta( days_test )
session_max_times = data.groupby('SessionId').Time.max()
session_train = session_max_times[ session_max_times < test_from.timestamp() ].index
session_test = session_max_times[ session_max_times >= test_from.timestamp() ].index
train = data[np.in1d(data.SessionId, session_train)]
test = data[np.in1d(data.SessionId, session_test)]
test = test[np.in1d(test.ItemId, train.ItemId)]
tslength = test.groupby('SessionId').size()
test = test[np.in1d(test.SessionId, tslength[tslength>=2].index)]
print('Full train set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}'.format(len(train), train.SessionId.nunique(), train.ItemId.nunique()))
train.to_csv(output_file + '_train_full.txt', sep='\t', index=False)
print('Test set\n\tEvents: {}\n\tSessions: {}\n\tItems: {}'.format(len(test), test.SessionId.nunique(), test.ItemId.nunique()))
test.to_csv(output_file + '_test.txt', sep='\t', index=False)
def slice_data( data, output_file, num_slices, days_offset, days_shift, days_train, days_test ):
for slice_id in range( 0, num_slices ) :
split_data_slice( data, output_file, slice_id, days_offset+(slice_id*days_shift), days_train, days_test )
def split_data_slice( data, output_file, slice_id, days_offset, days_train, days_test ) :
data_start = datetime.fromtimestamp( data.Time.min(), timezone.utc )
data_end = datetime.fromtimestamp( data.Time.max(), timezone.utc )
print('Full data set {}\n\tEvents: {}\n\tSessions: {}\n\tItems: {}\n\tSpan: {} / {}'.
format( slice_id, len(data), data.SessionId.nunique(), data.ItemId.nunique(), data_start.isoformat(), data_end.isoformat() ) )
start = datetime.fromtimestamp( data.Time.min(), timezone.utc ) + datetime.timedelta( days_offset )
middle = start + datetime.timedelta( days_train )
end = middle + datetime.timedelta( days_test )
#prefilter the timespan
session_max_times = data.groupby('SessionId').Time.max()
greater_start = session_max_times[session_max_times >= start.timestamp()].index
lower_end = session_max_times[session_max_times <= end.timestamp()].index
data_filtered = data[np.in1d(data.SessionId, greater_start.intersection( lower_end ))]
print('Slice data set {}\n\tEvents: {}\n\tSessions: {}\n\tItems: {}\n\tSpan: {} / {} / {}'.
format( slice_id, len(data_filtered), data_filtered.SessionId.nunique(), data_filtered.ItemId.nunique(), start.date().isoformat(), middle.date().isoformat(), end.date().isoformat() ) )
#split to train and test
session_max_times = data_filtered.groupby('SessionId').Time.max()
sessions_train = session_max_times[session_max_times < middle.timestamp()].index
sessions_test = session_max_times[session_max_times >= middle.timestamp()].index
train = data[np.in1d(data.SessionId, sessions_train)]
print('Train set {}\n\tEvents: {}\n\tSessions: {}\n\tItems: {}\n\tSpan: {} / {}'.
format( slice_id, len(train), train.SessionId.nunique(), train.ItemId.nunique(), start.date().isoformat(), middle.date().isoformat() ) )
train.to_csv(output_file + '_train_full.'+str(slice_id)+'.txt', sep='\t', index=False)
test = data[np.in1d(data.SessionId, sessions_test)]
test = test[np.in1d(test.ItemId, train.ItemId)]
tslength = test.groupby('SessionId').size()
test = test[np.in1d(test.SessionId, tslength[tslength>=2].index)]
print('Test set {}\n\tEvents: {}\n\tSessions: {}\n\tItems: {}\n\tSpan: {} / {} \n\n'.
format( slice_id, len(test), test.SessionId.nunique(), test.ItemId.nunique(), middle.date().isoformat(), end.date().isoformat() ) )
test.to_csv(output_file + '_test.'+str(slice_id)+'.txt', sep='\t', index=False)
# -------------------------------------
# MAIN TEST
# --------------------------------------
if __name__ == '__main__':
preprocess_info();