Kaggle competition: DSNet: fastai Hackathon
df_all_processed.isnull().sum()<split>
from fastai.vision import *
df_ans = df_all_processed[df_all_processed['Survived'].isnull() ]
df_ans = df_ans.loc[:, df_ans.columns != 'Survived']
df_cv_and_test = df_all_processed[df_all_processed['Survived'].notnull() ]
df_cv_and_test_X = df_cv_and_test.drop(['Survived', 'ClusterLabel'], axis = 1)
 df_cv_and_test_Y = df_cv_and_test['Survived']
df_cv_X, df_test_X, df_cv_Y, df_test_Y = train_test_split(df_cv_and_test_X,
df_cv_and_test_Y,
test_size = 0.1,
random_state = 42 )<define_search_space>
path = Path(".. /input")
 train_path = path/"train/train"
test_path = path/"test/test"
sub_df = pd.read_csv(f"{path}/sample_submission.csv")
 test_df = pd.read_csv(f"{path}/sample_submission.csv" )
hyper_rf = dict(
 n_estimators = [85, 86, 87, 88, 89],
max_depth = [3, 4, 5, 6, 7, 8, 9, 10],
random_state = [42] 
)
 hyper_svm = dict(
 C = [6.0, 7.0, 8.0],
kernel = ['linear', 'poly', 'rbf', 'sigmoid'],
gamma = ['auto', 'scale'],
probability = [True],
random_state = [42] 
)
 hyper_logit = dict(
 C = [0.1, 1.0, 10.0],
solver = ['lbfgs'],
random_state = [42] 
)
 hyper_adaboost = dict(
 n_estimators = [54, 55, 56],
learning_rate = [0.35, 0.4, 0.45],
random_state = [42] 
)
 hyper_xgboost = {
'eta': [0, 0.00001],
'gamma': [0.001, 0.05, 0.1],
'max_depth': [3, 4, 5],
'probability': [True],
'random_state': [42]
}<choose_model_class>
train = get_image_files(train_path)
 test = get_image_files(test_path )
n_cv = 10
gscv_rf = GridSearchCV(RandomForestClassifier() , hyper_rf, scoring = 'accuracy', cv = n_cv)
 gscv_svm = GridSearchCV(SVC() , hyper_svm, scoring = 'accuracy', cv = n_cv)
 gscv_logit = GridSearchCV(LogisticRegression() , hyper_logit, scoring = 'accuracy', cv = n_cv)
 gscv_adab = GridSearchCV(AdaBoostClassifier() , hyper_adaboost, scoring = 'accuracy', cv = n_cv)
 gscv_xbt = GridSearchCV(XGBClassifier() , hyper_xgboost, scoring = 'accuracy', cv = n_cv )<train_model>
data= ImageDataBunch.from_folder(train_path,valid_pct = 0.2,test = test_path,ds_tfms = get_transforms() ,size = 224 ).normalize()
data.add_test(ImageList.from_df(test_df, path, folder="test/test"))
print('1.Fitting Random Forest')
 gscv_rf.fit(df_cv_X.copy() , df_cv_Y.copy())
 print('2.Fitting SVM')
 gscv_svm.fit(df_cv_X.copy() , df_cv_Y.copy())
 print('3.Fitting Logit')
 gscv_logit.fit(df_cv_X.copy() , df_cv_Y.copy())
 print('4.Fitting AdaBoost')
 gscv_adab.fit(df_cv_X.copy() , df_cv_Y.copy())
 print('5.Fitting XBoostTree')
 gscv_xbt.fit(df_cv_X.copy() , df_cv_Y.copy() )<find_best_params>
data.show_batch()
def get_CV_score(classifier_name, n_cv):
ans = [0] * n_cv
for i in range(0, n_cv):
command_str = classifier_name + '.cv_results_['split' + str(i)+ '_test_score']'
ans[i] = eval(command_str)[0]
return ans
train_score_list_rf = get_CV_score('gscv_rf', n_cv)
 train_score_list_svm = get_CV_score('gscv_svm', n_cv)
 train_score_list_logit = get_CV_score('gscv_logit', n_cv)
 train_score_list_adab = get_CV_score('gscv_adab', n_cv)
 train_score_list_xbt = get_CV_score('gscv_xbt', n_cv)
 df_train_score = pd.DataFrame(dict(
 estimator = ['1.rf'] * n_cv + ['2.svm'] * n_cv + ['3.logit'] * n_cv + ['4.adaboost'] * n_cv + ['5.xboosttree'] * n_cv,
score = train_score_list_rf + train_score_list_svm + train_score_list_logit + train_score_list_adab + train_score_list_xbt 
))
df_test_score = pd.DataFrame(dict(
 estimator = ['1.rf'] + ['2.svm'] + ['3.logit'] + ['4.adaboost'] + ['5.xboosttree'],
score = [accuracy_score(gscv_rf.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_svm.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_logit.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_adab.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_xbt.best_estimator_.predict(df_test_X), df_test_Y)] 
))
ggplot(aes(x = 'estimator', color = 'estimator', y = 'score')) +\
geom_boxplot(data = df_train_score)+\
geom_jitter(data = df_train_score)+ coord_cartesian(ylim = [0.7, 1])+\
geom_bar(data = df_test_score, stat = 'identity', alpha = 0.2, position = "dodge" )<train_model>
learn = cnn_learner(data,models.resnet50,metrics = error_rate,model_dir="/tmp/model/" )
def print_opt_hyper(dict_hyper, model):
for k in dict_hyper.keys() :
print(k + ': ' + str(model.best_estimator_.get_params() [k])+ ' -- ' + str(dict_hyper[k]))
print('Optimal Hyperparameters:')
 print('---')
 print('1.Random Forest')
 print_opt_hyper(hyper_rf, gscv_rf)
 print('---')
 print('2.SVM')
 print_opt_hyper(hyper_svm, gscv_svm)
 print('---')
 print('3.Logistic Regression')
 print_opt_hyper(hyper_logit, gscv_logit)
 print('---')
 print('4.Ada Boost')
 print_opt_hyper(hyper_adaboost, gscv_adab)
 print('---')
 print('5.XGBTree')
 print_opt_hyper(hyper_xgboost, gscv_xbt)
 print('---' )<choose_model_class>
lr_find(learn )
estimator_list = [('rf', gscv_rf.best_estimator_), 
('svm', gscv_svm.best_estimator_), 
('logit', gscv_logit.best_estimator_), 
('adab', gscv_adab.best_estimator_), 
('xbt', gscv_xbt.best_estimator_)]
hard_vote_estimator = VotingClassifier(estimator_list)
 soft_vote_estimator = VotingClassifier(estimator_list)
 meta_logit_estimator = StackingClassifier(classifiers = [X[1] for X in estimator_list],
meta_classifier = LogisticRegression() )<train_on_grid>
lr = 1e-03
%%capture
hard_cv = GridSearchCV(hard_vote_estimator, param_grid = {'voting': ['hard']},
scoring = 'accuracy', cv = n_cv ).fit(df_cv_X.copy() , df_cv_Y.copy())
 soft_cv = GridSearchCV(soft_vote_estimator, param_grid = {'voting': ['soft']},
scoring = 'accuracy', cv = n_cv ).fit(df_cv_X.copy() , df_cv_Y.copy())
 stack_cv = GridSearchCV(meta_logit_estimator, param_grid = {'meta_classifier__C': [0.1, 1, 10, 100, 500]},
scoring = 'accuracy', cv = n_cv ).fit(df_cv_X.copy() , df_cv_Y.copy() )<compute_test_metric>
learn.fit_one_cycle(5, slice(lr))
print('Hard Voting Score: ' + str(hard_cv.best_score_))
print('Soft Voting Score: ' + str(soft_cv.best_score_))
print('Stacking Score: ' + str(stack_cv.best_score_))<compute_train_metric>
learn.fit_one_cycle(5, slice(lr))
train_score_list_hv = get_CV_score('hard_cv', n_cv)
 train_score_list_sv = get_CV_score('soft_cv', n_cv)
 train_score_list_st = get_CV_score('stack_cv', n_cv)
 df_train_score = pd.DataFrame(dict(
 estimator = ['1.rf'] * len(train_score_list_rf)+
['2.svm'] * len(train_score_list_svm)+
['3.logit'] * len(train_score_list_logit)+
['4.adaboost'] * len(train_score_list_adab)+
['5.xbtree'] * len(train_score_list_xbt)+
['6.hard voter'] * len(train_score_list_hv)+
['7.soft voter'] * len(train_score_list_sv)+
['8.stacking'] * len(train_score_list_st)
 , score = train_score_list_rf + train_score_list_svm + train_score_list_logit + train_score_list_adab +\
train_score_list_xbt + train_score_list_hv + train_score_list_sv + train_score_list_st 
))
df_test_score = pd.DataFrame(dict(
 estimator = ['1.rf'] + ['2.svm'] + ['3.logit'] + ['4.adaboost'] + ['5.xbtree'] + ['6.hard voter'] + ['7.soft voter'] + ['8.stacking'],
score = [accuracy_score(gscv_rf.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_svm.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_logit.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_adab.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(gscv_xbt.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(hard_cv.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(soft_cv.best_estimator_.predict(df_test_X), df_test_Y),
accuracy_score(stack_cv.best_estimator_.predict(df_test_X), df_test_Y),
] 
))
ggplot(aes(x = 'estimator', y = 'score', color = 'estimator')) + geom_boxplot(data = df_train_score)+\
geom_jitter(data = df_train_score)+ theme(axis_text_x = element_text(angle = 70,
hjust = 1, vjust = 0.5)) + coord_cartesian(ylim = [0.7, 1])+\
geom_bar(data = df_test_score, stat = 'identity', alpha = 0.2, position = "dodge" )<load_from_csv>
learn.fit_one_cycle(5, slice(lr))
model_ids = ['RandomForest', 'SVM', 'AdaBoost', 'Logistic', 'XBTree', 'SoftVoting', 'HardVoting', 'Stacking']
for i, model in enumerate([gscv_rf, gscv_svm, gscv_adab, gscv_logit, gscv_xbt, soft_cv, hard_cv, stack_cv]):
submission_estimator = model.best_estimator_.fit(df_cv_and_test_X, df_cv_and_test_Y)
 survival_ans_col = pd.DataFrame({'Survived': submission_estimator.predict(df_ans.drop('ClusterLabel', axis = 1, inplace = False)) })
 df_submission = pd.read_csv('/kaggle/input/test.csv')
 df_submission['Survived'] = [int(X)for X in survival_ans_col['Survived'].to_list() ]
df_submission = df_submission.loc[:, ['PassengerId', 'Survived']]
df_submission.to_csv('submission.csv')
 os.chdir('/kaggle/working')
 df_submission.to_csv('submission-' + model_ids[i] + '.csv', index = False)
 FileLinks('.' )<load_from_csv>
learn.save("model-1" )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
lr_a = 1e-4
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<groupby>
learn.fit_one_cycle(5,slice(lr_a,lr/5))
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% de mujeres que sobrevivieron:", rate_women )<groupby>
learn.save("model-2" )
men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% de hombres que sobrevivieron:", rate_men )<count_values>
learn.fit_one_cycle(5,slice(1e-5,lr/5))
print('Total hombres:',len(men))
print('Total mujeres:',len(women))
print('Total sobrevivientes hombres:',sum(men))
print('Total sobrevivientes mujeres:',sum(women))<define_variables>
learn.save("model-3" )
parch_0 = train_data.loc[train_data.Parch == 0]["Survived"]
rate_parch_0 = sum(parch_0)/len(parch_0)
 print("% parch = 0 que sobrevivieron:", rate_parch_0)
 parch_1 = train_data.loc[train_data.Parch == 1]["Survived"]
rate_parch_1 = sum(parch_1)/len(parch_1)
 print("% parch = 1 que sobrevivieron:", rate_parch_1)
 parch_2 = train_data.loc[train_data.Parch == 2]["Survived"]
rate_parch_2 = sum(parch_2)/len(parch_2)
 print("% parch = 2 que sobrevivieron:", rate_parch_2)
 parch_3 = train_data.loc[train_data.Parch == 3]["Survived"]
rate_parch_3 = sum(parch_3)/len(parch_3)
 print("% parch = 3 que sobrevivieron:", rate_parch_3)
 parch_4 = train_data.loc[train_data.Parch == 4]["Survived"]
rate_parch_4 = sum(parch_4)/len(parch_4)
 print("% parch = 4 que sobrevivieron:", rate_parch_4)
 parch_5 = train_data.loc[train_data.Parch == 5]["Survived"]
rate_parch_5 = sum(parch_5)/len(parch_5)
 print("% parch = 5 que sobrevivieron:", rate_parch_5)
 parch_6 = train_data.loc[train_data.Parch == 6]["Survived"]
rate_parch_6 = sum(parch_6)/len(parch_6)
 print("% parch = 3 que sobrevivieron:", rate_parch_6)
 print('----')
 print('Parch0 sobrevivientes sobre el total',sum(parch_0)/len(train_data['Parch']))
print('Parch1 sobrevivientes sobre el total',sum(parch_1)/len(train_data['Parch']))
print('Parch2 sobrevivientes sobre el total',sum(parch_2)/len(train_data['Parch']))
print('Parch3 sobrevivientes sobre el total',sum(parch_3)/len(train_data['Parch']))
print('Parch4 sobrevivientes sobre el total',sum(parch_4)/len(train_data['Parch']))
print('Parch5 sobrevivientes sobre el total',sum(parch_5)/len(train_data['Parch']))
print('Parch6 sobrevivientes sobre el total',sum(parch_6)/len(train_data['Parch']))
print(sum(train_data['Survived'])/len(train_data['Survived']))<count_missing_values>
learn.fit_one_cycle(5,slice(1e-5,lr/5))
print('Vacios en embarked:',train_data['Embarked'].isnull().values.any())
 print('Vacios en Sex:',train_data['Sex'].isnull().values.any())
 print('Vacios en Name:',train_data['Name'].isnull().values.any())
 print('Vacios en Age:',train_data['Age'].isnull().values.any())
 print('Vacios en Cabin:',train_data['Cabin'].isnull().values.any())
 print('Vacios en Ticket:',train_data['Ticket'].isnull().values.any())
<count_missing_values>
learn.load("model-3" )
print('Embarked:',train_data['Embarked'].isnull().sum())
 print('Age:',train_data['Age'].isnull().sum())
 print('Cabin:',train_data['Cabin'].isnull().sum() )<define_variables>
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]
embarked_s = train_data.loc[train_data.Embarked == 'S']["Survived"]
rate_embarked_s = sum(embarked_s)/len(train_data['Survived'])
 print('% que sobrevivio con embarked = S: ',rate_embarked_s)
 embarked_c = train_data.loc[train_data.Embarked == 'C']["Survived"]
rate_embarked_c = sum(embarked_c)/len(train_data['Survived'])
 print('% que sobrevivio con embarked = C:',rate_embarked_c)
 embarked_q = train_data.loc[train_data.Embarked == 'Q']["Survived"]
rate_embarked_q = sum(embarked_q)/len(train_data['Survived'])
 print('% que sobrevivio con embarked = Q:',rate_embarked_q)
 print('s:',len(embarked_s), 'c:',len(embarked_c), 'q:',len(embarked_q))
embarked_s = train_data.loc[train_data.Embarked == 'S']["Survived"]
rate_embarked_s = sum(embarked_s)/len(embarked_s)
 print('%S: ',rate_embarked_s)
 embarked_c = train_data.loc[train_data.Embarked == 'C']["Survived"]
rate_embarked_c = sum(embarked_c)/len(embarked_c)
 print('%C:',rate_embarked_c)
 embarked_q = train_data.loc[train_data.Embarked == 'Q']["Survived"]
rate_embarked_q = sum(embarked_q)/len(embarked_q)
 print('%Q:',rate_embarked_q )<filter>
sub_df.predicted_class = test_preds
sub_df.to_csv("submission.csv", index=False)
train_data[train_data['Embarked'].isnull() ] 
<filter>
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv(index = False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 create_download_link(sub_df )
train_data[train_data['Age'].isnull() ] 
<filter>
FileLink("/tmp/model/export.pkl")
primera_clase = train_data.loc[train_data.Pclass == 1]["Survived"]
rate_1 = sum(primera_clase)/len(primera_clase)
 print("% Sobrevivientes en primera clase:", rate_1)
 segunda_clase = train_data.loc[train_data.Pclass == 2]["Survived"]
rate_2 = sum(segunda_clase)/len(segunda_clase)
 print("% Sobrevivientes en segunda clase:", rate_2)
 tercera_clase = train_data.loc[train_data.Pclass == 3]["Survived"]
rate_3 = sum(tercera_clase)/len(tercera_clase)
 print("% Sobrevivientes en tercera clase:", rate_3)
<data_type_conversions>
lr_find(learn )
train_data['Embarked'].fillna('S', inplace=True)
 train_data[(train_data['PassengerId'] == 62)|(train_data['PassengerId'] == 830)]<feature_engineering>
learn.save("model-2" )
train_data['Cabin'].fillna('M', inplace=True)
 train_data['Cabin_full'] = train_data.Cabin.str.slice(0, 1)
 test_data['Cabin'].fillna('M', inplace=True)
 test_data['Cabin_full'] = test_data.Cabin.str.slice(0, 1 )<split>
learn.load("model-2" )
train_data[train_data['Cabin'].isnull() ]<feature_engineering>
data_1= ImageDataBunch.from_folder(train_path,valid_pct = 0.2,test = test_path,ds_tfms = get_transforms() ,size = 128 ).normalize(imagenet_stats )
test_data['age_bins'] = pd.cut(x=test_data['Age'], bins=[0, 12, 20, 25, 40, 80])
 test_data['age_bins'].unique()<feature_engineering>
learn.data=data_1
learn = learn.to_fp16()
train_data.corr(method ='pearson' )<feature_engineering>
learn.freeze()
learn.lr_find()
learn.recorder.plot()
train_data['vaSolo'] = train_data['SibSp'] + train_data['Parch']
train_data['vaSolo'] = np.where(train_data['vaSolo'] == 0, 1, 0)
 train_data.head()<feature_engineering>
learn.fit_one_cycle(5,1e-3 )
test_data['vaSolo'] = test_data['SibSp'] + test_data['Parch']
test_data['vaSolo'] = np.where(test_data['vaSolo'] == 0, 1, 0)
 train_data.head()<split>
learn.lr_find()
features = ['Pclass', 'Sex', 'Embarked', 'age_bins', 'vaSolo', 'Cabin_full']
y = train_data['Survived'].values
X = pd.get_dummies(train_data[features])
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.40, random_state=5, stratify=y)
 X_test_r = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=300, max_depth=4, random_state=0, max_features=4)
 model.fit(X_train, y_train)
 y_pred= model.predict(X_test)
 model.score(X_test, y_test )<feature_engineering>
learn.fit_one_cycle(2,1e-6 )
X_test_r.head()
X_test_r['Cabin_full_T'] = 0<compute_test_metric>
interp = ClassificationInterpretation.from_learner(learn)
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
for feature in zip(features, model.feature_importances_):
print(feature)
<predict_on_test>
learn.save("model1" )
predictions = model.predict(X_test_r )<save_to_csv>
lr_find(learn )
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Se guardó el csv")
<import_modules>
df = pd.read_csv("submission.csv" )
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]
sub_df.predicted_class = test_preds
sub_df.to_csv("submission.csv", index=False)
 sub_df.head()
train_init_df=pd.read_csv('/kaggle/input/titanic/train.csv')
 train_init_df.head()<load_from_csv>
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv()
b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 create_download_link(df )
test_init_df=pd.read_csv('/kaggle/input/titanic/test.csv')
 test_PassengerId = test_init_df["PassengerId"]
test_init_df.head()<count_missing_values>
learn.export('/tmp/model/learn.pkl')