Kaggle competition: Tabular Playground Series - Feb 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-feb-2021<load_from_csv>
%matplotlib inline
sns.set_style("whitegrid")
 !pip install pycaret
warnings.filterwarnings("ignore" )
lectures_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/lectures.csv' )<categorify>
train = pd.read_csv("/kaggle/input/tabular-playground-series-feb-2021/train.csv")
 test = pd.read_csv(".. /input/tabular-playground-series-feb-2021/test.csv" )
lectures_df['type_of'] = lectures_df['type_of'].replace('solving question', 'solving_question')
 lectures_df = pd.get_dummies(lectures_df, columns=['part', 'type_of'])
 part_lectures_columns = [column for column in lectures_df.columns if column.startswith('part')]
types_of_lectures_columns = [column for column in lectures_df.columns if column.startswith('type_of_')]<merge>
for col in categorical_features:
le = LabelEncoder()
train[col] = le.fit_transform(train[col])
 test[col] = le.fit_transform(test[col] )
train_lectures = train_df[train_df.content_type_id == True].merge(lectures_df, left_on='content_id', right_on='lecture_id', how='left' )<groupby>
continous_features = train.iloc[:, 11:25].columns
for i in continous_features:
xt = boxcox1p(train[i], [0])
 train["boxcox"+ i] = xt
user_lecture_stats_part = train_lectures.groupby('user_id',as_index = False)[part_lectures_columns + types_of_lectures_columns].sum()<data_type_conversions>
for i in continous_features:
xt = boxcox1p(test[i], [0])
 test["boxcox"+ i] = xt
lecturedata_types_dict = {
'user_id': 'int32',
'part_1': 'int8',
'part_2': 'int8',
'part_3': 'int8',
'part_4': 'int8',
'part_5': 'int8',
'part_6': 'int8',
'part_7': 'int8',
'type_of_concept': 'int8',
'type_of_intention': 'int8',
'type_of_solving_question': 'int8',
'type_of_starter': 'int8'
}
user_lecture_stats_part = user_lecture_stats_part.astype(lecturedata_types_dict )<data_type_conversions>
train_transform = pd.concat([train.iloc[:, 1:11], train.iloc[:, 26:]], axis=1)
 train_transform.columns
for column in user_lecture_stats_part.columns:
if(column !='user_id'):
user_lecture_stats_part[column] =(user_lecture_stats_part[column] > 0 ).astype('int8' )<filter>
X = train_transform
y = train["target"]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2 )
train_lectures[train_lectures.user_id==5382]<filter>
def K_fold_CV(X, y, model, params):
rmse_error = []
skf = KFold(n_splits=5, shuffle=True, random_state=42)
 for fold,(tr_idx, ts_idx)in enumerate(skf.split(X, y)) :
print(f"Fold: {fold}")
 x_tr, y_tr = X.iloc[tr_idx], y.iloc[tr_idx]
x_ts, y_ts = X.iloc[ts_idx], y.iloc[ts_idx]
reg = model(**params)
 reg.fit(x_tr, y_tr,
eval_set=[(x_ts, y_ts)],
early_stopping_rounds=100,
verbose=False)
 error = mean_squared_error(y_ts, reg.predict(x_ts))
rmse_error.append(error)
 print(f"RMSE Error: {error}")
 print("-"*25)
 return reg, np.mean(rmse_error )
user_lecture_stats_part[user_lecture_stats_part.user_id==5382]<drop_column>
param_lgb = {
"objective":"regression",
"metric":"rmse",
"feature_pre_filter":False,
"learning_rate":0.05,
"num_leaves":256,
"min_child_samples":20,
"sub_feature":0.4,
"sub_row":1,
"subsample_freq":0,
"lambda_l1":4.6,
"lambda_l2":1.9
}
del(train_lectures )<categorify>
%%time
model_lgb, lgb_error = K_fold_CV(X, y, LGBMRegressor, params=param_lgb )
cum = train_df.groupby('user_id')['content_type_id'].agg(['cumsum', 'cumcount'])
 train_df['user_lecture_cumsum'] = cum['cumsum']
train_df['user_lecture_lv'] = cum['cumsum'] / cum['cumcount']
train_df.user_lecture_lv=train_df.user_lecture_lv.astype('float16')
 train_df.user_lecture_cumsum=train_df.user_lecture_cumsum.astype('int8')
 user_lecture_agg = train_df.groupby('user_id')['content_type_id'].agg(['sum', 'count'] )<data_type_conversions>
param_cbr = {
'loss_function': 'RMSE',
"max_depth": 4,
'learning_rate': 0.03,
"bootstrap_type": 'Poisson',
"subsample": 0.8,
"border_count": 512,
"l2_leaf_reg": 200,
'random_state': 42,
"thread_count": 2,
'num_boost_round': 50000,
"task_type": "GPU",
"devices" : "0",
}
train_df['prior_question_had_explanation'].fillna(False, inplace=True)
 train_df = train_df.astype(data_types_dict)
 train_df = train_df[train_df[target] != -1].reset_index(drop=True)
 prior_question_elapsed_time_mean=train_df['prior_question_elapsed_time'].mean()
train_df['prior_question_elapsed_time'].fillna(prior_question_elapsed_time_mean, inplace=True )<groupby>
%%time
cbr, cbr_error = K_fold_CV(X, y, CatBoostRegressor, params=param_cbr )
max_timestamp_u = train_df[['user_id','timestamp']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u.columns = ['user_id', 'max_time_stamp']<data_type_conversions>
param_xgb = {'n_estimators': 1811,
'reg_alpha': 0.02641656905930228,
'reg_lambda': 9.219592384947825,
'colsample_bytree': 0.3,
'subsample': 1.0,
'learning_rate': 0.017,
'max_depth': 5,
'num_leaves': 154,
'min_child_samples': 47,
'min_data_per_groups': 96,
'tree_method': "gpu_hist",
"gpu_id": 0}
train_df['lagtime'] = train_df.groupby('user_id')['timestamp'].shift()
train_df['lagtime']=train_df['timestamp']-train_df['lagtime']
train_df['lagtime'].fillna(0, inplace=True)
 train_df.lagtime=train_df.lagtime.astype('int32')
<categorify>
model_xgb, xgb_error = K_fold_CV(X, y, XGBRegressor, params=param_xgb )
lagtime_agg = train_df.groupby('user_id')['lagtime'].agg(['mean'])
 train_df['lagtime_mean'] = train_df['user_id'].map(lagtime_agg['mean'])
 train_df.lagtime_mean=train_df.lagtime_mean.astype('int32' )<groupby>
submission = pd.read_csv(".. /input/tabular-playground-series-feb-2021/sample_submission.csv" )
user_prior_question_elapsed_time = train_df[['user_id','prior_question_elapsed_time']].groupby(['user_id'] ).tail(1)
 user_prior_question_elapsed_time.columns = ['user_id', 'prior_question_elapsed_time']<feature_engineering>
pred = cbr.predict(test[X.columns])
 submission["target"] = pred
train_df['delta_prior_question_elapsed_time'] = train_df.groupby('user_id')['prior_question_elapsed_time'].shift()
train_df['delta_prior_question_elapsed_time']=train_df['prior_question_elapsed_time']-train_df['delta_prior_question_elapsed_time']
train_df['delta_prior_question_elapsed_time'].fillna(0, inplace=True )<data_type_conversions>
submission.to_csv("submission.csv", index=False )
train_df.delta_prior_question_elapsed_time=train_df.delta_prior_question_elapsed_time.astype('int32' )<data_type_conversions>
submission.to_csv("submission.csv", index=False )