Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<feature_engineering>
warnings.filterwarnings("ignore" )
train['TransactionAmt_log'] = np.log(train['TransactionAmt'])
 test['TransactionAmt_log'] = np.log(test['TransactionAmt'] )<categorify>
train_data = pd.read_csv(path+'train.csv')
 test_data = pd.read_csv(path+'test.csv')
 samp_subm = pd.read_csv(path+'sample_submission.csv' )
train['TransactionAmt_to_mean_card1'] = train['TransactionAmt'] / train.groupby(['card1'])['TransactionAmt'].transform('mean')
 train['TransactionAmt_to_mean_card4'] = train['TransactionAmt'] / train.groupby(['card4'])['TransactionAmt'].transform('mean')
 train['TransactionAmt_to_std_card1'] = train['TransactionAmt'] / train.groupby(['card1'])['TransactionAmt'].transform('std')
 train['TransactionAmt_to_std_card4'] = train['TransactionAmt'] / train.groupby(['card4'])['TransactionAmt'].transform('std')
 test['TransactionAmt_to_mean_card1'] = test['TransactionAmt'] / test.groupby(['card1'])['TransactionAmt'].transform('mean')
 test['TransactionAmt_to_mean_card4'] = test['TransactionAmt'] / test.groupby(['card4'])['TransactionAmt'].transform('mean')
 test['TransactionAmt_to_std_card1'] = test['TransactionAmt'] / test.groupby(['card1'])['TransactionAmt'].transform('std')
 test['TransactionAmt_to_std_card4'] = test['TransactionAmt'] / test.groupby(['card4'])['TransactionAmt'].transform('std')
 train['D15_to_mean_card1'] = train['D15'] / train.groupby(['card1'])['D15'].transform('mean')
 train['D15_to_mean_card4'] = train['D15'] / train.groupby(['card4'])['D15'].transform('mean')
 train['D15_to_std_card1'] = train['D15'] / train.groupby(['card1'])['D15'].transform('std')
 train['D15_to_std_card4'] = train['D15'] / train.groupby(['card4'])['D15'].transform('std')
 test['D15_to_mean_card1'] = test['D15'] / test.groupby(['card1'])['D15'].transform('mean')
 test['D15_to_mean_card4'] = test['D15'] / test.groupby(['card4'])['D15'].transform('mean')
 test['D15_to_std_card1'] = test['D15'] / test.groupby(['card1'])['D15'].transform('std')
 test['D15_to_std_card4'] = test['D15'] / test.groupby(['card4'])['D15'].transform('std')
 train['D15_to_mean_addr1'] = train['D15'] / train.groupby(['addr1'])['D15'].transform('mean')
 train['D15_to_mean_addr2'] = train['D15'] / train.groupby(['addr2'])['D15'].transform('mean')
 train['D15_to_std_addr1'] = train['D15'] / train.groupby(['addr1'])['D15'].transform('std')
 train['D15_to_std_addr2'] = train['D15'] / train.groupby(['addr2'])['D15'].transform('std')
 test['D15_to_mean_addr1'] = test['D15'] / test.groupby(['addr1'])['D15'].transform('mean')
 test['D15_to_mean_addr2'] = test['D15'] / test.groupby(['addr2'])['D15'].transform('mean')
 test['D15_to_std_addr1'] = test['D15'] / test.groupby(['addr1'])['D15'].transform('std')
 test['D15_to_std_addr2'] = test['D15'] / test.groupby(['addr2'])['D15'].transform('std' )<string_transform>
print('Number train samples:', len(train_data.index))
print('Number test samples:', len(test_data.index))
print('Number features:', len(train_data.columns))
train[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = train['P_emaildomain'].str.split('.', expand=True)
 train[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = train['R_emaildomain'].str.split('.', expand=True)
 test[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = test['P_emaildomain'].str.split('.', expand=True)
 test[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = test['R_emaildomain'].str.split('.', expand=True )<categorify>
print('Missing values on the train data:', train_data.isnull().sum().sum())
 print('Missing values on the test data:', test_data.isnull().sum().sum() )
%time
train['cents'] =(train['TransactionAmt'] - np.floor(train['TransactionAmt'])).astype('float32')
 test['cents'] =(test['TransactionAmt'] - np.floor(test['TransactionAmt'])).astype('float32')
 print('cents, ', end='')
 encode_FE(train,test,['addr1','card1','card2','card3','P_emaildomain'])
 encode_CB('card1','addr1')
 encode_CB('card1_addr1','P_emaildomain')
 encode_FE(train,test,['card1_addr1','card1_addr1_P_emaildomain'])
 encode_AG(['TransactionAmt','D9','D11'],['card1','card1_addr1','card1_addr1_P_emaildomain'],['mean','std'],usena=True )<define_variables>
pca = PCA().fit(train_data[train_data.columns[1:-1]])
 plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('No of components')
 plt.ylabel('Cumulative explained variance')
 plt.grid()
plt.show()
many_null_cols = [col for col in train.columns if train[col].isnull().sum() / train.shape[0] > 0.9]
many_null_cols_test = [col for col in test.columns if test[col].isnull().sum() / test.shape[0] > 0.9]<define_variables>
features = ['cont'+str(i)for i in range(1, 15)]
no_features = ['id', 'target']
big_top_value_cols = [col for col in train.columns if train[col].value_counts(dropna=False, normalize=True ).values[0] > 0.9]
big_top_value_cols_test = [col for col in test.columns if test[col].value_counts(dropna=False, normalize=True ).values[0] > 0.9]<drop_column>
train_data['mean'] = train_data[features].mean(axis=1)
 train_data['std'] = train_data[features].std(axis=1)
 train_data['max'] = train_data[features].max(axis=1)
 train_data['min'] = train_data[features].min(axis=1)
 train_data['sum'] = train_data[features].sum(axis=1)
 test_data['mean'] = test_data[features].mean(axis=1)
 test_data['std'] = test_data[features].std(axis=1)
 test_data['max'] = test_data[features].max(axis=1)
 test_data['min'] = test_data[features].min(axis=1)
 test_data['sum'] = test_data[features].sum(axis=1 )
cols_to_drop = list(set(many_null_cols + many_null_cols_test + big_top_value_cols + big_top_value_cols_test + one_value_cols+ one_value_cols_test))
cols_to_drop.remove('isFraud')
 len(cols_to_drop )<drop_column>
X = train_data[train_data.columns.difference(no_features)]
y = train_data['target']
X_test = test_data[test_data.columns.difference(no_features)]
train = train.drop(cols_to_drop, axis=1)
 test = test.drop(cols_to_drop, axis=1 )<categorify>
iso = IsolationForest(contamination=0.01)
 yhat = iso.fit_predict(train_data[train_data.columns.difference(no_features)])
 mask = yhat != -1
def clean_inf_nan(df):
return df.replace([np.inf, -np.inf], np.nan)
 train = clean_inf_nan(train)
 test = clean_inf_nan(test )<define_variables>
print('Number of outliers:', len(train_data)-mask.sum() )
identity_cat_cols = ['id_12', 'id_13', 'id_14', 'id_15', 'id_16', 'id_17', 'id_18',
'id_19', 'id_20', 'id_21', 'id_22', 'id_23', 'id_24', 'id_25',
'id_26', 'id_27', 'id_28', 'id_29', 'id_30', 'id_31', 'id_32',
'id_33', 'id_34', 'id_35', 'id_36', 'id_37', 'id_38', 'DeviceType', 'DeviceInfo']
transaction_cat_cols = ['ProductCD', 'card1', 'card2', 'card3', 'card4', 'card5', 'card6', 'addr1', 'addr2',
'P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3',
'R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3','P_emaildomain', 'R_emaildomain',
'P_emaildomain_bin', 'R_emaildomain_bin', 'P_emaildomain_suffix', 'R_emaildomain_suffix',
'M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9']
cat_cols = identity_cat_cols + transaction_cat_cols
cat_cols = list(set(cat_cols)& set(train.columns))
encoded_cols = ['addr1','card1','card2','card3']
cat_cols = list(set(cat_cols)- set(encoded_cols))
common_cat_cols = [col for col in test.columns if col in cat_cols]
cat_cols == common_cat_cols<drop_column>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, random_state=2021 )
cat_train = train[common_cat_cols]
num_train = train.drop(cat_cols, axis=1)
 cat_test = test[common_cat_cols]
num_test = test.drop(common_cat_cols, axis=1)
 cat_train.columns == cat_test.columns<count_unique_values>
model = XGBRegressor(objective='reg:squarederror',
booster = "gbtree",
eval_metric = "rmse",
tree_method = "gpu_hist",
n_estimators = 600,
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)
 model.fit(X_train, y_train)
 y_val_pred = model.predict(X_val)
 print('Score validation data:', np.sqrt(mean_squared_error(y_val, y_val_pred)) )
cat_nunique = list(map(lambda col: cat_train[col].nunique() , common_cat_cols))
d = dict(zip(common_cat_cols, cat_nunique))
sorted(d.items() , key=lambda x:x[1] )<data_type_conversions>
y_test = model.predict(X_test )
cat_train = cat_train.astype('category')
 cat_test = cat_test.astype('category')
 print(cat_train.dtypes )<categorify>
output = samp_subm.copy()
output['target'] = y_test
<categorify><EOS> 
output.to_csv('submission.csv', index=False )