Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<concatenate>
import numpy as np
import pandas as pd
test["date_block_num"] = 34
all_data = pd.concat([train, test.drop("ID", axis=1)], ignore_index=True, keys=index_feats)
 all_data = all_data.fillna(0)
 all_data.sample(10 )<merge>
train_data = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 test_data = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 print('Train: ', train_data.shape)
 print('Test: ', test_data.shape )
all_data = pd.merge(all_data, shops, on="shop_id", how="left")
 all_data = pd.merge(all_data, items, on="item_id", how="left")
 all_data = pd.merge(all_data, categories, on="category_id", how="left")
 all_data.sample(10 )<drop_column>
y = train_data['target']
X = train_data.drop(columns=['target', 'id'])
 X_test = test_data.drop(columns='id' )
all_data = downcast1(all_data)
 del shops, items, categories
gc.collect()<merge>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size = 0.05, random_state=22 )
def add_mean_feats(df, mean_feats, index_features, agg_col="item_cnt_month", agg_func="mean"):

 if len(index_features)== 2:
feature_name = index_features[1] + f"_{agg_col}_{agg_func}"
else:
feature_name = index_features[1] + "_" + index_features[2] + f"_{agg_col}_{agg_func}"
group = df.groupby(index_features ).agg({agg_col:agg_func} ).reset_index().rename(columns={agg_col:feature_name})
 df = pd.merge(df, group, on=index_features, how="left")
 df = downcast1(df)
 mean_feats.append(feature_name)
 del group
gc.collect()
return df, mean_feats 
<feature_engineering>
params = {'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'boosting_type': 'gbdt',
'feature_pre_filter': False,
'learning_rate': 0.007,
'num_leaves': 102,
'min_child_samples': 20,
'sub_feature': 0.4,
'sub_row': 1,
'subsample_freq': 0,
'lambda_l1': 4.6,
'lambda_l2': 1.9}
N_FOLDS = 10
kf = KFold(n_splits = N_FOLDS)
 oof = np.zeros(len(y))
oof_vanilla = np.zeros(len(y))
preds = np.zeros(len(X_test))
params['learning_rate'] = 0.005
params['num_iterations'] = 5000
for train_ind, test_ind in tqdm(kf.split(X)) :
Xtrain = X.iloc[train_ind]
Xval = X.iloc[test_ind]
ytrain = y.iloc[train_ind]
yval = y.iloc[test_ind]
model = LGBMRegressor(**params)
 vanilla_model = LGBMRegressor()
model.fit(Xtrain, ytrain, eval_set =(( Xval,yval)) , early_stopping_rounds = 50, verbose = 0)
 vanilla_model.fit(Xtrain, ytrain)
 p = model.predict(Xval)
 p_vanilla = vanilla_model.predict(Xval)
 oof[test_ind] = p
oof_vanilla[test_ind] = p_vanilla
preds += model.predict(X_test)/N_FOLDS
print(f'mean square error on training data(vanilla model): {np.round(mean_squared_error(y, oof_vanilla, squared=False),5)}')
 print(f'mean square error on training data(with tuning): {np.round(mean_squared_error(y, oof, squared=False),5)}' )
item_mean_features = []
all_data, item_mean_features = add_mean_feats(all_data, item_mean_features, ["date_block_num", "item_id"])
 all_data<feature_engineering>
output = pd.DataFrame({"id":test_data.id, "target":preds})
 output.to_csv('submission.csv', index=False )
all_data, item_mean_features = add_mean_feats(all_data, item_mean_features, ["date_block_num", "item_id", "city"] )<feature_engineering>
print('Finish!' )