Kaggle competition: WSDM - KKBox's Music Recommendation Challenge
combined['Cabin'].value_counts()<define_variables>
print('Loading data...')
 data_path = '.. /input/'
train = pd.read_csv(data_path + 'train.csv', dtype={'msno' : 'category',
'source_system_tab' : 'category',
'source_screen_name' : 'category',
'source_type' : 'category',
'target' : np.uint8,
'song_id' : 'category'})
 test = pd.read_csv(data_path + 'test.csv', dtype={'msno' : 'category',
'source_system_tab' : 'category',
'source_screen_name' : 'category',
'source_type' : 'category',
'song_id' : 'category'})
 songs = pd.read_csv(data_path + 'songs.csv',dtype={'genre_ids': 'category',
'language' : 'category',
'artist_name' : 'category',
'composer' : 'category',
'lyricist' : 'category',
'song_id' : 'category'})
 members = pd.read_csv(data_path + 'members.csv',
dtype={'city' : 'category',
'bd' : np.uint8,
'gender' : 'category',
'registered_via' : 'category'}
,parse_dates=["registration_init_time","expiration_date"])
 songs_extra = pd.read_csv(data_path + 'song_extra_info.csv')
 print('Data preprocessing...' )
cabin_category = {'A':9, 'B':8, 'C':7, 'D':6, 'E':5, 'F':4, 'G':3, 'T':2, 'U':1}<categorify>
song_cols = ['song_id', 'artist_name', 'genre_ids', '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['registration_year'] = members['registration_init_time'].apply(lambda x: int(str(x)[0:4]))
members['expiration_year'] = members['expiration_date'].apply(lambda x: int(str(x)[0:4]))
members['expiration_month'] = members['expiration_date'].apply(lambda x: int(str(x)[4:6]))
members['membership_days'] =(members['expiration_date'] - members['registration_init_time'] ).dt.days.astype(int)
combined['Cabin'] = combined['Cabin'].map(cabin_category )<define_variables>
members['membership_days'] = members['membership_days']//200
members['membership_days'] = members['membership_days'].astype('category' )
dummy_col=['Title', 'Sex', 'Age_cat', 'SibSp', 'Parch', 'Fare_cat', 'Cabin', 'Embarked', 'Pclass', 'FamilySize_cat']<categorify>
member_cols = ['msno','city','registered_via', 'registration_year', 'expiration_year', 'membership_days']
train = train.merge(members[member_cols], on='msno', how='left')
 test = test.merge(members[member_cols], on='msno', how='left' )
dummy = pd.get_dummies(combined[dummy_col], columns=dummy_col, drop_first=False )<concatenate>
def isrc_to_year(isrc):
if type(isrc)== str:
if int(isrc[5:7])> 17:
return int(isrc[5:7])//5
else:
return int(isrc[5:7])//5
else:
return np.nan
songs_extra['song_year'] = songs_extra['isrc'].apply(isrc_to_year)
 songs_extra.drop(['isrc', 'name'], axis = 1, inplace = True )
combined = pd.concat([dummy, combined], axis = 1 )<feature_engineering>
train = train.merge(songs_extra, on = 'song_id', how = 'left')
 test = test.merge(songs_extra, on = 'song_id', how = 'left' )
combined['FareCat_Sex'] = combined['Fare_cat']*combined['Sex']
combined['Pcl_Sex'] = combined['Pclass']*combined['Sex']
combined['Pcl_Title'] = combined['Pclass']*combined['Title']
combined['Age_cat_Sex'] = combined['Age_cat']*combined['Sex']
combined['Age_cat_Pclass'] = combined['Age_cat']*combined['Pclass']
combined['Title_Sex'] = combined['Title']*combined['Sex']
combined['Age_Fare'] = combined['Age_cat']*combined['Fare_cat']
combined['SmallF'] = combined['FamilySize'].map(lambda s: 1 if s == 2 else 0)
 combined['MedF'] = combined['FamilySize'].map(lambda s: 1 if 3 <= s <= 4 else 0)
 combined['LargeF'] = combined['FamilySize'].map(lambda s: 1 if s >= 5 else 0)
 combined['Senior'] = combined['Age'].map(lambda s:1 if s>70 else 0 )<prepare_x_and_y>
train['genre_ids'] = train['genre_ids'].str.split('|' ).str[0]
X_train = combined[:train.shape[0]]
X_test = combined[train.shape[0]:]
y = targets
X_train['Y'] = y
df = X_train
X = df.drop('Y', axis=1)
 y = df.Y<import_modules>
temp_song_length = train['song_length']
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score<split>
train.drop('song_length', axis = 1, inplace = True)
 test.drop('song_length',axis = 1 , inplace =True )
x_train, x_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=10 )<prepare_x_and_y>
song_count = train.loc[:,["song_id","target"]]
song_count1 = song_count.groupby(["song_id"],as_index=False ).sum().rename(columns={"target":"repeat_count"})
 song_count2 = song_count.groupby(["song_id"],as_index=False ).count().rename(columns = {"target":"play_count"} )
d_train = xgb.DMatrix(x_train, label=y_train)
 d_valid = xgb.DMatrix(x_valid, label=y_valid)
 d_test = xgb.DMatrix(X_test )<init_hyperparams>
song_repeat = song_count1.merge(song_count2,how="inner",on="song_id")
 song_repeat["repeat_percentage"] = round(( song_repeat['repeat_count']*100)/ song_repeat['play_count'],1)
 song_repeat['repeat_count'] = song_repeat['repeat_count'].astype('int')
 song_repeat['repeat_percentage'] = song_repeat['repeat_percentage'].replace(100.0,np.nan)
params = {
'objective':'binary:logistic',
'eta': 0.3,
'max_depth':9,
'learning_rate':0.03,
'eval_metric':'auc',
'min_child_weight':1,
'subsample':1,
'colsample_bytree':0.4,
'seed':29,
'reg_lambda':2.8,
'reg_alpha':0,
'gamma':0,
'scale_pos_weight':1,
'n_estimators': 600,
'nthread':-1
}<define_variables>
train = train.merge(song_repeat,on="song_id",how="left")
 test = test.merge(song_repeat,on="song_id",how="left" )
watchlist = [(d_train, 'train'),(d_valid, 'valid')]
nrounds=10000<train_model>
test['song_id'] = test['song_id'].astype('category')
 test['repeat_count'] = test['repeat_count'].fillna(0)
 test['repeat_count'] = test['repeat_count'].astype('int')
 test['play_count'] = test['play_count'].fillna(0)
 test['play_count'] = test['play_count'].astype('int' )
model = xgb.train(params, d_train, nrounds, watchlist, early_stopping_rounds=600,
maximize=True, verbose_eval=10 )<categorify>
artist_count = train.loc[:,["artist_name","target"]]
artist_count1 = artist_count.groupby(["artist_name"],as_index=False ).sum().rename(columns={"target":"repeat_count_artist"})
 artist_count2 = artist_count.groupby(["artist_name"],as_index=False ).count().rename(columns = {"target":"play_count_artist"})
 artist_repeat = artist_count1.merge(artist_count2,how="inner",on="artist_name" )
leaks = {
897:1,
899:1,
930:1,
932:1,
949:1,
987:1,
995:1,
998:1,
999:1,
1016:1,
1047:1,
1083:1,
1097:1,
1099:1,
1103:1,
1115:1,
1118:1,
1135:1,
1143:1,
1152:1,
1153:1,
1171:1,
1182:1,
1192:1,
1203:1,
1233:1,
1250:1,
1264:1,
1286:1,
935:0,
957:0,
972:0,
988:0,
1004:0,
1006:0,
1011:0,
1105:0,
1130:0,
1138:0,
1173:0,
1284:0,
}<save_to_csv>
artist_repeat["repeat_percentage_artist"] = round(( artist_repeat['repeat_count_artist']*100)/ artist_repeat['play_count_artist'],1)
 artist_repeat['repeat_count_artist'] = artist_repeat['repeat_count_artist'].fillna(0)
 artist_repeat['repeat_count_artist'] = artist_repeat['repeat_count_artist'].astype('int')
 artist_repeat['repeat_percentage_artist'] = artist_repeat['repeat_percentage_artist'].replace(100.0,np.nan )
sub = pd.DataFrame()
sub['PassengerId'] = test['PassengerId']
sub['Survived'] = model.predict(d_test)
 sub['Survived'] = sub['Survived'].apply(lambda x: 1 if x>0.8 else 0)
 sub['Survived'] = sub.apply(lambda r: leaks[int(r['PassengerId'])] if int(r['PassengerId'])in leaks else r['Survived'], axis=1)
 sub.to_csv('sub_titan.csv', index=False )<load_from_csv>
del artist_repeat['repeat_count_artist']
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<drop_column>
train = train.merge(artist_repeat,on="artist_name",how="left")
 test = test.merge(artist_repeat,on="artist_name",how="left" )
train_data = train_data.drop(columns=['Name','Cabin'])
 train_data['family_member'] = train_data['SibSp'] + train_data['Parch']
train_data = train_data.drop(columns=['SibSp', 'Parch'] )<drop_column>
del train['artist_name']
del test['artist_name']
test_data = test_data.drop(columns=['Name','Cabin'])
 test_data['family_member'] = test_data['SibSp'] + test_data['Parch']
test_data = test_data.drop(columns=['SibSp', 'Parch'] )<train_model>
msno_count = train.loc[:,["msno","target"]]
msno_count1 = msno_count.groupby(["msno"],as_index=False ).sum().rename(columns={"target":"repeat_count_msno"})
 msno_count2 = msno_count.groupby(["msno"],as_index=False ).count().rename(columns = {"target":"play_count_msno"})
 msno_repeat = msno_count1.merge(msno_count2,how="inner",on="msno" )
train_data.fillna(0,inplace=True)
 test_data.fillna(0,inplace=True )<prepare_x_and_y>
msno_repeat["repeat_percentage_msno"] = round(( msno_repeat['repeat_count_msno']*100)/ msno_repeat['play_count_msno'],1)
 msno_repeat['repeat_count_msno'] = msno_repeat['repeat_count_msno'].fillna(0)
 msno_repeat['repeat_count_msno'] = msno_repeat['repeat_count_msno'].astype('int')
X = train_data.drop(columns=['Survived'])
 Y = train_data['Survived']
cate_features_index = np.where(X.dtypes != float)[0] 
<import_modules>
train = train.merge(msno_repeat,on="msno",how="left")
 test = test.merge(msno_repeat,on="msno",how="left" )
from catboost import CatBoostClassifier, cv, Pool<import_modules>
from sklearn.model_selection import train_test_split<import_modules>
for col in train.columns:
if train[col].dtype == object:
train[col] = train[col].astype('category')
 test[col] = test[col].astype('category' )
from sklearn.model_selection import train_test_split<split>
train['song_year'] = train['song_year'].astype('category')
 test['song_year'] = test['song_year'].astype('category' )
xtrain,xtest,ytrain,ytest = train_test_split(X,Y,train_size=0.82,random_state=42 )<choose_model_class>
drop_list = ['repeat_count','repeat_percentage',
'repeat_percentage_artist',
'repeat_count_msno','repeat_percentage_msno'
]
train = train.drop(drop_list,axis=1)
 test = test.drop(drop_list,axis=1 )
clf =CatBoostClassifier(eval_metric='Accuracy',use_best_model=True,random_seed=42 )<train_model>
test['play_count_msno'] = test['play_count_msno'].fillna(0)
 test['play_count_msno'] = test['play_count_msno'].astype('int')
 train['play_count_artist'] = train['play_count_artist'].fillna(0)
 test['play_count_artist'] = test['play_count_artist'].fillna(0)
 train['play_count_artist'] = train['play_count_artist'].astype('int')
 test['play_count_artist'] = test['play_count_artist'].astype('int')
<define_variables><EOS> 
kf = KFold(n_splits=3)
 predictions = np.zeros(shape=[len(test)])
 for train_indices ,validate_indices in kf.split(train):
train_data = lgb.Dataset(train.drop(['target'],axis=1 ).loc[train_indices,:],label=train.loc[train_indices,'target'])
 val_data = lgb.Dataset(train.drop(['target'],axis=1 ).loc[validate_indices,:],label=train.loc[validate_indices,'target'])
 params = {
'objective': 'binary',
'boosting': 'gbdt',
'learning_rate': 0.2 ,
'verbose': 0,
'num_leaves': 2**8,
'bagging_fraction': 0.95,
'bagging_freq': 1,
'bagging_seed': 1,
'feature_fraction': 0.9,
'feature_fraction_seed': 1,
'max_bin': 256,
'num_rounds': 80,
'metric' : 'auc'
}
lgbm_model = lgb.train(params, train_data, 100, valid_sets=[val_data])
 predictions += lgbm_model.predict(test.drop(['id'],axis=1))
del lgbm_model
predictions = predictions/3
INPUT_DATA_PATH = '.. /input/'
submission = pd.read_csv(INPUT_DATA_PATH + '/sample_submission.csv')
 submission.target=predictions
submission.to_csv('submission.csv',index=False )