Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<import_modules>
import optuna
import xgboost as xgb
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
<init_hyperparams>
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' )
best_params = {'bagging_fraction': 0.5818772519688797,
'colsample_bytree': 0.3035307099891744,
'feature_fraction': 0.795967379488282,
'gamma': 0.6896677451866189,
'learning_rate': 0.011336192527320772,
'max_depth': 20,
'min_child_samples': 140,
'num_leaves': 230,
'reg_alpha': 0.06035695642758,
'reg_lambda': 0.012734543098346575,
'subsample': 0.7}<prepare_x_and_y>
columns = [col for col in train.columns.to_list() if col not in ['id','target']]
X_train = final_train.copy()
y_train = X_train.pop('isFraud')
 X_test = final_test.copy()
X_train,X_test = X_train.align(other=X_test,join='left', axis=1 )<train_model>
data=train[columns]
target=train['target']
print("XGBoost version:", xgb.__version__)
 clf = xgb.XGBClassifier(
 n_estimators=2000,
**best_params,
tree_method='gpu_hist',
verbose=50,
early_stopping_rounds=100 
)
 clf.fit(X_train, y_train)
 y_preds = clf.predict_proba(X_test)[:,1]<save_to_csv>
def objective(trial,data=data,target=target):
train_x, test_x, train_y, test_y = train_test_split(data, target, test_size=0.15,random_state=42)
 param = {
'tree_method':'gpu_hist',
'lambda': trial.suggest_loguniform('lambda', 1e-3, 10.0),
'alpha': trial.suggest_loguniform('alpha', 1e-3, 10.0),
'colsample_bytree': trial.suggest_categorical('colsample_bytree', [0.3,0.4,0.5,0.6,0.7,0.8,0.9, 1.0]),
'subsample': trial.suggest_categorical('subsample', [0.4,0.5,0.6,0.7,0.8,1.0]),
'learning_rate': trial.suggest_categorical('learning_rate', [0.008,0.009,0.01,0.012,0.014,0.016,0.018, 0.02]),
'n_estimators': 4000,
'max_depth': trial.suggest_categorical('max_depth', [5,7,9,11,13,15,17,20]),
'random_state': trial.suggest_categorical('random_state', [24, 48,2020]),
'min_child_weight': trial.suggest_int('min_child_weight', 1, 300),
}
model = xgb.XGBRegressor(**param)
 model.fit(train_x,train_y,eval_set=[(test_x,test_y)],early_stopping_rounds=100,verbose=False)
 preds = model.predict(test_x)
 rmse = mean_squared_error(test_y, preds,squared=False)
 return rmse
sub['isFraud'] = y_preds
sub.to_csv('XGB_hypopt_model.csv', index=False )<import_modules>
Best_trial= {'lambda': 0.0042687338951820425,
'alpha': 6.2637008222060935,
'colsample_bytree': 0.4,
'subsample': 0.6,
'n_estimators': 4000,
'learning_rate': 0.01,
'max_depth': 11,
'random_state': 2020,
'min_child_weight': 171,
'tree_method':'gpu_hist'
}
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
preds = np.zeros(test.shape[0])
 kf = KFold(n_splits=5,random_state=48,shuffle=True)
 rmse=[]
models = []
n=0
for trn_idx, test_idx in kf.split(train[columns],train['target']):
X_tr,X_val=train[columns].iloc[trn_idx],train[columns].iloc[test_idx]
y_tr,y_val=train['target'].iloc[trn_idx],train['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[columns])/kf.n_splits
rmse.append(mean_squared_error(y_val, model.predict(X_val), squared=False))
models.append(model)
 print(n+1,rmse[n])
 n+=1
train_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_transaction.csv')
 train_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_identity.csv')
 test_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv')
 test_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_identity.csv' )<categorify>
sub['target']=preds
sub.to_csv('submission.csv', index=False )
train_transaction = reduce_mem_usage(train_transaction, verbose = True)
 train_identity = reduce_mem_usage(train_identity,verbose = True)
 test_transaction = reduce_mem_usage(test_transaction, verbose=True)
 test_identity = reduce_mem_usage(test_identity,verbose = True )<set_options>
import matplotlib.pyplot as plt