Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<load_from_csv>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
%%time
folder_path = '.. /input/'
train_identity = pd.read_csv(f'{folder_path}train_identity.csv')
 train_transaction = pd.read_csv(f'{folder_path}train_transaction.csv')
 test_identity = pd.read_csv(f'{folder_path}test_identity.csv')
 test_transaction = pd.read_csv(f'{folder_path}test_transaction.csv')
 sub = pd.read_csv(f'{folder_path}sample_submission.csv')
 fix = {o:n for o, n in zip(test_identity.columns, train_identity.columns)}
test_identity.rename(columns=fix, inplace=True)
 train = pd.merge(train_transaction, train_identity, on='TransactionID', how='left')
 test = pd.merge(test_transaction, test_identity, on='TransactionID', how='left')
 del train_identity, train_transaction, test_identity, test_transaction, fix
train = reduce_mem_usage(train)
 test = reduce_mem_usage(test )<define_variables>
train = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv', index_col='id')
 test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv', index_col='id' )
v = [1, 3, 4, 6, 8, 11]
v += [13, 14, 17, 20, 23, 26, 27, 30]
v += [36, 37, 40, 41, 44, 47, 48]
v += [54, 56, 59, 62, 65, 67, 68, 70]
v += [76, 78, 80, 82, 86, 88, 89, 91]
v += [107, 108, 111, 115, 117, 120, 121, 123]
v += [124, 127, 129, 130, 136]
v += [138, 139, 142, 147, 156, 162]
v += [165, 160, 166]
v += [178, 176, 173, 182]
v += [187, 203, 205, 207, 215]
v += [169, 171, 175, 180, 185, 188, 198, 210, 209]
v += [218, 223, 224, 226, 228, 229, 235]
v += [240, 258, 257, 253, 252, 260, 261]
v += [264, 266, 267, 274, 277]
v += [220, 221, 234, 238, 250, 271]
v += [294, 284, 285, 286, 291, 297]
v += [303, 305, 307, 309, 310, 320]
v += [281, 283, 289, 296, 301, 314]
use_Vcols = ['V'+str(x)for x in v]
Vcols = ['V'+str(x)for x in range(1,340)]
drop_Vcols = list(set(Vcols)- set(use_Vcols))
train.drop(drop_Vcols, axis=1, inplace=True)
 test.drop(drop_Vcols, axis=1, inplace=True )<count_missing_values>
plt.style.use('ggplot')
 plt.rcParams['axes.titlesize'] = 16
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['xtick.labelsize'] = 'large'
print(f'There are {train.isnull().any().sum() } columns in train dataset with missing values.' )<count_unique_values>
print('Missing values on the train data:', train.isnull().sum().sum())
 print('Missing values on the test data:', test.isnull().sum().sum() )
one_value_cols = [col for col in train.columns if train[col].nunique() <= 1]
one_value_cols_test = [col for col in test.columns if test[col].nunique() <= 1]
one_value_cols == one_value_cols_test<count_unique_values>
print('Duplicated rows on the train data:', train.duplicated().sum())
 print('Duplicated rows on the test data:', test.duplicated().sum() )
print(f'There are {len(one_value_cols)} columns in train dataset with one unique value.')
 print(f'There are {len(one_value_cols_test)} columns in test dataset with one unique value.' )<count_values>
q1 = train.quantile(0.25)
 q3 = train.quantile(0.75)
 iqr = q3 - q1
mask =(train >=(q1 - 1.5*iqr)) &(train <= q3 + 1.5*iqr)
 train = train[mask.apply(all, axis=1)]
print('Train set without outliers shape:', train.shape )
s1 = train['id_03'].value_counts(dropna=False, normalize=True ).head()
s2 = train['id_04'].value_counts(dropna=False, normalize=True ).head()
s3 = train['id_05'].value_counts(dropna=False, normalize=True ).head()
s4 = train['id_06'].value_counts(dropna=False, normalize=True ).head()
s5 = train['id_09'].value_counts(dropna=False, normalize=True ).head()
s6 = train['id_10'].value_counts(dropna=False, normalize=True ).head()
print(pd.concat([s1, s2, s3, s4, s5, s6], axis = 1))<count_values>
X_train, X_val, y_train, y_val = train_test_split(train[predictors],
train[target],
test_size = 0.2,
random_state=2021 )
train['id_11'].value_counts(dropna=False, normalize=True ).head()<feature_engineering>
model = XGBRegressor(objective='reg:squarederror',
booster = "gbtree",
eval_metric = "rmse",
tree_method = "gpu_hist",
n_estimators = 1000,
learning_rate = 0.04,
eta = 0.1,
max_depth = 7,
subsample=0.85,
colsample_bytree = 0.85,
colsample_bylevel = 0.8,
alpha = 0,
random_state = 2021 )
for i in range(1,16):
if i in [1,2,3,5,9]: continue
train['D'+str(i)] = train['D'+str(i)] - train.TransactionDT/np.float32(24*60*60)
 test['D'+str(i)] = test['D'+str(i)] - test.TransactionDT/np.float32(24*60*60 )<define_variables>
%time model.fit(X_train, y_train )
emails = {'gmail': 'google', 'att.net': 'att', 'twc.com': 'spectrum',
'scranton.edu': 'other', 'optonline.net': 'other', 'hotmail.co.uk': 'microsoft',
'comcast.net': 'other', 'yahoo.com.mx': 'yahoo', 'yahoo.fr': 'yahoo',
'yahoo.es': 'yahoo', 'charter.net': 'spectrum', 'live.com': 'microsoft',
'aim.com': 'aol', 'hotmail.de': 'microsoft', 'centurylink.net': 'centurylink',
'gmail.com': 'google', 'me.com': 'apple', 'earthlink.net': 'other', 'gmx.de': 'other',
'web.de': 'other', 'cfl.rr.com': 'other', 'hotmail.com': 'microsoft',
'protonmail.com': 'other', 'hotmail.fr': 'microsoft', 'windstream.net': 'other',
'outlook.es': 'microsoft', 'yahoo.co.jp': 'yahoo', 'yahoo.de': 'yahoo',
'servicios-ta.com': 'other', 'netzero.net': 'other', 'suddenlink.net': 'other',
'roadrunner.com': 'other', 'sc.rr.com': 'other', 'live.fr': 'microsoft',
'verizon.net': 'yahoo', 'msn.com': 'microsoft', 'q.com': 'centurylink',
'prodigy.net.mx': 'att', 'frontier.com': 'yahoo', 'anonymous.com': 'other',
'rocketmail.com': 'yahoo', 'sbcglobal.net': 'att', 'frontiernet.net': 'yahoo',
'ymail.com': 'yahoo', 'outlook.com': 'microsoft', 'mail.com': 'other',
'bellsouth.net': 'other', 'embarqmail.com': 'centurylink', 'cableone.net': 'other',
'hotmail.es': 'microsoft', 'mac.com': 'apple', 'yahoo.co.uk': 'yahoo', 'netzero.com': 'other',
'yahoo.com': 'yahoo', 'live.com.mx': 'microsoft', 'ptd.net': 'other', 'cox.net': 'other',
'aol.com': 'aol', 'juno.com': 'other', 'icloud.com': 'apple'}
us_emails = ['gmail', 'net', 'edu']
for c in ['P_emaildomain', 'R_emaildomain']:
train[c + '_bin'] = train[c].map(emails)
 test[c + '_bin'] = test[c].map(emails)
 train[c + '_suffix'] = train[c].map(lambda x: str(x ).split('.')[-1])
 test[c + '_suffix'] = test[c].map(lambda x: str(x ).split('.')[-1])
 train[c + '_suffix'] = train[c + '_suffix'].map(lambda x: x if str(x)not in us_emails else 'us')
 test[c + '_suffix'] = test[c + '_suffix'].map(lambda x: x if str(x)not in us_emails else 'us' )<categorify>
y_val_pred = model.predict(X_val)
 print('Validation Set RMSE:', np.sqrt(mean_squared_error(y_val, y_val_pred)) )
<feature_engineering><EOS> 
test['target'] = model.predict(test[predictors])
 test['target'].to_csv('submission.csv' )