Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<import_modules>
train, test = read_data_()
train, test = get_title_feature_(train, test)
 train, test = get_surname_(train, test)
 train, test = get_first_names_(train, test)
 train, test = get_married_feature_(train, test)
 train, test = get_family_counts_(train, test)
 train, test = extract_ticket_number_(train, test)
 train, test = get_ticket_length_(train, test)
 train, test = get_ticket_counts_(train, test)
 train, test = drop_unused_(train, test)
 train, test = clean_data_(train, test)
 train, test = format_categories_(train, test )
import lightgbm as lgbm<train_model>
X, X_submit, y = get_X_y_(train, test)
 X, X_submit = encode_categories_(X, X_submit, y)
 X, X_submit = impute_missing_(X, X_submit)
 X, X_submit = get_decomposition_features_(X, X_submit )
def lightGBM(train,target,test, n_folds):
params = {
'boosting_type':'gbdt',
'boost': 'gbdt',
'objective':'binary',
'learning_rate':0.008,
'metric':'auc',
'max_depth':2,
'num_leaves':13,
"bagging_fraction" : 0.4,
"feature_fraction" : 1.0,
"min_child_samples":80,
"bagging_freq" : 5,
"bagging_seed" : 2020,
"verbosity" : -1,
"random_state" : 2020,
'early_stopping_round':5500,
'min_data_in_leaf':80,
'boost_from_average':False,
'seed':2020,
'tree_learner': 'serial'
}
skfold = StratifiedKFold(n_splits=n_folds, random_state=42)
 train_pred = np.zeros(len(train))
predictions = np.zeros(len(test))
for fold_,(xidx, validx)in enumerate(skfold.split(train_data,target)) :
print("Fold idx:{}".format(fold_ + 1))
lgbmTrain = lgbm.Dataset(train.iloc[xidx],label=target.iloc[xidx])
 lgbmVal = lgbm.Dataset(train.iloc[validx],label=target.iloc[validx])
 evals_result = {}
model = lgbm.train(params,lgbmTrain,120000,valid_sets=[lgbmTrain,lgbmVal], evals_result=evals_result)
 train_pred[validx] = model.predict(train.iloc[validx], num_iteration=model.best_iteration)
 predictions += model.predict(test, num_iteration=model.best_iteration)/skfold.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, train_pred)))
 return predictions, model, evals_result<compute_test_metric>
environ["HYPEROPT_FMIN_SEED"] = "0"
estimator = RandomForestClassifier(n_jobs=-1, random_state=0, class_weight="balanced")
 def fn(params):
for p in ["n_estimators", "max_depth", "min_samples_split"]:
params[p] = int(params[p])
 params.update({"min_samples_leaf": params["min_samples_split"] - 1})
 estimator.set_params(**params)
 scores = cross_validate(estimator, X, y, scoring="roc_auc", cv=10, n_jobs=-1)
 return 1 - scores["test_score"].min()
space = {
"n_estimators": qloguniform("n_estimators", log(8), log(192), 2),
"criterion": choice("criterion", ["gini", "entropy"]),
"max_depth": quniform("max_depth", 2, 10, 1),
"min_samples_split": quniform("min_samples_split", 2, 9, 1),
"max_features": uniform("max_features", 0.1, 0.8),
"max_samples": uniform("max_samples", 0.1, 0.7),
}
best = fmin(fn=fn, space=space, algo=suggest, max_evals=200 )
prediction, model, eval_result = lightGBM(train_data,Target, test_data,5 )<find_best_params>
for p in ["n_estimators", "max_depth", "min_samples_split"]:
best[p] = int(best[p])
 best.update({"min_samples_leaf": best["min_samples_split"] - 1})
 best["criterion"] = ["gini", "entropy"][best["criterion"]]
print(best )
model.best_score<save_to_csv>
n_estimators = 5
estimator.set_params(**best)
 y_submit = zeros(( X_submit.shape[0],))
for r in range(n_estimators):
estimator.set_params(random_state=r + 1)
 estimator.fit(X, y)
 y_submit += estimator.predict(X_submit)
 y_submit =(y_submit / n_estimators)> 0.5
<load_from_csv><EOS> 
DataFrame(data={"PassengerId": test.index, "Survived": y_submit.astype(int)} ).to_csv(
 "submission.csv", index=False
)