{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd, numpy as np, pandas_profiling as pp","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:46:50.048322Z","iopub.execute_input":"2022-07-26T20:46:50.049361Z","iopub.status.idle":"2022-07-26T20:46:50.054341Z","shell.execute_reply.started":"2022-07-26T20:46:50.049320Z","shell.execute_reply":"2022-07-26T20:46:50.053108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/titanic/train.csv')\ntest = pd.read_csv('../input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:46:50.069501Z","iopub.execute_input":"2022-07-26T20:46:50.070091Z","iopub.status.idle":"2022-07-26T20:46:50.089915Z","shell.execute_reply.started":"2022-07-26T20:46:50.070054Z","shell.execute_reply":"2022-07-26T20:46:50.089053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(15)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:46:50.094816Z","iopub.execute_input":"2022-07-26T20:46:50.095517Z","iopub.status.idle":"2022-07-26T20:46:50.119269Z","shell.execute_reply.started":"2022-07-26T20:46:50.095468Z","shell.execute_reply":"2022-07-26T20:46:50.118364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pp.ProfileReport(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:46:50.128978Z","iopub.execute_input":"2022-07-26T20:46:50.129571Z","iopub.status.idle":"2022-07-26T20:47:04.855937Z","shell.execute_reply.started":"2022-07-26T20:46:50.129536Z","shell.execute_reply":"2022-07-26T20:47:04.854672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Dictionary<br><br>\n- **survival**: Survival (0 = No, 1 = Yes)<br><br>\n- **pclass**: Ticket class (1 = 1st, 2 = 2nd, 3 = 3rd)<br><br>\n- **sex**: Sex<br><br>\n- **Age**: Age in years<br><br>\n- **sibsp**: Number of siblings and/or spouses aboard<br><br>\n- **parch**: Number of parents and/or children aboard<br><br>\n- **ticket**: Ticket number<br><br>\n- **fare**: Passenger fare<br><br>\n- **cabin**: Cabin number<br><br>\n- **embarked**: Port of Embarkation (C = Cherbourg, Q = Queenstown, S= Southampton)","metadata":{}},{"cell_type":"code","source":"# Featuring Engineering\n\ndef create_features(df):\n    df['FamilySize'] = df['SibSp'] + df['Parch']\n    df['LonePassenger'] = df['FamilySize'].apply(lambda x: 1 if x == 1 else 0) #1 = yes, 0 = no\n    df['Sex'] = df['Sex'].map({'male':0, 'female':1})\n    df['Embarked'] = df['Embarked'].fillna('S')\n    return df\n\ntrain = create_features(train)\ntest = create_features(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:47:04.858235Z","iopub.execute_input":"2022-07-26T20:47:04.858561Z","iopub.status.idle":"2022-07-26T20:47:04.873370Z","shell.execute_reply.started":"2022-07-26T20:47:04.858533Z","shell.execute_reply":"2022-07-26T20:47:04.872454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(['Name','Cabin','Ticket','SibSp','Parch'], axis=1, inplace = True)\ntest.drop(['Name','Cabin','Ticket','SibSp','Parch'], axis=1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:47:04.874764Z","iopub.execute_input":"2022-07-26T20:47:04.875380Z","iopub.status.idle":"2022-07-26T20:47:04.894983Z","shell.execute_reply.started":"2022-07-26T20:47:04.875321Z","shell.execute_reply":"2022-07-26T20:47:04.893618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:47:04.897273Z","iopub.execute_input":"2022-07-26T20:47:04.898620Z","iopub.status.idle":"2022-07-26T20:47:04.916238Z","shell.execute_reply.started":"2022-07-26T20:47:04.898413Z","shell.execute_reply":"2022-07-26T20:47:04.915217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pp.ProfileReport(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:47:26.514744Z","iopub.execute_input":"2022-07-26T20:47:26.515201Z","iopub.status.idle":"2022-07-26T20:47:37.513082Z","shell.execute_reply.started":"2022-07-26T20:47:26.515164Z","shell.execute_reply":"2022-07-26T20:47:37.511969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --ignore-installed pycaret --user","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:56:27.458225Z","iopub.execute_input":"2022-07-26T20:56:27.458668Z","iopub.status.idle":"2022-07-26T20:59:29.077321Z","shell.execute_reply.started":"2022-07-26T20:56:27.458630Z","shell.execute_reply":"2022-07-26T20:59:29.075941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pycaret.classification import *\nsetup(data = train, target = 'Survived', numeric_imputation = 'median', normalize = True,\n     remove_outliers=True, fold = 10)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T21:04:28.791965Z","iopub.execute_input":"2022-07-26T21:04:28.793123Z","iopub.status.idle":"2022-07-26T21:04:38.420198Z","shell.execute_reply.started":"2022-07-26T21:04:28.793072Z","shell.execute_reply":"2022-07-26T21:04:38.419040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_3 = compare_models(sort='Accuracy', n_select = 3)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T21:05:35.610487Z","iopub.execute_input":"2022-07-26T21:05:35.610908Z","iopub.status.idle":"2022-07-26T21:06:09.476925Z","shell.execute_reply.started":"2022-07-26T21:05:35.610873Z","shell.execute_reply":"2022-07-26T21:06:09.475972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catboost = create_model('catboost')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T21:08:26.663468Z","iopub.execute_input":"2022-07-26T21:08:26.663929Z","iopub.status.idle":"2022-07-26T21:08:36.874311Z","shell.execute_reply.started":"2022-07-26T21:08:26.663896Z","shell.execute_reply":"2022-07-26T21:08:36.873108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tuning model\ntuned_catboost = tune_model(catboost, n_iter=1000, optimize = 'Accuracy')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T21:09:51.302913Z","iopub.execute_input":"2022-07-26T21:09:51.303319Z","iopub.status.idle":"2022-07-26T21:57:29.221022Z","shell.execute_reply.started":"2022-07-26T21:09:51.303287Z","shell.execute_reply":"2022-07-26T21:57:29.219655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Blending top 3 models\nblended_model = blend_models(estimator_list = top_3, fold = 10, method='soft')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T22:01:34.140074Z","iopub.execute_input":"2022-07-26T22:01:34.140588Z","iopub.status.idle":"2022-07-26T22:01:46.371366Z","shell.execute_reply.started":"2022-07-26T22:01:34.140550Z","shell.execute_reply":"2022-07-26T22:01:46.369642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"calibrated_blended = calibrate_model(blended_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T22:02:06.568818Z","iopub.execute_input":"2022-07-26T22:02:06.569280Z","iopub.status.idle":"2022-07-26T22:02:56.512743Z","shell.execute_reply.started":"2022-07-26T22:02:06.569244Z","shell.execute_reply":"2022-07-26T22:02:56.511706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**tuned_catboost** has the best accuracy score of them all.","metadata":{}},{"cell_type":"code","source":"predictions = predict_model(tuned_catboost, data = test)\npredictions.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T22:04:57.884445Z","iopub.execute_input":"2022-07-26T22:04:57.885001Z","iopub.status.idle":"2022-07-26T22:04:58.499326Z","shell.execute_reply.started":"2022-07-26T22:04:57.884951Z","shell.execute_reply":"2022-07-26T22:04:58.497934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = predictions.drop(['Pclass','Sex','Age','Fare','Embarked','FamilySize','LonePassenger',\n                              'Score'], axis = 1)\nsubmission['Survived'] = predictions['Label']\nsubmission.drop(['Label'], axis = 1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T22:07:14.482737Z","iopub.execute_input":"2022-07-26T22:07:14.484574Z","iopub.status.idle":"2022-07-26T22:07:14.502625Z","shell.execute_reply.started":"2022-07-26T22:07:14.484521Z","shell.execute_reply":"2022-07-26T22:07:14.500679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T22:07:16.987770Z","iopub.execute_input":"2022-07-26T22:07:16.988235Z","iopub.status.idle":"2022-07-26T22:07:17.002077Z","shell.execute_reply.started":"2022-07-26T22:07:16.988199Z","shell.execute_reply":"2022-07-26T22:07:17.000708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission.to_csv(f'submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T22:15:58.828810Z","iopub.execute_input":"2022-07-26T22:15:58.830538Z","iopub.status.idle":"2022-07-26T22:15:58.836059Z","shell.execute_reply.started":"2022-07-26T22:15:58.830475Z","shell.execute_reply":"2022-07-26T22:15:58.834970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Score:** 0.77272","metadata":{}}]}