Kaggle competition: WSDM - KKBox's Music Recommendation Challenge
<SOS> metric: AUC Kaggle data source: wsdm-kkboxs-music-recommendation-challenge<drop_column>
os.system('apt-get install p7zip')
 !pip install pyunpack
!pip install patool
directory = '/kaggle/working/'
Archive('/kaggle/input/kkbox-music-recommendation-challenge/train.csv.7z' ).extractall(directory)
 Archive('/kaggle/input/kkbox-music-recommendation-challenge/test.csv.7z' ).extractall(directory)
 Archive('/kaggle/input/kkbox-music-recommendation-challenge/songs.csv.7z' ).extractall(directory)
 Archive('/kaggle/input/kkbox-music-recommendation-challenge/members.csv.7z' ).extractall(directory)
 Archive('/kaggle/input/kkbox-music-recommendation-challenge/song_extra_info.csv.7z' ).extractall(directory)
 train = pd.read_csv('./train.csv')
 test = pd.read_csv('./test.csv')
 songs = pd.read_csv('./songs.csv')
 members = pd.read_csv('./members.csv')
 songs_extra = pd.read_csv('./song_extra_info.csv')
 print('Data preprocessing...')
 dict_count_song_played_train = {k: v for k, v in train['song_id'].value_counts().iteritems() }
dict_count_song_played_test = {k: v for k, v in test['song_id'].value_counts().iteritems() }
def return_number_played(x):
try:
return dict_count_song_played_train[x]
except KeyError:
try:
return dict_count_song_played_test[x]
except KeyError:
return 0
train['number_of_time_played'] = train['song_id'].apply(lambda x: return_number_played(x))
test['number_of_time_played'] = test['song_id'].apply(lambda x: return_number_played(x))
dict_user_activity = {k:v for k,v in pd.concat([train['msno'] , test['msno']] , axis = 0 ).value_counts().iteritems() }
def return_user_activity(x):
try:
return dict_user_activity[x]
except KeyError:
return 0
train['user_activity_msno'] = train['msno'].apply(lambda x: return_user_activity(x))
test['user_activity_msno'] = test['msno'].apply(lambda x: return_user_activity(x))
songs['genre_ids'].fillna('no_genre_id' , inplace= True)

members['registration'] = members['registration_init_time'].apply(lambda x: int(str(x)[0:4])) *12 + members['registration_init_time'].apply(lambda x: int(str(x)[4:6]))
members['expiration'] = members['expiration_date'].apply(lambda x: int(str(x)[0:4])) *12 + members['expiration_date'].apply(lambda x: int(str(x)[4:6]))
members['membership_month'] =(members['expiration_date'].apply(lambda x: int(str(x)[0:4])) *12 + members['expiration_date'].apply(lambda x: int(str(x)[4:6]))
- members['registration_init_time'].apply(lambda x: int(str(x)[0:4])) *12 - members['registration_init_time'].apply(lambda x: int(str(x)[4:6])))
 def isrc_to_year(isrc):
if type(isrc)== str:
if int(isrc[5:7])> 17:
return 1900 + int(isrc[5:7])
 else:
return 2000 + int(isrc[5:7])
 else:
return np.nan
songs_extra['song_year'] = songs_extra['isrc'].apply(isrc_to_year)
 songs_extra.drop(['isrc', 'name'], axis = 1, inplace = True)
 song_cols = ['song_id', 'genre_ids', 'artist_name', 'song_length', 'language']

 train = train.merge(songs[song_cols], on='song_id', how='left')
 test = test.merge(songs[song_cols], on='song_id', how='left')
 members_cols = ['msno', 'city', 'bd', 'gender', 'registration', 'expiration', 'membership_month']
train = train.merge(members[members_cols], on='msno', how='left')
 test = test.merge(members[members_cols], on='msno', how='left')
 train = train.merge(songs_extra, on = 'song_id', how = 'left')
 test = test.merge(songs_extra, on = 'song_id', how = 'left')
 min_max_scaling = ['number_of_time_played', 'user_activity_msno','membership_month', 'song_length']
for f in min_max_scaling:
ms = MinMaxScaler()
train[f] = ms.fit_transform(train[[f]])
 test[f] = ms.transform(test[[f]])
 del members, songs; gc.collect() ;
for col in train.columns:
if train[col].dtype == object:
train[col] = train[col].astype('category')
 test[col] = test[col].astype('category')
 X = train.drop(['target'], axis=1)
 y = train['target'].values
X_test = test.drop(['id'], axis=1)
 ids = test['id'].values
del train, test; gc.collect() ;
print('Training LGBM model...')