Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<train_model>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
rf_best = RandomForestRegressor(random_state = 0,
criterion = 'mae',
max_depth = 11,
n_estimators = 85,
min_samples_leaf = 1,
min_samples_split = 2)
 rf_best.fit(X_train, y_train )<predict_on_test>
train_df = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 test_df = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 sample_sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv' )
rf_pred = rf_best.predict(X_test)
 np.sqrt(mean_squared_log_error(rf_pred, y_test))<choose_model_class>
from sklearn.model_selection import train_test_split
gbr_best = GradientBoostingRegressor(learning_rate = 0.05,
max_depth = 3,
n_estimators = 3000,
min_samples_split = 10,
min_samples_leaf = 2,
loss = 'huber')
 gbr_best.fit(X_train, y_train )<predict_on_test>
from sklearn.model_selection import train_test_split
gbr_pred = gbr_best.predict(X_test)
 np.sqrt(mean_squared_log_error(gbr_pred, y_test))<train_on_grid>
X = train_df.drop('target', axis=1)
 y = train_df['target']
ridge = Ridge()
n_alphas = 200
alphas = np.logspace(-5, 5, n_alphas)
 ridge_params = {
'alpha': alphas
}
ridge_grid= GridSearchCV(ridge, ridge_params, scoring='neg_mean_squared_error', n_jobs = -1, cv=4)
 ridge_grid.fit(X_train, y_train)
 ridge_best = ridge_grid.best_estimator_<predict_on_test>
X_train, X_val, y_train, y_val = train_test_split(X, y,test_size=0.2, random_state=3 )
ridge_pred = ridge_best.predict(X_test)
 np.sqrt(mean_squared_log_error(ridge_pred, y_test))<predict_on_test>
lgb_model = LGBMRegressor(n_estimators = 493,metric='rmse',
reg_alpha = 2, reg_lambda=10, colsample_bytree=0.8 )
vc = VotingRegressor([("gbr", gbr_best), 
("rf", rf_best), 
("ridge", ridge_best)],
weights = [0.6, 0.1, 0.3])
 vc.fit(X_train, y_train)
 vc_pred = vc.predict(X_test)
 np.sqrt(mean_squared_log_error(vc_pred, y_test))<predict_on_test>
lgb_model.fit(X_train, y_train, eval_set=(X_val, y_val))
vc.fit(X, y)
 pred = vc.predict(test_data )<save_to_csv>
y_pred_lgb = lgb_model.predict(test_df )
submission = pd.DataFrame({
"Id": list(range(1461, 1461+len(test_data))),
"SalePrice": pred
})
 submission.to_csv('./submission.csv', index=False )<load_pretrained>
import xgboost as xgb
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...')


<set_options>
params = {'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'eta': 0.01,
'max_depth': 10,
'subsample': 0.6,
'colsample_bytree': 0.6,
'lambda':10,
'alpha':2,
'random_state': 42,
'silent': True}
tr_data = xgb.DMatrix(X_train, y_train)
 va_data = xgb.DMatrix(X_val, y_val)
 watchlist = [(tr_data, 'train'),(va_data, 'valid')]
model_xgb = xgb.train(params, tr_data, 1000, watchlist, maximize=False,verbose_eval=100 )
warnings.simplefilter(action='ignore', category=FutureWarning )<define_variables>
dtest = xgb.DMatrix(test_df)
 y_xgb_pred = model_xgb.predict(dtest, ntree_limit=model_xgb.best_ntree_limit )
DATA_DIRECTORY = ".. /input/home-credit-default-risk"<load_from_csv>
from catboost import CatBoostRegressor
df_train = pd.read_csv(os.path.join(DATA_DIRECTORY, 'application_train.csv'))
df_test = pd.read_csv(os.path.join(DATA_DIRECTORY, 'application_test.csv'))
df = df_train.append(df_test)
 del df_train, df_test; gc.collect()<categorify>
cb_model = CatBoostRegressor(iterations=795,
learning_rate=0.05,
depth=10,
reg_lambda=10,
eval_metric='RMSE',
random_seed = 42,
bagging_temperature = 0.2,
od_type='Iter',
metric_period = 50,
od_wait=20)
 cb_model.fit(X_train, y_train,
eval_set=(X_val, y_val),
use_best_model=True,
verbose=50 )
df = df[df['AMT_INCOME_TOTAL'] < 20000000]
df = df[df['CODE_GENDER'] != 'XNA']
df['DAYS_EMPLOYED'].replace(365243, np.nan, inplace=True)
 df['DAYS_LAST_PHONE_CHANGE'].replace(0, np.nan, inplace=True )<groupby>
cat_pred =cb_model.predict(test_df )
def get_age_group(days_birth):
age_years = -days_birth / 365
if age_years < 27: return 1
elif age_years < 40: return 2
elif age_years < 50: return 3
elif age_years < 65: return 4
elif age_years < 99: return 5
else: return 0<feature_engineering>
sample_sub['target'] =(y_pred_lgb + y_xgb_pred + cat_pred)/ 3
<feature_engineering><EOS> 
sample_sub.to_csv('submission.csv',columns=['id', 'target'], header=True, index=False )