Kaggle competition: Titanic - Machine Learning from Disaster
arch = models.densenet121<find_best_params>
from sklearn.model_selection import train_test_split, validation_curve, learning_curve, GridSearchCV
from sklearn.linear_model import LinearRegression, Ridge, Lasso, LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor, plot_tree
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor, ExtraTreesClassifier
from sklearn.ensemble import BaggingClassifier, BaggingRegressor, GradientBoostingClassifier, GradientBoostingRegressor
from xgboost import XGBClassifier, XGBRegressor
from sklearn.metrics import log_loss, roc_auc_score, accuracy_score, precision_score, recall_score, roc_curve, precision_recall_curve, auc, make_scorer, confusion_matrix, f1_score, fbeta_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
acc_02 = partial(accuracy_thresh, thresh=0.19)
 f_score = partial(fbeta, thresh=0.19)
 learn = cnn_learner(data, arch, metrics=[acc_02, f_score], model_dir='/kaggle/working/' )<init_hyperparams>
train_data = pd.read_csv(".. /input/titanic/train.csv")
 test_data = pd.read_csv(".. /input/titanic/test.csv")
 train_data.groupby('Survived')['PassengerId'].nunique()
train_data.groupby('Pclass')['PassengerId'].nunique()
lr = 0.01<train_model>
train = train_data.copy()
train['Cab'] = pd.Series(['cab' + str(i)[0] for i in train.Cabin], index=train.index)
 del train['PassengerId']
del train['Name']
del train['Ticket']
del train['Cabin']
del train['Parch']
dummy = pd.get_dummies(train['Sex'])
 train = train.join(dummy)
 del train['Sex']
dummy = pd.get_dummies(train['Embarked'])
 train = train.join(dummy)
 del train['Embarked']
del train['Cab']
train["Age"].fillna(train["Age"].mean() , inplace=True)
 print(train.head())
 print(train.info() )
learn.fit_one_cycle(5, slice(lr))<save_model>
test = test_data.copy()
del test['PassengerId']
del test['Name']
del test['Ticket']
del test['Cabin']
del test['Parch']
dummy = pd.get_dummies(test['Sex'])
 test = test.join(dummy)
 del test['Sex']
dummy = pd.get_dummies(test['Embarked'])
 test = test.join(dummy)
 del test['Embarked']
test["Age"].fillna(test["Age"].mean() , inplace=True)
 test["Fare"].fillna(test["Fare"].mean() , inplace=True)
 test["SibSp"].fillna(0, inplace=True)
 print(test.info() )
learn.save('stage-1-rn50' )<train_model>
X = train.drop(['Survived'], axis=1)
 y = train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.05,
random_state=4 )
learn.fit_one_cycle(5, slice(1e-5, lr/5))<save_model>
XGBC_model = XGBClassifier(max_depth= 4, n_estimators=20)
 XGBC_model.fit(X_train, y_train)
 print('XGBC_model Training score:', XGBC_model.score(X_train,y_train))
print('XGBC_model Test score: ', XGBC_model.score(X_test,y_test),'
')
 extratree_model = ExtraTreesClassifier(max_depth=5)
 extratree_model.fit(X_train, y_train)
 print('ExtraTrees Training score:', extratree_model.score(X_train,y_train))
print('ExtraTrees Test score: ', extratree_model.score(X_test,y_test),'
')
 bagging_model = BaggingClassifier(n_estimators = 10, random_state=0)
 bagging_model.fit(X_train, y_train)
 print('Bagging Training score:', bagging_model.score(X_train,y_train))
print('Bagging Test score: ', bagging_model.score(X_test,y_test),'
')
 decision_tree_model = DecisionTreeClassifier(random_state=0, max_depth = 5)
 decision_tree_model.fit(X_train, y_train)
 print('Decision Tree Training score:', decision_tree_model.score(X_train,y_train))
print('Decision Tree Test score: ', decision_tree_model.score(X_test,y_test),'
')
 random_forest_model = RandomForestClassifier(random_state=0, max_depth = 5)
 random_forest_model.fit(X_train, y_train)
 print('Random Forest Training score:', random_forest_model.score(X_train,y_train))
print('Random Forest Test score: ', random_forest_model.score(X_test,y_test),'
')
 gradient_boosting_model = GradientBoostingClassifier(random_state= 0, n_estimators = 150, max_depth = 2)
 gradient_boosting_model.fit(X_train, y_train)
 print('Gradient Boosting Training score:', gradient_boosting_model.score(X_train,y_train))
print('Gradient Bosting Test score: ', gradient_boosting_model.score(X_test,y_test),'
' )
learn.save('stage-2-rn50' )<categorify>
scores = cross_val_score(estimator=XGBC_model, X=X, y=y, cv=10)
 print(scores, scores.mean() , scores.std() , "
")
 scores = cross_val_score(estimator=extratree_model, X=X, y=y, cv=10)
 print(scores, scores.mean() , scores.std() , "
")
 scores = cross_val_score(estimator=bagging_model, X=X, y=y, cv=10)
 print(scores, scores.mean() , scores.std() , "
")
 scores = cross_val_score(estimator=decision_tree_model, X=X, y=y, cv=10)
 print(scores, scores.mean() , scores.std() , "
")
 scores = cross_val_score(estimator=random_forest_model, X=X, y=y, cv=10)
 print(scores, scores.mean() , scores.std() , "
")
 scores = cross_val_score(estimator=gradient_boosting_model, X=X, y=y, cv=10)
 print(scores, scores.mean() , scores.std() , "
")
data =(src.transform(tfms, size=256)
.databunch(num_workers=0 ).normalize(imagenet_stats))
learn.data = data
data.train_ds[0][0].shape<find_best_params>
survived1 = gradient_boosting_model.predict(test)
 survived2 = random_forest_model.predict(test)
 survived3 = decision_tree_model.predict(test)
 survived4 = extratree_model.predict(test)
 survived5 = bagging_model.predict(test)
 survived6 = XGBC_model.predict(test)
 survived =(survived1 + survived2 + survived3 + survived4 + survived5 + survived6)/6
s = []
for i in survived:
if i >= 0.5:
s.append(1)
 else:
s.append(0)
 passenger_id = test_data['PassengerId']
d = pd.DataFrame(columns = ['PassengerId', 'Survived'])
 d['PassengerId'] = passenger_id
d['Survived'] = s
print(d)
 d.groupby('Survived')['PassengerId'].nunique()
d.to_csv('gradient_boosting.csv',index=False )