Kaggle competition: Santander Customer Transaction Prediction
df = percent_null_value(train)
 df.head(len(df))<feature_engineering>
train_df = pd.read_csv(".. /input/train.csv" )
df = percent_null_value(test)
 df.head(len(df))<filter>
params = {
'boosting':'gbdt',
'bagging_freq':5,
'bagging_fraction':0.5,
'num_leaves':2,
'reg_lambda':100.0,
'learning_rate':0.01,
'max_bin':1023,
'seed':3366
}
train[train.Embarked.isnull() ]<count_values>
def optimal_rounds(X, verbose=False):

 rounds = []
for i in range(200):
if verbose:
print("Feature ", i)
 cv_res = lgb.cv(params,
lgb.Dataset(X[['var_'+str(i)]], X['target']),
nfold=3,
num_boost_round=100000,
metrics='binary_logloss',
verbose_eval=100 if verbose else None,
early_stopping_rounds=100 
)
 rounds.append(len(cv_res['binary_logloss-mean']))
return rounds
train[(train.Pclass == 1)&(train.Sex == 'female')]['Embarked'].value_counts()<data_type_conversions>
opt_rounds = optimal_rounds(train_df, verbose=True )
train.Embarked.fillna('S', inplace = True )<filter>
print("Optimal number of rounds: ", opt_rounds)
 print("Optimal number of rounds for var_108: ", opt_rounds[108])
 print("Optimal number of rounds for var_30: ", opt_rounds[30] )
test[test.Fare.isnull() ]<data_type_conversions>
num_ones = np.sum(train_df['target'] == 1)
 num_zeros = np.sum(train_df['target'] == 0)
 class LGBNaiveBayes:
def fit(self,X_train, y_train, opt_rounds):
self.clfs = []
for i in range(200):
if i%20 == 0:
print("Fitting var_"+ str(i)+"...")
 params['n_estimators'] = opt_rounds[i]
lgb_clf = lgb.LGBMClassifier(**params)
 lgb_clf.fit(X_train[['var_'+str(i)]], y_train)
 self.clfs.append(lgb_clf)
 def predict_proba(self,X):
log_sum = np.zeros(( X.shape[0],2))
for i in range(200):
log_sum += np.log(self.clfs[i].predict_proba(X[['var_'+str(i)]]))
log_sum += np.array([np.log(num_ones)- np.log(200000), np.log(num_zeros)- np.log(200000)])
 log_sum -= np.array([np.log(num_ones)- np.log(200000), np.log(num_zeros)- np.log(200000)])
 log_prob_x = logsumexp(log_sum, axis=1)
 return np.exp(log_sum - np.atleast_2d(log_prob_x ).T)
missing_value = test[(test.Pclass == 3)&(test.Sex == 'male')]['Fare'].mean()
test.Fare.fillna(missing_value, inplace = True )<feature_engineering>
clf = LGBNaiveBayes()
data = [train, test]
for dataset in data:
dataset.Cabin.fillna('N', inplace = True)
 dataset['Cabin'] = dataset['Cabin'].apply(lambda x : x[0] )<concatenate>
features = train_df.columns[2:].values
def cross_validate(nfolds):
sss = StratifiedShuffleSplit(nfolds)
 aucs = []
for train, test in sss.split(train_df[features], train_df['target']):
clf.fit(train_df.loc[train][features], train_df.loc[train]['target'], opt_rounds)
 y_true = train_df.loc[test]['target']
y_pred = clf.predict_proba(train_df.loc[test][features])[:,1]
test_auc = roc_auc_score(y_true, y_pred)
 aucs.append(test_auc)
 print("Test AUC:", test_auc)
 print("Mean test AUC: ", np.mean(aucs))
cross_validate(5 )
survived = train.Survived
all_data = pd.concat([train, test] )<categorify>
test_df = pd.read_csv('.. /input/test.csv')
 clf.fit(train_df[features], train_df['target'], opt_rounds)
 pred = clf.predict_proba(test_df.iloc[:][features])
 sub_df = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_df["target"] = pred[:,1]
sub_df.to_csv("submission.csv", index=False )