Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<drop_column>
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
import sklearn.datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import optuna
import lightgbm as lgb
import xgboost as xgb
import seaborn as sns
import matplotlib.pyplot as plt
cols = list(train.columns)
 for c in list(set(['D6','D7','D8','D9','D12','D13','D14'])& set(cols)) :
cols.remove(c)
 for c in list(set(['C3','M5','id_08','id_33'])& set(cols)) :
cols.remove(c)
 for c in list(set(['card4','id_07','id_14','id_21','id_30','id_32','id_34'])& set(cols)) :
cols.remove(c)
 for c in list(set(['id_'+str(x)for x in range(22,28)])& set(cols)) :
cols.remove(c )<create_dataframe>
train = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv')
 features = [f'cont{x}'for x in range(1,15)]
data = train[features]
X_test = test[features]
target = train['target']
train.head()
final_train = train[cols].copy()
final_test = test[list(set(cols)- set(['isFraud'])) ].copy()
print(f'Our Final Train dataset has {final_train.shape[0]} rows and {final_train.shape[1]} columns.')
 print(f'Our Final Test dataset has {final_test.shape[0]} rows and {final_test.shape[1]} columns.' )<drop_column>
Best_trial = {'lambda': 0.0030282073258141168, 'alpha': 0.01563845128469084, 'colsample_bytree': 0.5,
'subsample': 0.7,'n_estimators': 4000, 'learning_rate': 0.01,'max_depth': 15,
'random_state': 2020, 'min_child_weight': 257,'tree_method':'gpu_hist'
,'predictor': 'gpu_predictor'}
train = train_data
test = test_data
preds = np.zeros(test.shape[0])
 kf = KFold(n_splits=7,random_state=48,shuffle=True)
 rmse=[]
n=0
for trn_idx, test_idx in kf.split(train[features],target):
X_tr,X_val=train[features].iloc[trn_idx],train[features].iloc[test_idx]
y_tr,y_val=target.iloc[trn_idx],target.iloc[test_idx]
model = xgb.XGBRegressor(**Best_trial)
 model.fit(X_tr,y_tr,eval_set=[(X_val,y_val)],early_stopping_rounds=100,verbose=False)
 preds+=model.predict(test[features])/kf.n_splits
rmse.append(mean_squared_error(y_val, model.predict(X_val), squared=False))
print(n+1,rmse[n])
 n+=1
print(f"mean RMSE for all the folds is {np.mean(rmse)}" )
del train, test<define_variables>
sub1 = pd.read_csv('.. /input/results-driven-tabular-playground-series-201/submission.csv')
 predictions1 = sub1['target'].tolist()
results = [x*0.66 + y*0.34 for x, y in zip(predictions1, preds)]
idxT = X_train.index[:3*len(X_train)//4]
idxV = X_train.index[3*len(X_train)//4:]<count_values>
Best_trial ={'random_state': 33,'n_estimators':5000,
'min_data_per_group': 5,
'boosting_type': 'gbdt',
'device_type' : 'gpu',
'num_leaves': 256,
'num_iterations' : 5000,
'max_dept': -1,
'learning_rate': 0.005,
'subsample_for_bin': 200000,
'lambda_l1': 1.074622455507616e-05,
'lambda_l2': 2.0521330798729704e-06,
'n_jobs': -1,
'cat_smooth': 1.0,
'silent': True,
'importance_type': 'split',
'metric': 'rmse',
'feature_pre_filter': False,
'bagging_fraction': 0.8206341150202605,
'min_data_in_leaf': 100,
'min_sum_hessian_in_leaf': 0.001,
'bagging_freq': 6,
'feature_fraction': 0.5,
'min_gain_to_split': 0.0,
'min_child_samples': 20}
y_train.value_counts() /y_train.shape[0]<train_model>
preds = np.zeros(test.shape[0])
 kf = KFold(n_splits=10,random_state=48,shuffle=True)
 rmse=[]
n=0
for trn_idx, test_idx in kf.split(train[features],target):
X_tr,X_val=train[features].iloc[trn_idx],train[features].iloc[test_idx]
y_tr,y_val=target.iloc[trn_idx],target.iloc[test_idx]
model = lgb.LGBMRegressor(**Best_trial)
 model.fit(X_tr,y_tr,eval_set=[(X_val,y_val)],early_stopping_rounds=50,verbose=False)
 preds+=model.predict(test[features])/kf.n_splits
rmse.append(mean_squared_error(y_val, model.predict(X_val), squared=False))
print(n+1,rmse[n])
 n+=1
print(f"mean RMSE for all the folds is {np.mean(rmse)}" )
<prepare_x_and_y><EOS> 
final_results = [x*0.72 + y*0.28 for x, y in zip(results, preds)]
sub['target']=final_results
sub.to_csv('submission.csv', index=False )