Kaggle competition: Tabular Playground Series - Jan 2021
study = optuna.create_study(direction='minimize')
 study.optimize(objective, n_trials=50)
 print('Number of finished trials:', len(study.trials))
print('Best trial:', study.best_trial.params )<init_hyperparams>
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 test = pd.read_csv(input_path / 'test.csv', index_col='id')
 submission = pd.read_csv(input_path / 'sample_submission.csv', index_col='id' )
params = {
'objective': 'rmse',
'metric': 'rmse',
'num_leaves': 1012,
'min_data_in_leaf':10,
'feature_fraction':0.622351664881,
'learning_rate': 0.01,
'num_rounds': 1000,
'early_stopping_rounds': 30,
'seed': 1
}
feature_name_indexes = [
'country_part',
'item_category_common',
'item_category_code',
'city_code',
]
lgb_train = lgb.Dataset(X_train[feature_name], Y_train)
 lgb_eval = lgb.Dataset(X_valid[feature_name], Y_valid, reference=lgb_train)
 evals_result = {}
gbm = lgb.train(
 params,
lgb_train,
num_boost_round=3000,
valid_sets=(lgb_train, lgb_eval),
feature_name = feature_name,
categorical_feature = feature_name_indexes,
verbose_eval=50,
evals_result = evals_result,
)<save_to_csv>
target = train.pop('target')
 X_train, X_test, y_train, y_test = train_test_split(train, target, train_size=0.8 )
test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv')
 Y_test = gbm.predict(X_test[feature_name] ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('gbm_submission.csv', index=False )<load_from_csv>
parameters = {
'n_estimators': 350,
'tree_method': 'hist',
'learning_rate': 0.03,
'colsample_bytree': 0.9,
'subsample': 0.9,
'min_child_weight': 9,
'max_depth': 11,
'n_jobs': -1
}
categories = pd.read_csv(".. /input/eng-translations/categories_eng.csv")
 items = pd.read_csv(".. /input/eng-translations/items_eng.csv")
 sales = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sales_train.csv")
 test = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/test.csv")
 shops = pd.read_csv(".. /input/eng-translations/shops_eng.csv")
 submission = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sample_submission.csv" )<data_type_conversions>
parameters2 = {
'n_estimators': 350,
'tree_method': 'exact',
'learning_rate': 0.03,
'colsample_bytree': 0.9,
'subsample': 0.9,
'min_child_weight': 9,
'max_depth': 11,
'n_jobs': -1
}
def downcast1(df, verbose=True):

 start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
dtype_name = df[col].dtype.name
if dtype_name == 'object':
pass
elif dtype_name == 'bool':
df[col] = df[col].astype('int8')
 elif dtype_name.startswith('int')or(df[col].round() == df[col] ).all() :
df[col] = pd.to_numeric(df[col], downcast='integer')
 else:
df[col] = pd.to_numeric(df[col], downcast='float')
 end_mem = df.memory_usage().sum() / 1024**2
if verbose:
print('{:.1f}% compressed'.format(100 *(start_mem - end_mem)/ start_mem))
return df<define_variables>
def cleans(i):

 pattern = r'[A-Za-z0-9]+'
finds = re.findall(pattern, str(i))
stringy = ""
for j in finds:
stringy += f" {j}"
return stringy<feature_engineering>
shops["clean"] = shops["shop_name"].apply(cleans)
 shops.head()<feature_engineering>
final_model = XGBRegressor(tree_method='hist', min_child_weight=9, max_depth=11, n_jobs=-1, colsample_bytree=0.5, learning_rate=0.01, n_estimators=1500)
 final_model.fit(X_train, y_train, early_stopping_rounds=10, eval_set=[(X_test, y_test)], verbose=False)
 prediction = final_model.predict(X_test)
 mse = mean_squared_error(y_test, prediction,squared=False)
 print(mse)
sales.loc[sales["shop_id"]==0, "shop_id"] = 57
sales.loc[sales["shop_id"]==1, "shop_id"] = 58
sales.loc[sales["shop_id"]==10, "shop_id"] = 11
sales.loc[sales["shop_id"]==39, "shop_id"] = 40
test.loc[test['shop_id'] == 0, 'shop_id'] = 57
test.loc[test['shop_id'] == 1, 'shop_id'] = 58
test.loc[test['shop_id'] == 10, 'shop_id'] = 11
test.loc[test['shop_id'] == 39, 'shop_id'] = 40<count_unique_values>
submission['target'] = final_model.predict(test)
 submission.to_csv('xgb_reg.csv' )