Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
show_graphs = True
add_interactions = False
model_tuning = False
feature_selection = False
train_df = pd.read_csv('.. /input/train.csv' )<load_from_csv>
used_features = {
'PassengerId': 0,
'Pclass': 1,
'Name': 0,
'LastName': 0,
'Title': 0,
'Sex': 1,
'Sex-female x Pclass-1-2': 1,
'Sex-male x Pclass-3': 0,
'SibSp': 0,
'Parch': 0,
'FamilySize': 0,
'FamilySizeBin': 0,
'IsAloneF': 0,
'Age': 0,
'AgeBin': 1,
'IsChild': 0,
'IsChild x Pclass-1-2': 1,
'Cabin': 0,
'HasCabin': 0,
'CabinType': 0,
'Embarked': 0,
'Ticket': 0,
'TicketSize': 1,
'TicketSizeBin': 0,
'IsAloneT': 1,
'Fare': 0,
'FareOrig': 0,
'FareBin': 1,
'NameFareSize': 0,
'Group': 0,
'GroupSurvived': 1,
'GroupSize': 0
}
test_df=pd.read_csv('.. /input/test.csv' )<concatenate>
%matplotlib inline
combined_df=pd.concat([train_df[['posts']],test_df[['posts']]] )<feature_engineering>
df_train = pd.read_csv('.. /input/train.csv')
 df_valid = pd.read_csv('.. /input/test.csv')
 yt = df_train['Survived']
df_train['Data'] = 'T'
df_valid['Data'] = 'V'
df_full = pd.concat([df_train, df_valid], sort=False, ignore_index=True)
 mask_train = df_full['Data'] == 'T'
mask_valid = df_full['Data'] == 'V'
vectorizer = CountVectorizer(stop_words='english',analyzer = "word",tokenizer = None,preprocessor = None,max_features = 10000)
 combined_df=vectorizer.fit_transform(combined_df['posts'] )<prepare_x_and_y>
catgroup(df_full.loc[mask_train], 'Pclass', 'Survived' )
train = combined_df[:train_df.shape[0]]
test = combined_df[train_df.shape[0]:]
y=train_df['type']<split>
df_full['LastName'] = df_full['Name'].str.extract('^([^,]+),', expand=False )
y_mind = y.apply(lambda x: 0 if x[0] == 'I' else 1)
 y_energy = y.apply(lambda x: 0 if x[1] == 'S' else 1)
 y_nature = y.apply(lambda x: 0 if x[2] == 'F' else 1)
 y_tactics = y.apply(lambda x: 0 if x[3] == 'P' else 1 )<import_modules>
df_full['Title'] = df_full['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 dict_replace = {
"Capt": "Officer",
"Col": "Officer",
"Countess": "Noble",
"Don": "Noble",
"Dona": "Noble",
"Dr": "Noble",
"Jonkheer": "Noble",
"Lady": "Noble",
"Major": "Officer",
"Mlle": "Miss",
"Mme": "Mrs",
"Ms": "Miss",
"Rev": "Noble",
"Sir": "Noble",
}
df_full['Title'] = df_full['Title'].replace(dict_replace )
from sklearn.metrics import f1_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_validate
from sklearn.ensemble import GradientBoostingClassifier<choose_model_class>
catgroup(df_full.loc[mask_train], 'Sex', 'Survived' )
clf = GradientBoostingClassifier(n_estimators=200,learning_rate=0.1, random_state=0)
 kfolds = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
 scoring = {'f1': 'f1_micro'}<compute_train_metric>
df_full['Sex'] = df_full['Sex'].map({'male': 0, 'female': 1} )
result = cross_validate(clf,train, y_mind, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )<compute_test_metric>
mask_female = df_full['Sex'] == 1
mask_class12 = df_full['Pclass'].isin([1, 2])
 df_full['Sex-female x Pclass-1-2'] =(mask_female & mask_class12 ).astype(int )
print('Y_mind model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )<compute_train_metric>
catgroup(df_full.loc[mask_train], 'Sex-female x Pclass-1-2', 'Survived' )
result = cross_validate(clf,train, y_energy, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )<compute_test_metric>
mask_male = df_full['Sex'] == 0
mask_class3 = df_full['Pclass'] == 3
df_full['Sex-male x Pclass-3'] =(mask_male & mask_class3 ).astype(int )
print('Y_energy model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )<compute_train_metric>
catgroup(df_full.loc[mask_train], 'Sex-male x Pclass-3', 'Survived' )
result = cross_validate(clf,train, y_nature, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )<compute_test_metric>
df_full['FamilySize'] = df_full['SibSp'] + df_full['Parch'] + 1
print('Y_nature model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )<compute_train_metric>
catgroup(df_full[mask_train], 'FamilySize', 'Survived' )
result = cross_validate(clf,train, y_tactics, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )<compute_test_metric>
df_full['FamilySizeBin'] = df_full['FamilySizeBin'].map({'alone': 0, 'normal': 1, 'big': 2} )
print('Y_tactics model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )<predict_on_test>
df_full['IsAloneF'] =(df_full['FamilySizeBin'] == 0 ).astype(int)
 catgroup(df_full.loc[mask_train], 'IsAloneF', 'Survived' )
clf.fit(train,y_mind)
 X_t = test
predictions_Mind = clf.predict_proba(X_t)
 Mind = pd.DataFrame(predictions_Mind[:,1], columns = ['Mind'] )<predict_on_test>
mask_noage = df_full['Age'].isnull()
df_noage = df_full.loc[mask_noage]
df_noage.groupby(['Title', 'Pclass'], as_index=False)['Name'].count()
clf.fit(train,y_energy)
 predictions_Energy = clf.predict_proba(X_t)
 Energy = pd.DataFrame(predictions_Energy[:,1], columns = ['Energy'] )<predict_on_test>
df_medians = df_full.groupby('Title')['Age'].median()
for idx, median in df_medians.iteritems() :
mask_group = df_full['Title'] == idx
df_full.loc[mask_group & mask_noage, 'Age'] = median
clf.fit(train,y_tactics)
 predictions_Tactic = clf.predict_proba(X_t)
 Tactic = pd.DataFrame(predictions_Energy[:,1], columns = ['Tactic'] )<predict_on_test>
df_full['IsChild'] =(df_full['Age'] < 16 ).astype(int)
 catgroup(df_full.loc[mask_train], 'IsChild', 'Survived' )
clf.fit(train,y_nature)
 predictions_Nature = clf.predict_proba(X_t)
 Nature = pd.DataFrame(predictions_Nature[:,1], columns = ['Nature'] )<concatenate>
mask_class12 = df_full['Pclass'].isin([1, 2])
 df_full['IsChild x Pclass-1-2'] = df_full['IsChild'] * mask_class12.astype(int)
 catgroup(df_full.loc[mask_train], 'IsChild x Pclass-1-2', 'Survived' )
submission = pd.concat([Mind,Energy,
Nature,Tactic], axis=1)
<prepare_output>
df_full['HasCabin'] = df_full['Cabin'].notnull().astype(int)
 mask_class1 =(df_full['Pclass'] == 1 ).astype(int)
 print("Correlation with 1st class: ", df_full['HasCabin'].corr(mask_class1))
catgroup(df_full.loc[mask_train], 'HasCabin', 'Survived' )
submission['index'] = submission['index'] +1
submission.columns = ['id', 'mind', 'energy', 'nature', 'tactics']
submission.head() 
<save_to_csv>
mask_noembarked = df_full['Embarked'].isnull()
df_full[mask_noembarked]
submission.to_csv('submission_Gradient.csv', index=False )<set_options>
df_ticket = df_full.loc[mask_train].groupby('Ticket', as_index=False)['Survived', 'Name'].agg({'Survived': 'mean', 'Name': 'count'})
 df_ticket = df_ticket.groupby('Name', as_index=False)['Survived'].mean()
df_ticket = df_ticket.sort_values(by='Survived')
 df_ticket
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 %matplotlib inline
if not sys.warnoptions:
warnings.simplefilter("ignore")
<load_from_csv>
df_ticket = df_full.groupby('Ticket')['Name'].count()
df_full['TicketSize'] = df_full['Ticket'].map(df_ticket )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 test_id = test['id']
train_test_combined = pd.concat([train,test] )<feature_engineering>
df_full['TicketSizeBin'] = df_full['TicketSizeBin'].map({'alone': 0, 'normal': 1, 'big': 2} )
def remove_url(string):

 pattern_url = r'http[s]?://(?:[A-Za-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9A-Fa-f][0-9A-Fa-f])) +'
string = re.sub(pattern_url, " ", string)
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_url )<feature_engineering>
df_full['IsAloneT'] =(df_full['TicketSizeBin'] == 0 ).astype(int)
 catgroup(df_full.loc[mask_train], 'IsAloneT', 'Survived' )
def remove_numbers(string):

 pattern_number = r'[0-9]*'
string = re.sub(pattern_number, "", string)
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_numbers )<feature_engineering>
fare_scaler = 'TicketSize'
df_full['Fare'] = df_full['Fare'] / df_full[fare_scaler]
def remove_punc(strings):

 for punctuation in string.punctuation:
strings = strings.replace(punctuation, ' ')
 return strings
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_punc )<feature_engineering>
mask_zerofare = df_full['Fare'] == 0
df_full.loc[mask_zerofare]
def remove_extra_spacing(string):

 string = re.sub('\s+', ' ', string)
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_extra_spacing )<string_transform>
df_full['Fare'].fillna(df_full['Fare'].median() , inplace=True )
stopwords_list = set(stopwords.words("english"))
lemmatiser = WordNetLemmatizer()
def remove_stopwords(string):

 string = " ".join([lemmatiser.lemmatize(word)for word in string.split() if(word not in stopwords_list)&(len(word)< 15)])
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_stopwords )<categorify>
df_full['FareOrig'] = df_full['Fare'] * df_full[fare_scaler]
def generate_targets(data):

 df = data.copy()
df['mind'] = df['type'].apply(lambda x: x[0] == 'E' ).astype('int')
 df['energy'] = df['type'].apply(lambda x: x[1] == 'N' ).astype('int')
 df['nature'] = df['type'].apply(lambda x: x[2] == 'T' ).astype('int')
 df['tactics'] = df['type'].apply(lambda x: x[3] == 'J' ).astype('int')
 return df[['mind','energy','nature','tactics']]
y = generate_targets(train)
 y.head()<feature_engineering>
df_full['FareBin'] = pd.qcut(df_full['Fare'], 4, labels=False ).astype(int )
tfidvectorizer = TfidfVectorizer(stop_words='english',min_df=1,max_df=0.95)
 tfidfvectorized_X = tfidvectorizer.fit_transform(train_test_combined['posts'] )<split>
df_familygroup = df_full.loc[mask_train].groupby(['LastName', 'FareOrig'], as_index=False)['Survived', 'Name'].agg({'Survived': 'mean', 'Name': 'count'})
 df_familygroup = df_familygroup.groupby('Name', as_index=False)['Survived'].mean()
df_familygroup = df_familygroup.sort_values(by='Survived')
 df_familygroup
X = tfidfvectorized_X[:len(y)]
X_submission = tfidfvectorized_X[len(y):]
print(X.shape, y.shape, X_submission.shape)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, stratify=y, random_state=22 )<choose_model_class>
df_familygroup = df_full.groupby(['LastName', 'FareOrig'])['Name'].count()
df_full['NameFareSize'] = df_full[['LastName', 'FareOrig']].apply(lambda row: df_familygroup[(row[0], row[1])], axis=1 )
LR_model = LogisticRegression()
results_index = ['Accuracy','Precision','Recall','F1 Score']
LR_model_results = pd.DataFrame(index = results_index, columns=list(y.columns))
LR_default_submission_dict = {'id':test['id']}
print(" Logistic Regression(Default Parameters)")
 print('-'*len(" Logistic Regression(Default Parameters)"))
for attribute in list(y.columns):
LR_model.fit(X_train, y_train[attribute])
 y_pred = LR_model.predict(X_test)
 LR_default_submission_dict[attribute] = LR_model.predict(X_submission)
 LR_model_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 LR_model_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 LR_model_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 LR_model_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 LR_model_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],y_pred)
 LR_model_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print('
')
 print(" Logistic Regression Results(Default Parameters)")
 print("-"*len(" Logistic Regression Results(Default Parameters)"))
print(LR_model_results)
<choose_model_class>
df_full['Group'] = ''
df_groups = df_full.groupby(['LastName', 'FareOrig'])
 for group, df_group in df_groups:
for idx, row in df_group.iterrows() :
group_members = df_group.drop(idx)['PassengerId'].tolist()
df_full.at[idx, 'Group'] = group_members
LR_model = LogisticRegression()
grid_parameters = {"C":[0.05,0.5,1,5,10,20,25,30],
"solver" : ['lbfgs', 'liblinear', 'sag', 'saga'],
"class_weight": ['balanced']}
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=22)
 results_index = ['Accuracy','Precision','Recall','F1 Score']
LR_model_GSCV_results = pd.DataFrame(index = results_index, columns=list(y.columns))
LR_submission_dict = {'id':test['id']}
print(" Logistic Regression(Grid Searched Parameters)")
 print("-"*len(" Logistic Regression(Grid Searched Parameters "))
print("(Best parameters per attribute)")
 print(" ")
 for attribute in list(y.columns):
LR_model_GSCV = GridSearchCV(LR_model, param_grid = grid_parameters, scoring = 'f1', cv = kfold)
 LR_model_GSCV.fit(X_train, y_train[attribute])
 print(f'{attribute}:',LR_model_GSCV.best_params_)
 print(" ")
 best_model = LR_model_GSCV.best_estimator_
y_pred = best_model.predict(X_test)
 LR_submission_dict[attribute] = best_model.predict(X_submission)
 LR_model_GSCV_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],best_model.predict(X_test))
LR_model_GSCV_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print(" ")
 print("-"*len(" Logistic Regression Results(Grid Searched Parameters)"))
print(" Logistic Regression Results(Grid Searched Parameters)")
 print("-"*len(" Logistic Regression Results(Grid Searched Parameters)"))
print(LR_model_GSCV_results)
 LR_submission_df = pd.DataFrame(LR_submission_dict)
<choose_model_class>
df_groups = df_full.groupby('Ticket')
 for group, df_group in df_groups:
for idx, row in df_group.iterrows() :
group_members = df_group.drop(idx)['PassengerId'].tolist()
df_full.at[idx, 'Group'].extend(group_members)
 df_full['Group'] = df_full['Group'].map(set )
KNN_model = KNeighborsClassifier(weights='distance', n_neighbors=3)
 results_index = ['Accuracy','Precision','Recall','F1 Score']
KNN_model_results = pd.DataFrame(index = results_index, columns=list(y.columns))
KNN_submission_dict = {'id':test['id']}
print(" K-Nearest-Neighbors Results ")
 print('-'*len(" K-Nearest-Neighbors Results "))
for attribute in list(y.columns):
KNN_model.fit(X_train, y_train[attribute])
 y_pred = KNN_model.predict(X_test)
 KNN_submission_dict[attribute] = KNN_model.predict(X_submission)
 KNN_model_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 KNN_model_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 KNN_model_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 KNN_model_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 KNN_model_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],y_pred)
 KNN_model_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print('
')
 print(" K-Nearest-Neighbors Results ")
 print("-"*49)
 print(KNN_model_results )<choose_model_class>
def group_survived(group):
mask_group = df_full['PassengerId'].isin(group)
 s = df_full.loc[mask_group, 'Survived'].max()
return s if pd.notnull(s)else 0.5
df_full['GroupSurvived'] = df_full['Group'].apply(group_survived )
RFC_model = RandomForestClassifier()
grid_parameters = {"n_estimators":[100,200],
"class_weight": ['balanced']}
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=22)
 results_index = ['Accuracy','Precision','Recall','F1 Score']
RFC_model_GSCV_results = pd.DataFrame(index = results_index, columns=list(y.columns))
RFC_submission_dict = {'id':test['id']}
print(" Random Forest Classifier(Grid Searched Parameters)")
 print("-"*len(" Random Forest Classifier(Grid Searched Parameters)"))
print("(Best parameters per attribute)")
 print(" ")
 for attribute in list(y.columns):
RFC_model_GSCV = GridSearchCV(RFC_model, param_grid = grid_parameters, scoring = 'neg_log_loss', cv = kfold)
 RFC_model_GSCV.fit(X_train, y_train[attribute])
 print(f'{attribute}:',RFC_model_GSCV.best_params_)
 print(" ")
 best_model = RFC_model_GSCV.best_estimator_
y_pred = best_model.predict(X_test)
 RFC_submission_dict[attribute] = best_model.predict(X_submission)
 RFC_model_GSCV_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print(" ")
 print("-"*len(" Random Forest Classifier Results(Grid Searched Parameters)"))
print(" Random Forest Classifier Results(Grid Searched Parameters)")
 print("-"*len(" Random Forest Classifier Results(Grid Searched Parameters)"))
print(RFC_model_GSCV_results )<save_to_csv>
df_full['GroupSize'] = df_full['Group'].str.len() + 1
LR_submission_df = pd.DataFrame(LR_submission_dict)
 LR_submission_df.to_csv("LR_submission_df.csv",index=False )<set_options>
df_fullgroup = df_full.loc[mask_train].groupby('GroupSize', as_index=False)['Survived'].mean()
df_fullgroup = df_fullgroup.sort_values(by='Survived')
 df_fullgroup
%matplotlib inline
class color:
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
warnings.filterwarnings("ignore" )<load_from_csv>
list_drop_features = [name for name, include in used_features.items() if not include]
df_full.drop(columns=list_drop_features, inplace=True )
df_train = pd.read_csv('.. /input/train.csv')
 df_train.head()<load_from_csv>
base_columns = ['Survived', 'Data']
data_columns = [col for col in df_full.columns if col not in base_columns]
scaler = StandardScaler()
df_full.loc[mask_train, data_columns] = scaler.fit_transform(df_full.loc[mask_train, data_columns])
 df_full.loc[mask_valid, data_columns] = scaler.transform(df_full.loc[mask_valid, data_columns] )
test_df = pd.read_csv('.. /input/test.csv')
 test_df.head()<train_model>
Xt = df_full.loc[mask_train].drop(columns=base_columns)
 Xv = df_full.loc[mask_valid].drop(columns=base_columns )
print('Training set consist of', round(len(df_train)/8675*100),'% data')
 print('Testing set consist of', round(len(test_df)/8675*100),'% data' )<string_transform>
models = {}
models['Logistic Regression'] = LogisticRegression(penalty='l2', C=1.0, random_state=0, solver='saga', max_iter=300)
 models['SVC_rbf'] = SVC(probability=True, kernel='rbf', gamma='scale', random_state=0)
 models['SVC_lin'] = SVC(probability=True, kernel='linear', random_state=0)
 models['KNN'] = KNeighborsClassifier(algorithm='auto', leaf_size=20, metric='minkowski', metric_params=None,
n_jobs=1, n_neighbors=10, p=3, weights='uniform')
 dtree_params = {'criterion': 'entropy', 'max_depth': 5, 'max_features': None, 'min_impurity_decrease': 0.0,
'min_samples_leaf': 0.01, 'min_samples_split': 0.01, 'min_weight_fraction_leaf': 0.0, 'splitter': 'best'}
models['Decision Tree'] = DecisionTreeClassifier(**dtree_params)
 models['Random Forest'] = RandomForestClassifier(criterion='entropy', n_estimators=200, oob_score=True)
 xgb_params = {'subsample': 0.5, 'reg_lambda': 5, 'reg_alpha': 0, 'n_estimators': 200, 'min_child_weight': 0, 'max_depth': 6,
'max_delta_step': 1, 'learning_rate': 1.0, 'gamma': 2, 'colsample_bytree': 0.5, 'colsample_bylevel': 0.5}
models['XGBoost'] = XGBClassifier(objective='binary:logistic', **xgb_params )
len(df_train.iloc[1,1].split('|||'))<count_values>
scoring = ['accuracy']
cv = StratifiedShuffleSplit(n_splits=10, test_size=0.3, random_state=0)
 for mname, model in models.items() :
result = cross_validate(model, Xt, yt, scoring=scoring, cv=cv, return_train_score=False)
 print("CV results for model {}: mean {:2.4f}, std {:2.4f}".format(mname, np.mean(result['test_accuracy']), np.std(result['test_accuracy'])) )
train_val_count=df_train['type'].value_counts()
train_val_count<count_values>
if model_tuning:
param_grid_XGBoost = {
'colsample_bytree': [0.1, 0.5, 1],
'colsample_bylevel': [0.1, 0.5, 1],
'subsample': [0.1, 0.5, 1],
'learning_rate': [0.05, 0.1, 0.3, 1.0],
'max_depth': [0, 3, 6, 10],
'reg_alpha': [0, 0.1, 1, 5],
'reg_lambda': [0, 0.1, 1, 5],
'gamma': [0, 1, 2, 5],
'n_estimators': [100, 200, 300, 500],
'min_child_weight': [0, 1, 2, 5],
'max_delta_step': [0, 1, 2, 5],
}
param_grid_DTC = {
'criterion': ['gini', 'entropy'],
'splitter': ['best', 'random'],
'max_depth': [None, 3, 5, 7, 10],
'min_samples_split': [2, 0.01, 0.05, 0.1],
'min_samples_leaf': [1, 0.01, 0.05, 0.1],
'min_weight_fraction_leaf': [0.0, 0.1, 0.2],
'max_features': [None, 'auto'],
'min_impurity_decrease': [0.0, 0.2, 0.4, 0.7],
}
tune_model = GridSearchCV(models['Decision Tree'], param_grid=param_grid_DTC, scoring='roc_auc', cv=cv)
 tune_model.fit(Xt, yt)
 print('Best parameters:
', tune_model.best_params_ )
<define_variables><EOS> 
list_submit = models.keys()
dict_submissions = {}
for mname in list_submit:
model = models[mname]
model.fit(Xt, yt)
 ytp = model.predict(Xt)
 acc = model.score(Xt, yt)
 yvp = model.predict(Xv)
 dict_submissions[mname] = yvp
submission = pd.DataFrame({"PassengerId": df_valid["PassengerId"], "Survived": yvp})
 submission.to_csv('submission_{}.csv'.format(mname), index=False )