Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<count_values>
warnings.filterwarnings('ignore' )
pd.qcut(train['Cabin_num'],3 ).value_counts()<groupby>
%%time
train_df = pd.read_csv(PATH+"train.csv")
 test_df = pd.read_csv(PATH+"test.csv" )
train['Survived'].groupby(pd.qcut(train['Cabin_num'], 3)).mean()<count_values>
def missing_data(data):
total = data.isnull().sum()
percent =(data.isnull().sum() /data.isnull().count() *100)
 tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 types = []
for col in data.columns:
dtype = str(data[col].dtype)
 types.append(dtype)
 tt['Types'] = types
return(np.transpose(tt))
train['Embarked'].value_counts()<count_values>
%%time
missing_data(train_df )
train['Embarked'].value_counts(normalize=True )<groupby>
%%time
missing_data(test_df )
train['Survived'].groupby(train['Embarked'] ).mean()<feature_engineering>
print("There are {}% target values with 1".format(100 * train_df["target"].value_counts() [1]/train_df.shape[0]))
def names(train, test):
for i in [train, test]:
i['Name_Len'] = i['Name'].apply(lambda x: len(x))
i['Name_Title'] = i['Name'].apply(lambda x: x.split(',')[1] ).apply(lambda x: x.split() [0])
 del i['Name']
return train, test<feature_engineering>
%%time
correlations = train_df[features].corr().abs().unstack().sort_values(kind="quicksort" ).reset_index()
correlations = correlations[correlations['level_0'] != correlations['level_1']]
correlations.head(10 )
def age_impute(train, test):
for i in [train, test]:
i['Age_Null_Flag'] = i['Age'].apply(lambda x: 1 if pd.isnull(x)else 0)
 data = train.groupby(['Name_Title', 'Pclass'])['Age']
i['Age'] = data.transform(lambda x: x.fillna(x.mean()))
return train, test<drop_column>
%%time
features = train_df.columns.values[2:202]
unique_max_train = []
unique_max_test = []
for feature in features:
values = train_df[feature].value_counts()
unique_max_train.append([feature, values.max() , values.idxmax() ])
 values = test_df[feature].value_counts()
unique_max_test.append([feature, values.max() , values.idxmax() ] )
def fam_size(train, test):
for i in [train, test]:
i['Fam_Size'] = np.where(( i['SibSp']+i['Parch'])== 0 , 'Solo',
np.where(( i['SibSp']+i['Parch'])<= 3,'Nuclear', 'Big'))
del i['SibSp']
del i['Parch']
return train, test<drop_column>
%%time
idx = features = train_df.columns.values[2:202]
for df in [test_df, train_df]:
df['sum'] = df[idx].sum(axis=1)
 df['min'] = df[idx].min(axis=1)
 df['max'] = df[idx].max(axis=1)
 df['mean'] = df[idx].mean(axis=1)
 df['std'] = df[idx].std(axis=1)
 df['skew'] = df[idx].skew(axis=1)
 df['kurt'] = df[idx].kurtosis(axis=1)
 df['med'] = df[idx].median(axis=1 )
def cabin(train, test):
for i in [train, test]:
i['Cabin_Letter'] = i['Cabin'].apply(lambda x: str(x)[0])
 del i['Cabin']
return train, test<categorify>
print('Train and test columns: {} {}'.format(len(train_df.columns), len(test_df.columns)) )
def cabin_num(train, test):
for i in [train, test]:
i['Cabin_num1'] = i['Cabin'].apply(lambda x: str(x ).split(' ')[-1][1:])
 i['Cabin_num1'].replace('an', np.NaN, inplace = True)
 i['Cabin_num1'] = i['Cabin_num1'].apply(lambda x: int(x)if not pd.isnull(x)and x != '' else np.NaN)
 i['Cabin_num'] = pd.qcut(train['Cabin_num1'],3)
 train = pd.concat(( train, pd.get_dummies(train['Cabin_num'], prefix = 'Cabin_num')) , axis = 1)
 test = pd.concat(( test, pd.get_dummies(test['Cabin_num'], prefix = 'Cabin_num')) , axis = 1)
 del train['Cabin_num']
del test['Cabin_num']
del train['Cabin_num1']
del test['Cabin_num1']
return train, test<categorify>
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']
def embarked_impute(train, test):
for i in [train, test]:
i['Embarked'] = i['Embarked'].fillna('S')
 return train, test<correct_missing_values>
param = {
'tree_method': 'gpu_hist',
'objective': 'binary:logitraw',
'eta':0.01,
'gamma':0.01,
'max_depth':10,
'min_child_weight':20,
'subsample':0.05,
'max_leaves':20,
'eval_metric':'auc',
'verbosity':1
}
test['Fare'].fillna(train['Fare'].mean() , inplace = True )<categorify>
folds = StratifiedKFold(n_splits=10, shuffle=False, random_state=44000)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold {}".format(fold_))
trn_data = xgb.DMatrix(train_df.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = xgb.DMatrix(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 num_round = 1000000
clf = xgb.train(param, trn_data, num_round, evals = [(trn_data, 'train'),(val_data, 'valid')], early_stopping_rounds=3000, verbose_eval=1000)
 train_data =xgb.DMatrix(train_df.iloc[val_idx][features])
 oof[val_idx] = clf.predict(train_data, ntree_limit=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([fold_importance_df], axis=0)
 test_data = xgb.DMatrix(test_df[features])
 predictions += clf.predict(test_data, ntree_limit=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )
def dummies(train, test, columns = ['Pclass', 'Sex', 'Embarked', 'Ticket_Lett', 'Cabin_Letter', 'Name_Title', 'Fam_Size']):
for column in columns:
train[column] = train[column].apply(lambda x: str(x))
test[column] = test[column].apply(lambda x: str(x))
good_cols = [column+'_'+i for i in train[column].unique() if i in test[column].unique() ]
train = pd.concat(( train, pd.get_dummies(train[column], prefix = column)[good_cols]), axis = 1)
 test = pd.concat(( test, pd.get_dummies(test[column], prefix = column)[good_cols]), axis = 1)
 del train[column]
del test[column]
return train, test<drop_column>
sub_df = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_df["target"] = predictions
sub_df.to_csv("submission.csv", index=False )