-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighbor_knn_final_temp.py
282 lines (226 loc) · 10.7 KB
/
neighbor_knn_final_temp.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
import numpy as np
import pandas as pd
import os
from collections import Counter
from warnings import warn
warn("Unsupported module 'tqdm' is used.")
from tqdm import tqdm
class NeighborKNN:
'''
K Nearest Neighbor
version NeighborKNN-1.0 updates
+ song to tag prediction
+ tag to song prediction
'''
__version__ = "NeighborKNN-1.0"
def __init__(self, k=100, rho=0.4, \
weight_val_songs=0.5, weight_pred_songs=0.5, \
weight_val_tags=0.5, weight_pred_tags=0.5, \
sim_songs="idf", sim_tags="cos", sim_normalize=False, \
train=None, val=None, song_meta=None, pred=None, \
verbose=True, version_check=True):
'''
k : int
rho : float; 0.4(default) only for idf
alpha, beta : float; 0.5(default)
sim_songs, sim_tags : "cos"(default), "idf", "jaccard"
sim_normalize : boolean; when sim == "cos" or "idf"
verbose : boolean
'''
### data sets
self.train_id = train["id"].copy()
self.train_songs = train["songs"].copy()
self.train_tags = train["tags"].copy()
self.val_id = val["id"].copy()
self.val_songs = val["songs"].copy()
self.val_tags = val["tags"].copy()
self.val_updt_date = val["updt_date"].copy()
self.song_meta_issue_date = song_meta["issue_date"].copy()
self.pred_songs = pred["songs"].copy()
self.pred_tags = pred["tags"].copy()
self.freq_songs = None
self.freq_tags = None
self.k = k
self.rho = rho
self.weight_val_songs = weight_val_songs
self.weight_pred_songs = weight_pred_songs
self.weight_val_tags = weight_val_tags
self.weight_pred_tags = weight_pred_tags
self.sim_songs = sim_songs
self.sim_tags = sim_tags
self.sim_normalize = sim_normalize
self.verbose = verbose
self.__version__ = NeighborKNN.__version__
if version_check:
print(f"NeighborKNN version: {NeighborKNN.__version__}")
TOTAL_SONGS = song_meta.shape[0] # total number of songs
### transform date format in val
for idx in self.val_id.index:
self.val_updt_date.at[idx] = int(''.join(self.val_updt_date[idx].split()[0].split('-')))
self.val_updt_date.astype(np.int64)
if self.sim_songs == "idf":
self.freq_songs = np.zeros(TOTAL_SONGS, dtype=np.int64)
_playlist = self.train_songs
for _songs in _playlist:
self.freq_songs[_songs] += 1
del train, val, song_meta, pred
def predict(self, start=0, end=None, auto_save=False, auto_save_step=500, auto_save_fname='auto_save'):
'''
start, end : range(start, end). if end = None, range(start, end of val)
auto_save : boolean; False(default)
auto_save_step : int; 500(default)
auto_save_fname : string (without extension); 'auto_save'(default)
@returns : pandas.DataFrame; columns=['id', 'songs', 'tags']
'''
# TODO: Remove unsupported module 'tqdm'.
if end:
_range = tqdm(range(start, end)) if self.verbose else range(start, end)
elif type(start) == type([]):
_range = tqdm(start) if self.verbose else start
elif end == None:
_range = tqdm(range(start, self.val_id.index.stop)) if self.verbose else range(start, self.val_id.index.stop)
pred = []
all_songs = [set(songs) for songs in self.train_songs] # list of set
all_tags = [set(tags) for tags in self.train_tags] # list of set
for uth in _range:
k = self.k
# predict songs by tags
if self.val_songs[uth] == [] and self.val_tags[uth] != []:
playlist_tags_in_pred = set(self.pred_tags[uth])
playlist_tags_in_val = set(self.val_tags[uth])
playlist_updt_date = self.val_updt_date[uth]
simTags_in_pred = np.array([self._sim(playlist_tags_in_pred, vplaylist, self.sim_tags, opt='tags') for vplaylist in all_tags])
simTags_in_val = np.array([self._sim(playlist_tags_in_val , vplaylist, self.sim_tags, opt='tags') for vplaylist in all_tags])
simTags = ((self.weight_pred_tags * simTags_in_pred) / (len(playlist_tags_in_pred))) + \
((self.weight_val_tags * simTags_in_val) / (len(playlist_tags_in_val)))
songs = set()
while len(songs) < 100:
top = simTags.argsort()[-k:]
_songs = []
for vth in top:
_songs += self.train_songs[vth]
songs = set(_songs)
date_check = []
for track_i in songs:
if self.song_meta_issue_date[track_i] <= playlist_updt_date:
date_check.append(track_i)
songs = set(date_check)
k += 100
norm = simTags[top].sum()
if norm == 0:
norm = 1.0e+10 # FIXME
relevance = np.array([(song, np.sum([simTags[vth] if song in all_songs[vth] else 0 for vth in top]) / norm) for song in songs])
relevance = relevance[relevance[:, 1].argsort()][-100:][::-1]
sorted_songs = relevance[:, 0].astype(np.int64).tolist()
pred_songs = sorted_songs
# # check if issue_date of songs is earlier than updt_date of playlist
# for track_i in sorted_songs:
# if self.song_meta_issue_date[track_i] <= playlist_updt_date:
# pred_songs.append(track_i)
# if len(pred_songs) == 100:
# break
pred.append({
"id" : int(self.val_id[uth]),
"songs" : pred_songs,
"tags" : self.pred_tags[uth]
})
# predict tags using songs
elif self.val_songs[uth] != [] and self.val_tags[uth] == []:
playlist_songs_in_pred = set(self.pred_songs[uth])
playlist_songs_in_val = set(self.val_songs[uth])
simSongs_in_pred = np.array([self._sim(playlist_songs_in_pred, vplaylist, self.sim_songs, opt='songs') for vplaylist in all_songs])
simSongs_in_val = np.array([self._sim(playlist_songs_in_val , vplaylist, self.sim_songs, opt='songs') for vplaylist in all_songs])
simSongs = ((self.weight_pred_songs * simSongs_in_pred) / (len(playlist_songs_in_pred))) + \
((self.weight_val_songs * simSongs_in_val) / (len(playlist_songs_in_val)))
tags = []
while len(tags) < 10:
top = simSongs.argsort()[-k:]
_tags = []
for vth in top:
_tags += self.train_tags[vth]
counts = Counter(_tags).most_common(30)
tags = [tag for tag, _ in counts]
k += 100
pred_tags = tags[:10]
pred.append({
"id" : int(self.val_id[uth]),
"songs" : self.pred_songs[uth],
"tags" : pred_tags
})
# if val.songs[uth] == [] and val.tags[uth] == [] -> pred.songs[uth] == [] and pred.tags[uth] == []
# if val.songs[uth] != [] and val.tags[uth] != [] -> pred.songs[uth] != [] and pred.tags[uth] != []
else:
pred.append({
"id" : int(self.val_id[uth]),
"songs" : self.pred_songs[uth],
"tags" : self.pred_tags[uth]
})
if (auto_save == True) and ((uth + 1) % auto_save_step == 0):
self._auto_save(pred, auto_save_fname)
return pd.DataFrame(pred)
def _sim(self, u, v, sim, opt):
'''
u : set (playlist in train data)
v : set (playlist in test data)
sim : string; "cos", "idf", "jaccard" (kind of similarity)
opt : string; "songs", "tags"
'''
if sim == "cos":
if self.sim_normalize:
try:
len(u & v) / ((len(u) ** 0.5) * (len(v) ** 0.5))
except:
return 0
else:
return len(u & v)
elif sim == "idf":
if opt == "songs":
freq = self.freq_songs
elif opt == "tags":
freq = self.freq_tags
freq = freq[list(u & v)]
freq = 1 / (((freq - 1) ** self.rho) + 1) # numpy!
if self.sim_normalize:
try:
return freq.sum() / ((len(u) ** 0.5) * (len(v) ** 0.5))
except:
return 0
else:
return freq.sum()
elif sim == "jaccard":
return len(u & v) / len(u | v)
def _auto_save(self, pred, auto_save_fname):
'''
pred : list of dictionaries
auto_save_fname : string
'''
if not os.path.isdir("./_temp"):
os.mkdir('./_temp')
pd.DataFrame(pred).to_json(f'_temp/{auto_save_fname}.json', orient='records')
if __name__=="__main__":
### 4. modeling : NeighborKNN
### 4.1 hyperparameters: k, rho, weights
### 4.2 parameters: sim_songs, sim_tags, sim_normalize
k = 100
rho = 0.4
weight_val_songs = 0.5
weight_pred_songs = 1 - weight_val_songs
weight_val_tags = 0.5
weight_pred_tags = 1 - weight_val_tags
sim_songs = "idf"
sim_tags = "cos"
sim_normalize = False
### 4.3 run NeighborKNN.predict() : returns pandas.DataFrame
pred = NeighborKNN(k=k, rho=rho, \
weight_val_songs=weight_val_songs, weight_pred_songs=weight_pred_songs, \
weight_val_tags=weight_val_tags, weight_pred_tags=weight_pred_tags, \
sim_songs=sim_songs, sim_tags=sim_tags, sim_normalize=sim_normalize, \
train=train, val=val, song_meta=song_meta, pred=pred).predict(start=0, end=None, auto_save=True)
# print(pred)
### ==============================(save data)==============================
version = NeighborKNN.__version__
version = version[version.find('-') + 1: version.find('.')]
path = "."
fname2 = f"neighbor-knn{version}_k{k}rho{int(rho * 10)}s{int(weight_val_songs * 10)}t{int(weight_val_tags * 10)}_{sim_songs}{sim_tags}{sim_normalize}"
pred.to_json(f'{path}/{fname2}.json', orient='records')
### ======================================================================