Kaggle competition: Santander Customer Satisfaction
<SOS> metric: AUC Kaggle data source: santander-customer-satisfaction<install_modules>
%matplotlib inline
rcParams['figure.figsize'] = 12, 4
print(check_output(["ls", ".. /input"] ).decode("utf8"))
!/opt/conda/bin/python3.7 -m pip install --upgrade pip
!pip uninstall -y typing
!pip install carefree-learn<define_variables>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
file_folder = "/kaggle/input/titanic"
working_folder = "/kaggle/working"<load_from_csv>
remove = []
c = df_train.columns
for i in range(len(c)-1):
v = df_train[c[i]].values
for j in range(i+1,len(c)) :
if np.array_equal(v,df_train[c[j]].values):
remove.append(c[j])
 df_train.drop(remove, axis=1, inplace=True)
 df_test.drop(remove, axis=1, inplace=True )
def scoring(raw_metrics, mean, std):
return mean + std
def test() :
train_file = f"{file_folder}/train.csv"
test_file = f"{file_folder}/test.csv"
data_config = {"label_name": "Survived"}
hpo = cflearn.tune_with(
 train_file,
model="tree_dnn",
temp_folder=f"{working_folder}/__hpo__",
task_type=TaskTypes.CLASSIFICATION,
estimator_scoring_function=scoring,
data_config=data_config,
num_parallel=5 
)
 results = cflearn.repeat_with(
 train_file,
**hpo.best_param,
models="tree_dnn",
temp_folder=f"{working_folder}/__repeat__",
num_repeat=10, num_jobs=5,
data_config=data_config 
)
 ensemble = cflearn.EnsemblePattern(results.patterns["tree_dnn"])
 predictions = ensemble.predict(test_file ).ravel()
x_te, _ = results.transformer.data.read_file(test_file, contains_labels=False)
 id_list = DataTuple.with_transpose(x_te, None ).xT[0]
with open(f"{working_folder}/submissions.csv", "w")as f:
f.write("PassengerId,Survived
")
 for test_id, prediction in zip(id_list, predictions):
f.write(f"{test_id},{prediction}
")
 return results
if __name__ == '__main__':
results = test()<find_best_params>
y_train = df_train['TARGET'].values
X_train = df_train.drop(['ID','TARGET'], axis=1 ).values
id_test = df_test['ID']
X_test = df_test.drop(['ID'], axis=1 ).values
len_train = len(X_train)
 len_test = len(X_test)
experiments = results.experiments
ms = {k: list(map(cflearn.load_task, v)) for k, v in experiments.tasks.items() }
print(ms )<prepare_x_and_y>
clf = xgb.XGBClassifier(
 missing=np.nan,
max_depth=5,
n_estimators=350,
learning_rate=0.02,
nthread=4,
subsample=0.95,
colsample_bytree=0.85,
seed=4242)
 X_fit, X_eval, y_fit, y_eval= train_test_split(X_train, y_train, test_size=0.3)
model = ms["tree_dnn"][0].model
data = model.tr_data
print("=== Raw ===")
 print(f"Feature dimension : {len(data.raw.x[0])}")
 print()
for line in data.raw.x[:3]:
print(line)
 print(data.raw.y[:3])
 print()
print("=== Converted ===")
 print(f"Feature dimension : {data.converted.x.shape[1]}")
 print(data.converted.x[:3])
 print(data.converted.y[:3])
 print()
print("=== Processed ===")
 print(f"Feature dimension : {data.processed.x.shape[1]}")
 print(data.processed.x[:3])
 print(data.processed.y[:3] )<categorify>
clf.fit(X_train, y_train, early_stopping_rounds=20, eval_metric="auc", eval_set=[(X_eval, y_eval)])
print(model.encoders )<set_options>
y_pred= clf.predict_proba(X_test)[:,1]
submission = pd.DataFrame({"ID":id_test, "TARGET":y_pred})
 submission.to_csv("submission.csv", index=False)
 print('Completed!' )
%matplotlib inline 
<load_from_csv>
def modelfit(alg, dtrain, predictors,useTrainCV=True, cv_folds=5, early_stopping_rounds=50):
if useTrainCV:
xgb_param = alg.get_xgb_params()
xgtrain = xgb.DMatrix(dtrain,label = predictors)
 cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params() ['n_estimators'], nfold=cv_folds,
metrics='auc', early_stopping_rounds=early_stopping_rounds, show_progress=False)
 alg.set_params(n_estimators=cvresult.shape[0])
 alg.fit(predictors, dtrain['Disbursed'],eval_metric='auc')
 dtrain_predictions = alg.predict(predictors)
 dtrain_predprob = alg.predict_proba(predictors)[:,1]
print("
Model Report")
 print("Accuracy : %.4g" % metrics.accuracy_score(dtrain['Disbursed'].values, dtrain_predictions))
print("AUC Score(Train): %f" % metrics.roc_auc_score(dtrain['Disbursed'], dtrain_predprob))
feat_imp = pd.Series(alg.booster().get_fscore() ).sort_values(ascending=False)
 feat_imp.plot(kind='bar', title='Feature Importances')
 plt.ylabel('Feature Importance Score')
 print("Done" )
<prepare_x_and_y><EOS> 
xgb1 = xgb.XGBClassifier(
 learning_rate =0.1,
n_estimators=1000,
max_depth=5,
min_child_weight=1,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=27)
 modelfit(xgb1, X_train, y_train )