Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<define_variables>
warnings.filterwarnings('ignore' )
passenger_id = test.PassengerId<feature_engineering>
def get_logger() :
FORMAT = '[%(levelname)s]%(asctime)s:%(name)s:%(message)s'
logging.basicConfig(format=FORMAT)
 logger = logging.getLogger('main')
 logger.setLevel(logging.DEBUG)
 return logger
logger = get_logger()
train_df['Name_Len'] = train_df['Name'].apply(lambda x: len(x))
train_df['Survived'].groupby(pd.qcut(train_df['Name_Len'],5)).mean()<feature_engineering>
def read_data(nrows=None):
logger.info('Input data')
 train_df = pd.read_csv('.. /input/santander-customer-transaction-prediction/train.csv',nrows=nrows)
 test_df = pd.read_csv('.. /input/santander-customer-transaction-prediction/test.csv')
 return train_df, test_df
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
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
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
def ticket_grouped(train, test):
for i in [train, test]:
i['Ticket_Lett'] = i['Ticket'].apply(lambda x: str(x)[0])
 i['Ticket_Lett'] = i['Ticket_Lett'].apply(lambda x: str(x))
i['Ticket_Lett'] = np.where(( i['Ticket_Lett'] ).isin(['1', '2', '3', 'S', 'P', 'C', 'A']), i['Ticket_Lett'],
np.where(( i['Ticket_Lett'] ).isin(['W', '4', '7', '6', 'L', '5', '8']),
'Low_ticket', 'Other_ticket'))
i['Ticket_Len'] = i['Ticket'].apply(lambda x: len(x))
del i['Ticket']
return train, test
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
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
def embarked_impute(train, test):
for i in [train, test]:
i['Embarked'] = i['Embarked'].fillna('S')
 return train, test<correct_missing_values>
def process_data(train_df, test_df):
logger.info('Features engineering')
 synthetic = np.load('.. /input/publicprivate/synthetic_samples_indexes.npy')
 synthetic = synthetic-200000
synthetic = np.array(synthetic)
 test_df = test_df.iloc[~test_df.index.isin(synthetic)]
idx = [c for c in train_df.columns if c not in ['ID_code', 'target']]
traintest = pd.concat([train_df, test_df])
 traintest = traintest.reset_index(drop=True)
 for col in idx:
varname = col + '_IsUnique'
traintest[varname] = 0
_, index_, count_ = np.unique(traintest.loc[:,col].values, return_counts=True, return_index=True)
 traintest[varname][index_[count_ == 1]] += 1
traintest[varname] = traintest[varname] /(traintest[varname] == 1 ).sum()
for col in idx:
traintest[col+'_freq'] = traintest[col].map(traintest.groupby(col ).size())
 for col in idx:
varname = col + '_IsUnique'
tmp_col = traintest.loc[traintest[varname] > 0][col]
traintest[col + '_OnlyUnique'] = tmp_col
traintest[col + '_OnlyUnique'] = traintest[col + '_OnlyUnique'].fillna(0)
 traintest[col + '_NotUnique'] = traintest[col] - traintest[col + '_OnlyUnique']
traintest[col + '_NotUnique'] = traintest[col + '_NotUnique'].replace(0,np.nan)
 traintest[col + '_OnlyUnique'] = traintest[col + '_OnlyUnique'].replace(0,np.nan)
 traintest.pop(varname)
 train_df = traintest[:200000]
test_df = traintest[200000:]
print('Train and test shape:',train_df.shape, test_df.shape)
 return train_df, test_df
test['Fare'].fillna(train['Fare'].mean() , inplace = True )<categorify>
def run_model(train_df, test_df):
logger.info('Prepare the model')
 features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']
logger.info('Run model')
 param = {
'bagging_freq': 5,
'bagging_fraction': 0.38,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.045,
'learning_rate': 0.0095,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 20,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 3,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}
num_round = 1000000
folds = StratifiedKFold(n_splits=5, shuffle=False, random_state=42)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold {}".format(fold_))
trn_data = lgb.Dataset(train_df.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 3500)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)))
 return predictions
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>
def submit(test_df, predictions):
logger.info('Prepare submission')
 all_test_df = pd.read_csv('.. /input/santander-customer-transaction-prediction/test.csv')
 sub = pd.DataFrame({"ID_code": all_test_df.ID_code.values})
 sub["target"] = 0
sub_real = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub_real["target"] = predictions
sub = sub.set_index('ID_code')
 sub_real = sub_real.set_index('ID_code')
 sub.update(sub_real)
 sub = sub.reset_index()
sub.to_csv("submission.csv", index=False )
<categorify><EOS> 
def main(nrows=None):
train_df, test_df = read_data(nrows)
 train_df, test_df = process_data(train_df, test_df)
 predictions = run_model(train_df, test_df)
 submit(test_df, predictions )