Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
mpl.rcParams.update({'font.size': 13} )
filepath = 'submission.csv'<save_to_csv>
folder = '/kaggle/input/titanic/'
train = pd.read_csv(folder + 'train.csv')
 train_shape = train.shape
test = pd.read_csv(folder + 'test.csv')
 test_shape = test.shape
target = 'survived'
train.columns = train.columns.str.lower()
test.columns = test.columns.str.lower()
submission = pd.DataFrame({
"ID_code": id_codes,
"target": y_pred_logit_snapml
})
 submission.to_csv(filepath, index=False )<set_options>
train.apply(lambda col: len(col.unique()))
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
train.drop('fare', axis=1, inplace=True)
 test.drop('fare', axis=1, inplace=True )
train = pd.read_csv("/kaggle/input/santander-customer-transaction-prediction/train.csv")
 test = pd.read_csv("/kaggle/input/santander-customer-transaction-prediction/test.csv" )<sort_values>
def check_features_list(features):

 if type(features)== str:
features = [features]
return features
def convert_categorical_features(df, cat_features):

 if len(cat_features)== 0:
return df
cat_features = check_features_list(cat_features)
 for feature in cat_features:
dummies = pd.get_dummies(df[feature], prefix=feature)
 df = pd.concat([df,dummies], axis=1)
 df.drop(cat_features, axis=1, inplace=True)
 return df
correlations = train[features].corr().abs().unstack().sort_values(kind="quicksort" ).reset_index()
correlations = correlations[correlations['level_0'] != correlations['level_1']]
correlations.head(10 )<count_unique_values>
lr = LogisticRegression()
cat_features = ['pclass','sex','embarked']
for feature in cat_features:
accuracy = evaluate_lr_model(train, cat_features=feature)
 print('{}: {:.2f}%'.format(feature, accuracy*100))
num_features = ['sibsp','parch']
for feature in num_features:
accuracy = evaluate_lr_model(train, num_features=feature)
 print('{}: {:.2f}%'.format(feature, accuracy*100))
features = train.columns.values[2:202]
unique_max_train = []
unique_max_test = []
for feature in features:
values = train[feature].value_counts()
unique_max_train.append([feature, values.max() , values.idxmax() ])
 values = test[feature].value_counts()
unique_max_test.append([feature, values.max() , values.idxmax() ] )<feature_engineering>
accuracy = evaluate_lr_model(train, cat_features, num_features)
 print('{}: {:.2f}%'.format(cat_features + num_features, accuracy*100))
idx = features = train.columns.values[2:202]
for df in [test, train]:
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 )<define_variables>
def predict_test(model, train_c, test_c, filenamne):

 features = train_c.columns.to_list()
features.remove(target)
 model.fit(train_c[features], train_c[target])
 predictions = model.predict(test_c[features])
 submission = pd.DataFrame({"PassengerId":test["passengerid"],"Survived":predictions})
 submission.to_csv(filenamne, index=False)
 train_c = copy_convert_dataset(train, cat_features, num_features)
 test_c = copy_convert_dataset(test, cat_features, num_features, False)
 predict_test(lr, train_c, test_c, 'submission_1.csv' )
features = [c for c in train.columns if c not in ['ID_code', 'target']]
target = train['target']<init_hyperparams>
cat_features = ['pclass','sex','embarked','age_group']
accuracy = evaluate_lr_model(train, cat_features, num_features)
 print('{}: {:.2f}%'.format(cat_features + num_features, accuracy*100))
param = {
'bagging_freq': 5,
'bagging_fraction': 0.4,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}<train_model>
titles_map = {
"Mr" : "Mr",
"Mme": "Mrs",
"Ms": "Mrs",
"Mrs" : "Mrs",
"Master" : "Master",
"Mlle": "Miss",
"Miss" : "Miss",
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Dr": "Officer",
"Rev": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Countess": "Royalty",
"Dona": "Royalty",
"Lady" : "Royalty"
}
def extract_title(name):

 return name.str.extract('([A-Za-z]+)\.', expand=False ).map(titles_map)
 train["title"] = extract_title(train['name'])
 train.pivot_table(index='title',values='survived')['survived'].plot.barh()
set_spines()
folds = StratifiedKFold(n_splits=10, shuffle=False, random_state=44000)
 oof = np.zeros(len(train))
predictions = np.zeros(len(test))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train.values, target.values)) :
print("Fold {}".format(fold_))
trn_data = lgb.Dataset(train.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train.iloc[val_idx][features], label=target.iloc[val_idx])
 num_round = 1000000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 3000)
 oof[val_idx] = clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )<save_to_csv>
cat_features = ['pclass','sex','embarked','age_group','title']
accuracy = evaluate_lr_model(train, cat_features, num_features)
 print('{}: {:.2f}%'.format(cat_features + num_features, accuracy*100))
sub_df = pd.DataFrame({"ID_code":test["ID_code"].values})
 sub_df["target"] = predictions
sub_df.to_csv("submission.csv", index=False )<install_modules>
test['age_group'] = group_age(test['age'])
 test["title"] = extract_title(test['name'])
 train_c = copy_convert_dataset(train, cat_features, num_features)
 test_c = copy_convert_dataset(test, cat_features, num_features, False)
 predict_test(lr, train_c, test_c, 'submission_2.csv' )
!pip install.. /input/sacremoses > /dev/null
sys.path.insert(0, ".. /input/transformers/" )<set_options>
models = [
{
'name':'Logistic regression',
'estimator':LogisticRegression() ,
'hyperparameters':{
'solver': ['newton-cg', 'lbfgs', 'liblinear']
}
},
{
'name':'Decision tree',
'estimator':DecisionTreeClassifier(random_state=1),
'hyperparameters':{
'criterion':['entropy','gini'],
'splitter':['best','random'],
'max_depth':range(4,10),
'max_features':['auto','sqrt','log2'],
'min_samples_split':[2,3,4]
}
}, {
'name':'RandomForestClassifier',
'estimator':RandomForestClassifier(n_estimators=300, random_state=1),
'hyperparameters':{
'criterion':['entropy','gini'],
'max_depth':range(4,9),
'max_features':['auto','sqrt','log2'],
'min_samples_split':[2,3,4]
}
}
]
def tune_models(models, df):
start = time()
for model in models:
print(model['name'])
 print('-' * len(model['name']))
grid = GridSearchCV(model['estimator'], param_grid=model['hyperparameters'], cv=5)
 grid.fit(df.loc[:,df.columns != target], df[target])
 model['best_score'] = grid.best_score_
model['best_params'] = grid.best_params_
model['best_model'] = grid.best_estimator_
print('Best accuracy: {:.2f}%'.format(model['best_score']*100))
print('Best parameters: {}
'.format(model['best_params']))
end = time()
print()
print('Elapsed time: {:.2f}s'.format(end - start))
return models
models = tune_models(models, train_c )
np.set_printoptions(suppress=True)
 print(tf.__version__ )<load_from_csv>
predict_test(models[2]['best_model'], train_c, test_c, 'submission_3.csv' )