Kaggle competition: Titanic - Machine Learning from Disaster
xlnet_base_cased_models = create_xlnet_base_cased_models()
xlnet_base_cased_preds = predict(xlnet_base_cased_models, test_loader )<set_options>
training = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 training['train_test'] = 1
test['train_test'] = 0
test['Survived'] = np.NaN
all_data = pd.concat([training,test])
 %matplotlib inline
all_data.columns
del xlnet_base_cased_models, test_loader
torch.cuda.empty_cache()
gc.collect()<load_pretrained>
df_num = training[['Age','SibSp','Parch','Fare']]
df_cat = training[['Survived','Pclass','Sex','Ticket','Cabin','Embarked']]
test_loader, _ = get_test_loader(model_type="xlnet-base-cased", content="Question", batch_size=32 )<predict_on_test>
pd.pivot_table(training, index = 'Survived', values = ['Age','SibSp','Parch','Fare'] )
xlnet_base_cased_question_models = create_xlnet_base_cased_question_models()
xlnet_base_cased_question_preds = predict(xlnet_base_cased_question_models, test_loader )<set_options>
print(pd.pivot_table(training, index = 'Survived', columns = 'Pclass', values = 'Ticket' ,aggfunc ='count'))
print()
print(pd.pivot_table(training, index = 'Survived', columns = 'Sex', values = 'Ticket' ,aggfunc ='count'))
print()
print(pd.pivot_table(training, index = 'Survived', columns = 'Embarked', values = 'Ticket' ,aggfunc ='count'))
del xlnet_base_cased_question_models, test_loader
torch.cuda.empty_cache()
gc.collect()<load_pretrained>
df_cat.Cabin
training['cabin_multiple'] = training.Cabin.apply(lambda x: 0 if pd.isna(x)else len(x.split(' ')))
 training['cabin_multiple'].value_counts()
test_loader, _ = get_test_loader(model_type="xlnet-base-cased", content="Answer", batch_size=32 )<predict_on_test>
pd.pivot_table(training, index = 'Survived', columns = 'cabin_multiple', values = 'Ticket' ,aggfunc ='count' )
xlnet_base_cased_answer_models = create_xlnet_base_cased_answer_models()
xlnet_base_cased_answer_preds = predict(xlnet_base_cased_answer_models, test_loader )<set_options>
training['cabin_adv'] = training.Cabin.apply(lambda x: str(x)[0])
del xlnet_base_cased_answer_models, test_loader
torch.cuda.empty_cache()
gc.collect()<concatenate>
print(training.cabin_adv.value_counts())
 pd.pivot_table(training,index='Survived',columns='cabin_adv', values = 'Name', aggfunc='count' )
xlnet_base_cased_question_answer_preds = np.concatenate([xlnet_base_cased_question_preds, xlnet_base_cased_answer_preds], axis=1 )<load_pretrained>
training['numeric_ticket'] = training.Ticket.apply(lambda x: 1 if x.isnumeric() else 0)
 training['ticket_letters'] = training.Ticket.apply(lambda x: ''.join(x.split(' ')[:-1] ).replace('.','' ).replace('/','' ).lower() if len(x.split(' ')[:-1])>0 else 0)
test_loader, tokenizer = get_test_loader(model_type="roberta-base", content="Question", batch_size=32 )<predict_on_test>
training['numeric_ticket'].value_counts()
roberta_base_question_models = create_roberta_base_question_models(tokenizer)
 roberta_base_question_preds = predict(roberta_base_question_models, test_loader )<set_options>
pd.set_option("max_rows", None)
 training['ticket_letters'].value_counts()
del roberta_base_question_models, test_loader, tokenizer
torch.cuda.empty_cache()
gc.collect()<load_pretrained>
pd.pivot_table(training,index='Survived',columns='numeric_ticket', values = 'Ticket', aggfunc='count' )
test_loader, tokenizer = get_test_loader(model_type="roberta-base", content="Answer", batch_size=32 )<predict_on_test>
training.Name.head(50)
 training['name_title'] = training.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip())
roberta_base_answer_models = create_roberta_base_answer_models(tokenizer)
 roberta_base_answer_preds = predict(roberta_base_answer_models, test_loader )<set_options>
training['name_title'].value_counts()
del roberta_base_answer_models, test_loader, tokenizer
torch.cuda.empty_cache()
gc.collect()<concatenate>
all_data['cabin_multiple'] = all_data.Cabin.apply(lambda x: 0 if pd.isna(x)else len(x.split(' ')))
 all_data['cabin_adv'] = all_data.Cabin.apply(lambda x: str(x)[0])
 all_data['numeric_ticket'] = all_data.Ticket.apply(lambda x: 1 if x.isnumeric() else 0)
 all_data['ticket_letters'] = all_data.Ticket.apply(lambda x: ''.join(x.split(' ')[:-1] ).replace('.','' ).replace('/','' ).lower() if len(x.split(' ')[:-1])>0 else 0)
 all_data['name_title'] = all_data.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip())
 all_data.Age = all_data.Age.fillna(training.Age.median())
 all_data.Fare = all_data.Fare.fillna(training.Fare.median())
 all_data.dropna(subset=['Embarked'],inplace = True)
 all_data['norm_sibsp'] = np.log(all_data.SibSp+1)
 all_data['norm_sibsp'].hist()
all_data['norm_fare'] = np.log(all_data.Fare+1)
 all_data['norm_fare'].hist()
all_data.Pclass = all_data.Pclass.astype(str)
 all_dummies = pd.get_dummies(all_data[['Pclass','Sex','Age','SibSp','Parch','norm_fare','Embarked','cabin_adv','cabin_multiple','numeric_ticket','name_title','train_test']])
 X_train = all_dummies[all_dummies.train_test == 1].drop(['train_test'], axis =1)
 X_test = all_dummies[all_dummies.train_test == 0].drop(['train_test'], axis =1)
 y_train = all_data[all_data.train_test==1].Survived
y_train.shape
roberta_base_question_answer_preds = np.concatenate([roberta_base_question_preds, roberta_base_answer_preds], axis=1 )<load_pretrained>
scale = StandardScaler()
all_dummies_scaled = all_dummies.copy()
all_dummies_scaled[['Age','SibSp','Parch','norm_fare']]= scale.fit_transform(all_dummies_scaled[['Age','SibSp','Parch','norm_fare']])
 all_dummies_scaled
X_train_scaled = all_dummies_scaled[all_dummies_scaled.train_test == 1].drop(['train_test'], axis =1)
 X_test_scaled = all_dummies_scaled[all_dummies_scaled.train_test == 0].drop(['train_test'], axis =1)
 y_train = all_data[all_data.train_test==1].Survived
test_loader, _ = get_test_loader(model_type="bert-base-cased", content="Question", batch_size=32 )<predict_on_test>
from sklearn.model_selection import cross_val_score
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn import tree
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
bert_base_cased_question_models = create_bert_base_cased_question_models()
bert_base_cased_question_preds = predict(bert_base_cased_question_models, test_loader )<set_options>
gnb = GaussianNB()
cv = cross_val_score(gnb,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
del bert_base_cased_question_models, test_loader
torch.cuda.empty_cache()
gc.collect()<load_pretrained>
lr = LogisticRegression(max_iter = 2000)
 cv = cross_val_score(lr,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )
test_loader, _ = get_test_loader(model_type="bert-base-cased", content="Answer", batch_size=32 )<predict_on_test>
lr = LogisticRegression(max_iter = 2000)
 cv = cross_val_score(lr,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
bert_base_cased_answer_models = create_bert_base_cased_answer_models()
bert_base_cased_answer_preds = predict(bert_base_cased_answer_models, test_loader )<set_options>
dt = tree.DecisionTreeClassifier(random_state = 1)
 cv = cross_val_score(dt,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )
del bert_base_cased_answer_models, test_loader
torch.cuda.empty_cache()
gc.collect()<concatenate>
dt = tree.DecisionTreeClassifier(random_state = 1)
 cv = cross_val_score(dt,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
bert_base_cased_question_answer_preds = np.concatenate([bert_base_cased_question_preds, bert_base_cased_answer_preds], axis=1 )<load_pretrained>
knn = KNeighborsClassifier()
cv = cross_val_score(knn,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )
test_loader, _ = get_test_loader(model_type="bert-base-uncased", content="Question", batch_size=32 )<predict_on_test>
knn = KNeighborsClassifier()
cv = cross_val_score(knn,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
bert_base_uncased_question_models = create_bert_base_uncased_question_models()
bert_base_uncased_question_preds = predict(bert_base_uncased_question_models, test_loader )<set_options>
rf = RandomForestClassifier(random_state = 1)
 cv = cross_val_score(rf,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )
del bert_base_uncased_question_models, test_loader
torch.cuda.empty_cache()
gc.collect()<load_pretrained>
rf = RandomForestClassifier(random_state = 1)
 cv = cross_val_score(rf,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
test_loader, _ = get_test_loader(model_type="bert-base-uncased", content="Answer", batch_size=32 )<predict_on_test>
svc = SVC(probability = True)
 cv = cross_val_score(svc,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
bert_base_uncased_answer_models = create_bert_base_uncased_answer_models()
bert_base_uncased_answer_preds = predict(bert_base_uncased_answer_models, test_loader )<set_options>
xgb = XGBClassifier(random_state =1)
 cv = cross_val_score(xgb,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
del bert_base_uncased_answer_models, test_loader
torch.cuda.empty_cache()
gc.collect()<concatenate>
voting_clf = VotingClassifier(estimators = [('lr',lr),('knn',knn),('rf',rf),('gnb',gnb),('svc',svc),('xgb',xgb)], voting = 'soft' )
bert_base_uncased_question_answer_preds = np.concatenate([bert_base_uncased_question_preds, bert_base_uncased_answer_preds], axis=1 )<load_pretrained>
cv = cross_val_score(voting_clf,X_train_scaled,y_train,cv=5)
 print(cv)
 print(cv.mean() )
test_loader, _ = get_test_loader(model_type="bert-base-cased", batch_size=32 )<predict_on_test>
voting_clf.fit(X_train_scaled,y_train)
 y_hat_base_vc = voting_clf.predict(X_test_scaled ).astype(int)
 basic_submission = {'PassengerId': test.PassengerId, 'Survived': y_hat_base_vc}
base_submission = pd.DataFrame(data=basic_submission)
 base_submission.to_csv('base_submission.csv', index=False )
bert_base_cased_models = create_bert_base_cased_models()
bert_base_cased_preds = predict(bert_base_cased_models, test_loader )<set_options>
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
del bert_base_cased_models, test_loader
torch.cuda.empty_cache()
gc.collect()<load_pretrained>
def clf_performance(classifier, model_name):
print(model_name)
 print('Best Score: ' + str(classifier.best_score_))
print('Best Parameters: ' + str(classifier.best_params_))
test_loader, _ = get_test_loader(model_type="bert-base-uncased", batch_size=32 )<predict_on_test>
lr = LogisticRegression()
param_grid = {'max_iter' : [2000],
'penalty' : ['l1', 'l2'],
'C' : np.logspace(-4, 4, 20),
'solver' : ['liblinear']}
clf_lr = GridSearchCV(lr, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_lr = clf_lr.fit(X_train_scaled,y_train)
 clf_performance(best_clf_lr,'Logistic Regression' )
bert_base_uncased_models = create_bert_base_uncased_models()
bert_base_uncased_preds = predict(bert_base_uncased_models, test_loader )<set_options>
knn = KNeighborsClassifier()
param_grid = {'n_neighbors' : [3,5,7,9],
'weights' : ['uniform', 'distance'],
'algorithm' : ['auto', 'ball_tree','kd_tree'],
'p' : [1,2]}
clf_knn = GridSearchCV(knn, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_knn = clf_knn.fit(X_train_scaled,y_train)
 clf_performance(best_clf_knn,'KNN' )
del bert_base_uncased_models, test_loader
torch.cuda.empty_cache()
gc.collect()<define_variables>
svc = SVC(probability = True)
 param_grid = tuned_parameters = [{'kernel': ['rbf'], 'gamma': [.1,.5,1,2,5,10],
'C': [.1, 1, 10, 100, 1000]},
{'kernel': ['linear'], 'C': [.1, 1, 10, 100, 1000]},
{'kernel': ['poly'], 'degree' : [2,3,4,5], 'C': [.1, 1, 10, 100, 1000]}]
clf_svc = GridSearchCV(svc, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_svc = clf_svc.fit(X_train_scaled,y_train)
 clf_performance(best_clf_svc,'SVC' )
preds =(( bert_base_uncased_preds + bert_base_uncased_question_answer_preds)/2.0 \
+(xlnet_base_cased_preds + xlnet_base_cased_question_answer_preds)/2.0 \
+(bert_base_cased_preds + bert_base_cased_question_answer_preds)/2.0 \
+(roberta_base_preds + roberta_base_question_answer_preds)/2.0)/4.0 
<save_to_csv>
sub[TARGET_COLUMNS] = bert_base_uncased_question_answer_preds
sub.to_csv('submission_bert_base_uncased.csv', index=False)
 sub[TARGET_COLUMNS] =roberta_base_preds
sub.to_csv('submission_roberta_base.csv', index=False)
<prepare_output>
rf = RandomForestClassifier(random_state = 1)
 param_grid = {'n_estimators': [400,450,500,550],
'criterion':['gini','entropy'],
'bootstrap': [True],
'max_depth': [15, 20, 25],
'max_features': ['auto','sqrt', 10],
'min_samples_leaf': [2,3],
'min_samples_split': [2,3]}
clf_rf = GridSearchCV(rf, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_rf = clf_rf.fit(X_train_scaled,y_train)
 clf_performance(best_clf_rf,'Random Forest' )
sub[TARGET_COLUMNS] = preds<load_from_csv>
best_rf = best_clf_rf.best_estimator_.fit(X_train_scaled,y_train)
 feat_importances = pd.Series(best_rf.feature_importances_, index=X_train_scaled.columns)
 feat_importances.nlargest(20 ).plot(kind='barh' )
test = pd.read_csv(f'{DATA_DIR}/test.csv' )<merge>
test = test.set_index('qa_id' ).join(sub.set_index('qa_id'))<normalization>
xgb = XGBClassifier(random_state = 1)
 param_grid = {
'n_estimators': [450,500,550],
'colsample_bytree': [0.75,0.8,0.85],
'max_depth': [None],
'reg_alpha': [1],
'reg_lambda': [2, 5, 10],
'subsample': [0.55, 0.6,.65],
'learning_rate':[0.5],
'gamma':[.5,1,2],
'min_child_weight':[0.01],
'sampling_method': ['uniform']
}
clf_xgb = GridSearchCV(xgb, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_xgb = clf_xgb.fit(X_train_scaled,y_train)
 clf_performance(best_clf_xgb,'XGB')
def postprocessing(oof_df):
scaler = MinMaxScaler()
type_one_column_list = [
'question_conversational', \
'question_has_commonly_accepted_answer', \
'question_not_really_a_question', \
'question_type_choice', \
'question_type_compare', \
'question_type_consequence', \
'question_type_definition', \
'question_type_entity', \
'question_type_instructions',
]
oof_df[type_one_column_list] = scaler.fit_transform(oof_df[type_one_column_list])
 tmp = oof_df.copy(deep=True)
 for column in type_one_column_list:
oof_df.loc[tmp[column] <= 0.16667, column] = 0
oof_df.loc[(tmp[column] > 0.16667)&(tmp[column] <= 0.41667), column] = 0.333333
oof_df.loc[(tmp[column] > 0.41667)&(tmp[column] <= 0.58333), column] = 0.500000
oof_df.loc[(tmp[column] > 0.58333)&(tmp[column] <= 0.73333), column] = 0.666667
oof_df.loc[(tmp[column] > 0.73333), column] = 1
type_three_column_list = [
'question_interestingness_self',
]
scaler = MinMaxScaler(feature_range=(0, 1))
oof_df[type_three_column_list] = scaler.fit_transform(oof_df[type_three_column_list])
 tmp[type_three_column_list] = scaler.fit_transform(tmp[type_three_column_list])
 for column in type_three_column_list:
oof_df.loc[tmp[column] <= 0.385, column] = 0.333333
oof_df.loc[(tmp[column] > 0.385)&(tmp[column] <= 0.47), column] = 0.444444
oof_df.loc[(tmp[column] > 0.47)&(tmp[column] <= 0.525), column] = 0.5
oof_df.loc[(tmp[column] > 0.525)&(tmp[column] <= 0.605), column] = 0.555556
oof_df.loc[(tmp[column] > 0.605)&(tmp[column] <= 0.715), column] = 0.666667
oof_df.loc[(tmp[column] > 0.715)&(tmp[column] <= 0.8), column] = 0.833333
oof_df.loc[(tmp[column] > 0.8)&(tmp[column] <= 0.94), column] = 0.888889
oof_df.loc[(tmp[column] > 0.94), column] = 1
type_four_column_list = [
'answer_satisfaction'
]
scaler = MinMaxScaler(feature_range=(0.2, 1))
oof_df[type_four_column_list] = scaler.fit_transform(oof_df[type_four_column_list])
 tmp[type_four_column_list] = scaler.fit_transform(tmp[type_four_column_list])
 for column in type_four_column_list:
oof_df.loc[tmp[column] <= 0.233, column] = 0.200000
oof_df.loc[(tmp[column] > 0.233)&(tmp[column] <= 0.283), column] = 0.266667
oof_df.loc[(tmp[column] > 0.283)&(tmp[column] <= 0.315), column] = 0.300000
oof_df.loc[(tmp[column] > 0.315)&(tmp[column] <= 0.365), column] = 0.333333
oof_df.loc[(tmp[column] > 0.365)&(tmp[column] <= 0.433), column] = 0.400000
oof_df.loc[(tmp[column] > 0.433)&(tmp[column] <= 0.483), column] = 0.466667
oof_df.loc[(tmp[column] > 0.483)&(tmp[column] <= 0.517), column] = 0.500000
oof_df.loc[(tmp[column] > 0.517)&(tmp[column] <= 0.567), column] = 0.533333
oof_df.loc[(tmp[column] > 0.567)&(tmp[column] <= 0.633), column] = 0.600000
oof_df.loc[(tmp[column] > 0.633)&(tmp[column] <= 0.683), column] = 0.666667
oof_df.loc[(tmp[column] > 0.683)&(tmp[column] <= 0.715), column] = 0.700000
oof_df.loc[(tmp[column] > 0.715)&(tmp[column] <= 0.767), column] = 0.733333
oof_df.loc[(tmp[column] > 0.767)&(tmp[column] <= 0.833), column] = 0.800000
oof_df.loc[(tmp[column] > 0.883)&(tmp[column] <= 0.915), column] = 0.900000
oof_df.loc[(tmp[column] > 0.915)&(tmp[column] <= 0.967), column] = 0.933333
oof_df.loc[(tmp[column] > 0.967), column] = 1
oof_values = oof_df[TARGET_COLUMNS].values
DEGREE = len(oof_df)//45*9
oof_values = np.around(oof_values * DEGREE)/ DEGREE
oof_df[TARGET_COLUMNS] = oof_values
return oof_df<categorify>
y_hat_xgb = best_clf_xgb.best_estimator_.predict(X_test_scaled ).astype(int)
 xgb_submission = {'PassengerId': test.PassengerId, 'Survived': y_hat_xgb}
submission_xgb = pd.DataFrame(data=xgb_submission)
 submission_xgb.to_csv('xgb_submission3.csv', index=False )
test = postprocessing(test )<count_values>
best_lr = best_clf_lr.best_estimator_
best_knn = best_clf_knn.best_estimator_
best_svc = best_clf_svc.best_estimator_
best_rf = best_clf_rf.best_estimator_
best_xgb = best_clf_xgb.best_estimator_
voting_clf_hard = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc)], voting = 'hard')
 voting_clf_soft = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc)], voting = 'soft')
 voting_clf_all = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc),('lr', best_lr)], voting = 'soft')
 voting_clf_xgb = VotingClassifier(estimators = [('knn',best_knn),('rf',best_rf),('svc',best_svc),('xgb', best_xgb),('lr', best_lr)], voting = 'soft')
 print('voting_clf_hard :',cross_val_score(voting_clf_hard,X_train,y_train,cv=5))
print('voting_clf_hard mean :',cross_val_score(voting_clf_hard,X_train,y_train,cv=5 ).mean())
 print('voting_clf_soft :',cross_val_score(voting_clf_soft,X_train,y_train,cv=5))
print('voting_clf_soft mean :',cross_val_score(voting_clf_soft,X_train,y_train,cv=5 ).mean())
 print('voting_clf_all :',cross_val_score(voting_clf_all,X_train,y_train,cv=5))
print('voting_clf_all mean :',cross_val_score(voting_clf_all,X_train,y_train,cv=5 ).mean())
 print('voting_clf_xgb :',cross_val_score(voting_clf_xgb,X_train,y_train,cv=5))
print('voting_clf_xgb mean :',cross_val_score(voting_clf_xgb,X_train,y_train,cv=5 ).mean())
for column in TARGET_COLUMNS:
print(test[column].value_counts() )<prepare_output>
params = {'weights' : [[1,1,1],[1,2,1],[1,1,2],[2,1,1],[2,2,1],[1,2,2],[2,1,2]]}
vote_weight = GridSearchCV(voting_clf_soft, param_grid = params, cv = 5, verbose = True, n_jobs = -1)
 best_clf_weight = vote_weight.fit(X_train_scaled,y_train)
 clf_performance(best_clf_weight,'VC Weights')
 voting_clf_sub = best_clf_weight.best_estimator_.predict(X_test_scaled)
sub = test[TARGET_COLUMNS].reset_index()<feature_engineering>
voting_clf_hard.fit(X_train_scaled, y_train)
 voting_clf_soft.fit(X_train_scaled, y_train)
 voting_clf_all.fit(X_train_scaled, y_train)
 voting_clf_xgb.fit(X_train_scaled, y_train)
 best_rf.fit(X_train_scaled, y_train)
 y_hat_vc_hard = voting_clf_hard.predict(X_test_scaled ).astype(int)
 y_hat_rf = best_rf.predict(X_test_scaled ).astype(int)
 y_hat_vc_soft = voting_clf_soft.predict(X_test_scaled ).astype(int)
 y_hat_vc_all = voting_clf_all.predict(X_test_scaled ).astype(int)
 y_hat_vc_xgb = voting_clf_xgb.predict(X_test_scaled ).astype(int )
sub[ sub[TARGET_COLUMNS] > 1.0] = 1.0<load_from_csv>
final_data = {'PassengerId': test.PassengerId, 'Survived': y_hat_rf}
submission = pd.DataFrame(data=final_data)
 final_data_2 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_hard}
submission_2 = pd.DataFrame(data=final_data_2)
 final_data_3 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_soft}
submission_3 = pd.DataFrame(data=final_data_3)
 final_data_4 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_all}
submission_4 = pd.DataFrame(data=final_data_4)
 final_data_5 = {'PassengerId': test.PassengerId, 'Survived': y_hat_vc_xgb}
submission_5 = pd.DataFrame(data=final_data_5)
 final_data_comp = {'PassengerId': test.PassengerId, 'Survived_vc_hard': y_hat_vc_hard, 'Survived_rf': y_hat_rf, 'Survived_vc_soft' : y_hat_vc_soft, 'Survived_vc_all' : y_hat_vc_all, 'Survived_vc_xgb' : y_hat_vc_xgb}
comparison = pd.DataFrame(data=final_data_comp )
test = pd.read_csv(f'{DATA_DIR}/test.csv' )<define_variables>
comparison['difference_rf_vc_hard'] = comparison.apply(lambda x: 1 if x.Survived_vc_hard != x.Survived_rf else 0, axis =1)
 comparison['difference_soft_hard'] = comparison.apply(lambda x: 1 if x.Survived_vc_hard != x.Survived_vc_soft else 0, axis =1)
 comparison['difference_hard_all'] = comparison.apply(lambda x: 1 if x.Survived_vc_all != x.Survived_vc_hard else 0, axis =1)
n=test['url'].apply(lambda x:(( 'ell.stackexchange.com' in x)or('english.stackexchange.com' in x)) ).tolist()
spelling=[]
for x in n:
if x:
spelling.append(0.5)
 else:
spelling.append(0.)<prepare_output>
comparison.difference_hard_all.value_counts()
sub['question_type_spelling'] = spelling<count_values>
submission.to_csv('submission_rf.csv', index =False)
 submission_2.to_csv('submission_vc_hard.csv',index=False)
 submission_3.to_csv('submission_vc_soft.csv', index=False)
 submission_4.to_csv('submission_vc_all.csv', index=False)
 submission_5.to_csv('submission_vc_xgb2.csv', index=False )