Kaggle competition: Tabular Playground Series - Feb 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-feb-2021<data_type_conversions>
shap.initjs()
sns.set_style('whitegrid')
 OneHotEncoder, OrdinalEncoder)
delta_prior_question_elapsed_time_mean=train_df['delta_prior_question_elapsed_time'].mean()
train_df['delta_prior_question_elapsed_time'].fillna(delta_prior_question_elapsed_time_mean, inplace=True)
 train_df.delta_prior_question_elapsed_time=train_df.delta_prior_question_elapsed_time.astype('int32' )<data_type_conversions>
df_train = pd.read_csv('.. /input/tabular-playground-series-feb-2021/train.csv')
 df_test = pd.read_csv('.. /input/tabular-playground-series-feb-2021/test.csv')
 df_sub = pd.read_csv('.. /input/tabular-playground-series-feb-2021/sample_submission.csv')
 SEED = 1991
sub_295 = pd.read_csv('.. /input/tp-f-my-subs/submission_001.csv')
 sub_222 = pd.read_csv('.. /input/tp-f-my-subs/submission_021.csv')
 sub_216 = pd.read_csv('.. /input/tp-f-my-subs/submission_216.csv')
 sub_257 = pd.read_csv('.. /input/tp-f-my-subs/submission_257.csv')
 sub_279 = pd.read_csv('.. /input/tp-f-my-subs/submission_012.csv' )
train_df['lag'] = train_df.groupby('user_id')[target].shift()
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 user_agg = train_df.groupby('user_id')['lag'].agg(['sum', 'count'] ).astype('int16')
 cum['cumsum'].fillna(0, inplace=True)
 train_df['user_correctness'] = cum['cumsum'] / cum['cumcount']
train_df['user_correct_count'] = cum['cumsum']
train_df['user_uncorrect_count'] = cum['cumcount']-cum['cumsum']
train_df.drop(columns=['lag'], inplace=True)
 train_df['user_correctness'].fillna(0.67, inplace=True)
 train_df.user_correctness=train_df.user_correctness.astype('float16')
 train_df.user_correct_count=train_df.user_correct_count.astype('int16')
 train_df.user_uncorrect_count=train_df.user_uncorrect_count.astype('int16')
<set_options>
target = df_train['target']
df_train.drop('target', axis=1, inplace=True )
del cum
gc.collect()<data_type_conversions>
len(target[target <= 4] )
<data_type_conversions>
CAT_01 = list(set(CAT ).difference(set(['cat6'])))
 CAT_01
train_df.prior_question_had_explanation=train_df.prior_question_had_explanation.astype('int8')
 explanation_agg = train_df.groupby('user_id')['prior_question_had_explanation'].agg(['sum', 'count'])
 explanation_agg=explanation_agg.astype('int16')
<data_type_conversions>
cum = train_df.groupby('user_id')['prior_question_had_explanation'].agg(['cumsum', 'cumcount'])
 cum['cumcount']=cum['cumcount']+1
train_df['explanation_mean'] = cum['cumsum'] / cum['cumcount']
train_df['explanation_true_count'] = cum['cumsum']
train_df['explanation_false_count'] = cum['cumcount']-cum['cumsum']
train_df.explanation_mean=train_df.explanation_mean.astype('float16')
 train_df.explanation_true_count=train_df.explanation_true_count.astype('int16')
 train_df.explanation_false_count=train_df.explanation_false_count.astype('int16' )<set_options>
del cum
gc.collect()<categorify>
train = df_train.copy()
test = df_test.copy()
train.drop(['id'], axis=1, inplace=True)
 test.drop(['id'], axis=1, inplace=True)
 train.shape, test.shape
content_agg = train_df.groupby('content_id')[target].agg(['sum', 'count','var'])
 task_container_agg = train_df.groupby('task_container_id')[target].agg(['sum', 'count','var'])
 content_agg=content_agg.astype('float32')
 task_container_agg=task_container_agg.astype('float32' )<data_type_conversions>
cols = train.columns.tolist()
ct = ColumnTransformer([('onehot',OrdinalEncoder() , slice(len(CAT))), 
('quantile',QuantileTransformer(random_state=SEED, n_quantiles=1500),
slice(len(CAT),len(CAT)+ len(NUM)+ 5)) ])
 train = ct.fit_transform(train)
 test = ct.transform(test)
 train = pd.DataFrame(train, columns = cols)
 test = pd.DataFrame(test, columns = cols)
 train[CAT] = train[CAT] / 10
test[CAT] = test[CAT] / 10
train_df['task_container_uncor_count'] = train_df['task_container_id'].map(task_container_agg['count']-task_container_agg['sum'] ).astype('int32')
 train_df['task_container_cor_count'] = train_df['task_container_id'].map(task_container_agg['sum'] ).astype('int32')
 train_df['task_container_std'] = train_df['task_container_id'].map(task_container_agg['var'] ).astype('float16')
 train_df['task_container_correctness'] = train_df['task_container_id'].map(task_container_agg['sum'] / task_container_agg['count'])
 train_df.task_container_correctness=train_df.task_container_correctness.astype('float16' )<groupby>
def feat_eng(df):
df['cont001'] = df['cont8'] * df['cont0']
df['cont002'] = df['cont9'] * df['cont0']
df['cont003'] = df['cont9'] * df['cont5']
df['cont004'] = df['cont8'] * df['cont5']
df['cont005'] = df['cont2'] * df['cont4']
df['cat005'] = df['cat2'] * df['cat1']
return df
content_elapsed_time_agg=train_df.groupby('content_id')['prior_question_elapsed_time'].agg(['mean'])
 content_had_explanation_agg=train_df.groupby('content_id')['prior_question_had_explanation'].agg(['mean'] )<train_model>
def feat_eng_01(df):
es = ft.EntitySet(id = 'data')
 original_cols = to_transform
es = es.entity_from_dataframe(entity_id = 'data',
dataframe = df[original_cols],
index = 'id',
time_index = None)
 new_features, new_feature_names = ft.dfs(entityset = es, target_entity = 'data',
trans_primitives = ['multiply_numeric'])
 new_features.reset_index(drop=True, inplace=True)
 new_features.drop(original_cols, axis=1, inplace=True)
 return new_features
print('start questions data...' )<load_from_csv>
questions_df = pd.read_csv(
 '.. /input/riiid-test-answer-prediction/questions.csv',
usecols=[0, 1,3,4],
dtype={'question_id': 'int16','bundle_id': 'int16', 'part': 'int8','tags': 'str'}
)<groupby>
xgb_params = {
'booster':'gbtree',
'n_estimators':20000,
'max_depth':5,
'eta':0.01,
'gamma':2.8,
'objective':'reg:squarederror',
'verbosity':0,
'subsample':0.85,
'colsample_bytree':0.45,
'lambda':5,
'scale_pos_weight':1,
'objective':'reg:squarederror',
'eval_metric':'rmse',
'tree_method':'gpu_hist',
'gpu_id':0
}
bundle_agg = questions_df.groupby('bundle_id')['question_id'].agg(['count'] )<data_type_conversions>
N_FOLDS = 10
kf = KFold(n_splits = N_FOLDS)
 oof = np.zeros(len(target))
preds_folds_xgb = np.zeros(len(test))
for train_ind, test_ind in tqdm(kf.split(train)) :
X_train = train.iloc[train_ind]
X_val = train.iloc[test_ind]
y_train = target.iloc[train_ind]
y_val = target.iloc[test_ind]
model = XGBRegressor(**xgb_params)
 model.fit(X_train, y_train, eval_set = [(X_val, y_val)], early_stopping_rounds = 50, verbose = False)
 p = model.predict(X_val)
 oof[test_ind] = p
preds_folds_xgb += model.predict(test)/N_FOLDS
questions_df['content_sub_bundle'] = questions_df['bundle_id'].map(bundle_agg['count'] ).astype('int8' )<set_options>
print(f'rmse on oof: {np.round(mean_squared_error(target, oof, squared=False),5)}' )
questions_df['tags'].fillna('188', inplace=True )<string_transform>
def gettags(tags,num):
tags_splits=tags.split(" ")
 result=''
for t in tags_splits:
x=int(t)
 if(x<32*(num+1)and x>=32*num):
result=result+' '+t
return result<categorify>
avg_pred_01 = sub_216['target'] * 1.3 - preds_folds_xgb * 0.1 - sub_222['target'] * 0.2
for num in range(0,6):
questions_df["tags"+str(num)] = questions_df["tags"].apply(lambda row: gettags(row,num))
le = LabelEncoder()
le.fit(np.unique(questions_df['tags'+str(num)].values))
questions_df['tags'+str(num)]=questions_df[['tags'+str(num)]].apply(le.transform )<data_type_conversions>
df_sub['target'] = avg_pred_01
df_sub.to_csv('submission_035.csv', index=False )
questions_df_dict = {
'tags0': 'int8',
'tags1': 'int8',
'tags2': 'int8',
'tags3': 'int8',
'tags4': 'int8',
'tags5': 'int8',
}
questions_df = questions_df.astype(questions_df_dict )<drop_column>
lgb_params_kfold = {
'n_estimators':3000,
'learning_rate': 0.004,
'min_data_per_group': 5,
'boosting_type': 'gbdt',
'num_leaves': 256,
'max_depth': -1,
'lambda_l1': 4.5,
'lambda_l2': 1.2,
'metric': 'rmse',
'cat_smooth': 1.0,
'silent': True,
'importance_type': 'split',
'feature_pre_filter': False,
'bagging_fraction': 0.85,
'min_data_in_leaf': 100,
'min_sum_hessian_in_leaf': 0.001,
'bagging_freq': 7,
'feature_fraction': 0.5,
'min_gain_to_split': 0.0,
'min_child_samples': 20,
'n_jobs': -1,
'random_state': SEED}
xgb_params = {
'booster':'gbtree',
'n_estimators':10000,
'max_depth':5,
'eta':0.006,
'gamma':1.2,
'objective':'reg:squarederror',
'verbosity':0,
'subsample':0.85,
'colsample_bytree':0.8,
'lambda':4.5,
'alpha':1.2,
'scale_pos_weight':1,
'objective':'reg:squarederror',
'eval_metric':'rmse',
'seed':SEED,
'tree_method':'gpu_hist',
'gpu_id':0
}
xgb_params = {
'booster':'gbtree',
'n_estimators':10000,
'max_depth':7,
'eta':0.01,
'gamma':1.8,
'objective':'reg:squarederror',
'verbosity':0,
'subsample':0.85,
'colsample_bytree':0.4,
'lambda':2.7,
'alpha':6,
'scale_pos_weight':1,
'objective':'reg:squarederror',
'eval_metric':'rmse',
'seed':SEED,
'tree_method':'gpu_hist',
'gpu_id':0
}