{"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":"markdown","source":"Using Deep Learning - Accuracy score of 0.78 on Test Data","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"id":"PccXnMAAIVhH","execution":{"iopub.status.busy":"2022-07-31T14:00:03.911116Z","iopub.execute_input":"2022-07-31T14:00:03.911572Z","iopub.status.idle":"2022-07-31T14:00:03.916304Z","shell.execute_reply.started":"2022-07-31T14:00:03.911534Z","shell.execute_reply":"2022-07-31T14:00:03.915494Z"},"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')\nsubmission = pd.read_csv('../input/titanic/gender_submission.csv')","metadata":{"id":"AKstqPFUInuv","outputId":"7bba9bf4-46d9-4aa3-c135-38284d5b06a4","execution":{"iopub.status.busy":"2022-07-31T14:00:03.932007Z","iopub.execute_input":"2022-07-31T14:00:03.932882Z","iopub.status.idle":"2022-07-31T14:00:03.954288Z","shell.execute_reply.started":"2022-07-31T14:00:03.932829Z","shell.execute_reply":"2022-07-31T14:00:03.953437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"id":"t4OnT1peKBNL","execution":{"iopub.status.busy":"2022-07-31T14:00:03.969789Z","iopub.execute_input":"2022-07-31T14:00:03.970817Z","iopub.status.idle":"2022-07-31T14:00:03.997197Z","shell.execute_reply.started":"2022-07-31T14:00:03.970777Z","shell.execute_reply":"2022-07-31T14:00:03.996331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"id":"gSziIXakKBuw","execution":{"iopub.status.busy":"2022-07-31T14:00:04.002448Z","iopub.execute_input":"2022-07-31T14:00:04.002884Z","iopub.status.idle":"2022-07-31T14:00:04.035395Z","shell.execute_reply.started":"2022-07-31T14:00:04.002838Z","shell.execute_reply":"2022-07-31T14:00:04.034504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"id":"RT50GYnaKfAc","execution":{"iopub.status.busy":"2022-07-31T14:00:04.037180Z","iopub.execute_input":"2022-07-31T14:00:04.038320Z","iopub.status.idle":"2022-07-31T14:00:04.054041Z","shell.execute_reply.started":"2022-07-31T14:00:04.038278Z","shell.execute_reply":"2022-07-31T14:00:04.052865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()[train.isnull().sum() != 0].sort_values(ascending=False) *100 / train.shape[0]","metadata":{"id":"f3wydd4pKfwT","execution":{"iopub.status.busy":"2022-07-31T14:00:04.056014Z","iopub.execute_input":"2022-07-31T14:00:04.056378Z","iopub.status.idle":"2022-07-31T14:00:04.072796Z","shell.execute_reply.started":"2022-07-31T14:00:04.056344Z","shell.execute_reply":"2022-07-31T14:00:04.071526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()[test.isnull().sum() != 0].sort_values(ascending=False) *100 / test.shape[0]","metadata":{"id":"bgyasKzbLt-o","execution":{"iopub.status.busy":"2022-07-31T14:00:04.074371Z","iopub.execute_input":"2022-07-31T14:00:04.074753Z","iopub.status.idle":"2022-07-31T14:00:04.089170Z","shell.execute_reply.started":"2022-07-31T14:00:04.074720Z","shell.execute_reply":"2022-07-31T14:00:04.087922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"id":"6KR5eRXALxKx","execution":{"iopub.status.busy":"2022-07-31T14:00:04.091597Z","iopub.execute_input":"2022-07-31T14:00:04.092048Z","iopub.status.idle":"2022-07-31T14:00:04.100885Z","shell.execute_reply.started":"2022-07-31T14:00:04.092004Z","shell.execute_reply":"2022-07-31T14:00:04.099947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.dtypes","metadata":{"id":"g311kxKNMDuE","execution":{"iopub.status.busy":"2022-07-31T14:00:04.102200Z","iopub.execute_input":"2022-07-31T14:00:04.103066Z","iopub.status.idle":"2022-07-31T14:00:04.115777Z","shell.execute_reply.started":"2022-07-31T14:00:04.103029Z","shell.execute_reply":"2022-07-31T14:00:04.114676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"passenger_id = test['PassengerId'].values","metadata":{"id":"4X8tDbu_ZPbp","execution":{"iopub.status.busy":"2022-07-31T14:00:04.117893Z","iopub.execute_input":"2022-07-31T14:00:04.118510Z","iopub.status.idle":"2022-07-31T14:00:04.128486Z","shell.execute_reply.started":"2022-07-31T14:00:04.118463Z","shell.execute_reply":"2022-07-31T14:00:04.127231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# delete name and id, delete cabin\n# age is given as floats, leave it be","metadata":{"id":"Fl1MZpb1MSzD","execution":{"iopub.status.busy":"2022-07-31T14:00:04.130359Z","iopub.execute_input":"2022-07-31T14:00:04.130850Z","iopub.status.idle":"2022-07-31T14:00:04.140636Z","shell.execute_reply.started":"2022-07-31T14:00:04.130746Z","shell.execute_reply":"2022-07-31T14:00:04.139488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(columns=['PassengerId', 'Name', 'Cabin'], inplace=True)\ntest.drop(columns=['PassengerId', 'Name', 'Cabin'], inplace=True)","metadata":{"id":"6VyQMffdM1VW","execution":{"iopub.status.busy":"2022-07-31T14:00:04.142398Z","iopub.execute_input":"2022-07-31T14:00:04.142969Z","iopub.status.idle":"2022-07-31T14:00:04.155350Z","shell.execute_reply.started":"2022-07-31T14:00:04.142933Z","shell.execute_reply":"2022-07-31T14:00:04.154462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"id":"1X9fuODpNAhx","execution":{"iopub.status.busy":"2022-07-31T14:00:04.157207Z","iopub.execute_input":"2022-07-31T14:00:04.157807Z","iopub.status.idle":"2022-07-31T14:00:04.187499Z","shell.execute_reply.started":"2022-07-31T14:00:04.157771Z","shell.execute_reply":"2022-07-31T14:00:04.186614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Age'].unique()","metadata":{"id":"o5765D8RNq_o","execution":{"iopub.status.busy":"2022-07-31T14:00:04.189242Z","iopub.execute_input":"2022-07-31T14:00:04.189850Z","iopub.status.idle":"2022-07-31T14:00:04.198615Z","shell.execute_reply.started":"2022-07-31T14:00:04.189812Z","shell.execute_reply":"2022-07-31T14:00:04.197502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Age'].unique()","metadata":{"id":"EzTwf-AxOBV8","execution":{"iopub.status.busy":"2022-07-31T14:00:04.200079Z","iopub.execute_input":"2022-07-31T14:00:04.200992Z","iopub.status.idle":"2022-07-31T14:00:04.210270Z","shell.execute_reply.started":"2022-07-31T14:00:04.200950Z","shell.execute_reply":"2022-07-31T14:00:04.209124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Ticket'].unique()","metadata":{"id":"X4QC6iEnOEaT","execution":{"iopub.status.busy":"2022-07-31T14:00:04.213215Z","iopub.execute_input":"2022-07-31T14:00:04.213715Z","iopub.status.idle":"2022-07-31T14:00:04.226634Z","shell.execute_reply.started":"2022-07-31T14:00:04.213667Z","shell.execute_reply":"2022-07-31T14:00:04.224953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # dont drop ticket\n# train.drop(columns=['Ticket'], inplace=True)\n# test.drop(columns=['Ticket'], inplace=True)","metadata":{"id":"Eo6Od4liPa1N","execution":{"iopub.status.busy":"2022-07-31T14:00:04.228251Z","iopub.execute_input":"2022-07-31T14:00:04.228737Z","iopub.status.idle":"2022-07-31T14:00:04.233813Z","shell.execute_reply.started":"2022-07-31T14:00:04.228690Z","shell.execute_reply":"2022-07-31T14:00:04.232908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fill age with mean\n# train - fill embarked with mode\n# test - fill fare with mean","metadata":{"id":"Gcc8OLpfP0Nv","execution":{"iopub.status.busy":"2022-07-31T14:00:04.236001Z","iopub.execute_input":"2022-07-31T14:00:04.236392Z","iopub.status.idle":"2022-07-31T14:00:04.243447Z","shell.execute_reply.started":"2022-07-31T14:00:04.236346Z","shell.execute_reply":"2022-07-31T14:00:04.242464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Age'].fillna(train['Age'].mean(), inplace = True)\ntest['Age'].fillna(test['Age'].mean(), inplace = True)","metadata":{"id":"XPyzerf2QFbk","execution":{"iopub.status.busy":"2022-07-31T14:00:04.244876Z","iopub.execute_input":"2022-07-31T14:00:04.245456Z","iopub.status.idle":"2022-07-31T14:00:04.256130Z","shell.execute_reply.started":"2022-07-31T14:00:04.245394Z","shell.execute_reply":"2022-07-31T14:00:04.255202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Embarked'].fillna(train['Embarked'].mode()[0], inplace = True)\ntest['Fare'].fillna(test['Fare'].mean(), inplace = True)","metadata":{"id":"AuXU5FQqQdfs","execution":{"iopub.status.busy":"2022-07-31T14:00:04.258294Z","iopub.execute_input":"2022-07-31T14:00:04.258872Z","iopub.status.idle":"2022-07-31T14:00:04.268314Z","shell.execute_reply.started":"2022-07-31T14:00:04.258837Z","shell.execute_reply":"2022-07-31T14:00:04.267000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check for nulls again","metadata":{"id":"L3UX5whtQicB","execution":{"iopub.status.busy":"2022-07-31T14:00:04.269634Z","iopub.execute_input":"2022-07-31T14:00:04.270477Z","iopub.status.idle":"2022-07-31T14:00:04.276717Z","shell.execute_reply.started":"2022-07-31T14:00:04.270419Z","shell.execute_reply":"2022-07-31T14:00:04.275768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()[train.isnull().sum() != 0].sort_values(ascending=False) *100 / train.shape[0]","metadata":{"id":"pyelWFh9QtEg","execution":{"iopub.status.busy":"2022-07-31T14:00:04.298103Z","iopub.execute_input":"2022-07-31T14:00:04.299138Z","iopub.status.idle":"2022-07-31T14:00:04.311602Z","shell.execute_reply.started":"2022-07-31T14:00:04.299097Z","shell.execute_reply":"2022-07-31T14:00:04.310595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()[test.isnull().sum() != 0].sort_values(ascending=False) *100 / test.shape[0]","metadata":{"id":"NGisIZ9aQvc4","execution":{"iopub.status.busy":"2022-07-31T14:00:04.354956Z","iopub.execute_input":"2022-07-31T14:00:04.355437Z","iopub.status.idle":"2022-07-31T14:00:04.369161Z","shell.execute_reply.started":"2022-07-31T14:00:04.355385Z","shell.execute_reply":"2022-07-31T14:00:04.368237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def with_family(row):\n    if((row['SibSp'] != 0) | (row['Parch'] != 0)):\n        return 'Yes'\n    else: return 'No'\n    \n    \ntrain['With Family'] = train.apply(lambda row: with_family(row), axis=1)\ntest['With Family'] = train.apply(lambda row: with_family(row), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:04.371370Z","iopub.execute_input":"2022-07-31T14:00:04.372328Z","iopub.status.idle":"2022-07-31T14:00:04.426977Z","shell.execute_reply.started":"2022-07-31T14:00:04.372280Z","shell.execute_reply":"2022-07-31T14:00:04.426039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Survived'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:04.428688Z","iopub.execute_input":"2022-07-31T14:00:04.429215Z","iopub.status.idle":"2022-07-31T14:00:04.437235Z","shell.execute_reply.started":"2022-07-31T14:00:04.429183Z","shell.execute_reply":"2022-07-31T14:00:04.436028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Try to group families\n# Standard deviation docs - https://pandas.pydata.org/docs/reference/api/pandas.Series.std.html\n# use ddof to avoid nan std - https://stackoverflow.com/questions/32130954/pandas-standard-deviation-returns-nan\ntrain_grouped = train.groupby(['Ticket'])\nwhole_family_same_fate = train_grouped['Survived'].std(ddof=0) == 0","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:04.439131Z","iopub.execute_input":"2022-07-31T14:00:04.439552Z","iopub.status.idle":"2022-07-31T14:00:04.453579Z","shell.execute_reply.started":"2022-07-31T14:00:04.439503Z","shell.execute_reply":"2022-07-31T14:00:04.452501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 681\nwhole_family_same_fate.value_counts() * 100 / whole_family_same_fate.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:04.455887Z","iopub.execute_input":"2022-07-31T14:00:04.457023Z","iopub.status.idle":"2022-07-31T14:00:04.468892Z","shell.execute_reply.started":"2022-07-31T14:00:04.456918Z","shell.execute_reply":"2022-07-31T14:00:04.467952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# If std is zero, use any value, if std > 0, use 0.5. for other cases use 0.5\n# create a dict from grouped, use that in original\nwhole_family_survival = train_grouped['Survived'].std(ddof=0)[train_grouped['Survived'].std(ddof=0) == 0]\nwhole_family_survival_dict = whole_family_survival.to_dict()\ntrain['Family Survived'] = 0.5\ntrain['Family Survived'] = train['Ticket'].map(whole_family_survival_dict)\ntrain['Family Survived'] = train['Family Survived'].fillna(0.5)\ntrain['Family Survived'].unique()\n# whole_family_survival_dict\n# whole_family_survival_dict = pd.DataFrame[whole_family_survival_dict]\n# whole_family_survival_dict this is a pandas series","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:04.470138Z","iopub.execute_input":"2022-07-31T14:00:04.470748Z","iopub.status.idle":"2022-07-31T14:00:04.502536Z","shell.execute_reply.started":"2022-07-31T14:00:04.470714Z","shell.execute_reply":"2022-07-31T14:00:04.501268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Family Survived'] = test['Ticket'].map(whole_family_survival_dict)\ntest['Family Survived'] = test['Family Survived'].fillna(0.5)\ntest['Family Survived'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:04.504357Z","iopub.execute_input":"2022-07-31T14:00:04.504753Z","iopub.status.idle":"2022-07-31T14:00:04.520975Z","shell.execute_reply.started":"2022-07-31T14:00:04.504719Z","shell.execute_reply":"2022-07-31T14:00:04.519709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get y value, encode","metadata":{"id":"YHiwreVcQ33Z","execution":{"iopub.status.busy":"2022-07-31T14:00:04.523107Z","iopub.execute_input":"2022-07-31T14:00:04.523613Z","iopub.status.idle":"2022-07-31T14:00:04.528901Z","shell.execute_reply.started":"2022-07-31T14:00:04.523563Z","shell.execute_reply":"2022-07-31T14:00:04.527905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train['Survived'].values\ny_train","metadata":{"id":"ZH3vj8RFRCnW","execution":{"iopub.status.busy":"2022-07-31T14:00:04.532377Z","iopub.execute_input":"2022-07-31T14:00:04.533086Z","iopub.status.idle":"2022-07-31T14:00:04.547576Z","shell.execute_reply.started":"2022-07-31T14:00:04.533045Z","shell.execute_reply":"2022-07-31T14:00:04.546350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(columns=['Survived'], inplace=True)","metadata":{"id":"7HsN6HzMRmlQ","execution":{"iopub.status.busy":"2022-07-31T14:00:04.550898Z","iopub.execute_input":"2022-07-31T14:00:04.551945Z","iopub.status.idle":"2022-07-31T14:00:04.559740Z","shell.execute_reply.started":"2022-07-31T14:00:04.551896Z","shell.execute_reply":"2022-07-31T14:00:04.558571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find unique categorical values\ntrain['Sex'].unique()","metadata":{"id":"6gLSOwCQRuHu","execution":{"iopub.status.busy":"2022-07-31T14:00:04.562623Z","iopub.execute_input":"2022-07-31T14:00:04.563028Z","iopub.status.idle":"2022-07-31T14:00:04.571845Z","shell.execute_reply.started":"2022-07-31T14:00:04.562990Z","shell.execute_reply":"2022-07-31T14:00:04.570963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Sex'].unique()","metadata":{"id":"S3-d4dF7R4c1","execution":{"iopub.status.busy":"2022-07-31T14:00:04.573376Z","iopub.execute_input":"2022-07-31T14:00:04.573778Z","iopub.status.idle":"2022-07-31T14:00:04.587938Z","shell.execute_reply.started":"2022-07-31T14:00:04.573742Z","shell.execute_reply":"2022-07-31T14:00:04.586630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Embarked'].unique()","metadata":{"id":"dFVP5LiLR6n8","execution":{"iopub.status.busy":"2022-07-31T14:00:04.589223Z","iopub.execute_input":"2022-07-31T14:00:04.590169Z","iopub.status.idle":"2022-07-31T14:00:04.599899Z","shell.execute_reply.started":"2022-07-31T14:00:04.590120Z","shell.execute_reply":"2022-07-31T14:00:04.598944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Embarked'].unique()","metadata":{"id":"KMjL211qSDEn","execution":{"iopub.status.busy":"2022-07-31T14:00:04.603056Z","iopub.execute_input":"2022-07-31T14:00:04.603489Z","iopub.status.idle":"2022-07-31T14:00:04.611913Z","shell.execute_reply.started":"2022-07-31T14:00:04.603422Z","shell.execute_reply":"2022-07-31T14:00:04.610821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot frequency distribution - visual check if any value is more than 85%\nimport matplotlib.pyplot as plt\ncount = 0\nfig, ax = plt.subplots(3, 4, figsize = (18, 20))\nfor i in range(train.shape[1]):\n    plt.subplot(3, 4, count + 1)\n    plt.hist(train.iloc[:, i].dropna(axis = 0), rwidth = 0.9, color = 'green')\n    plt.xlabel(train.columns[i], fontsize = 15)\n    count += 1\nplt.tight_layout()\nplt.show()","metadata":{"id":"7ytRexwIR8-J","execution":{"iopub.status.busy":"2022-07-31T14:00:04.613698Z","iopub.execute_input":"2022-07-31T14:00:04.614473Z","iopub.status.idle":"2022-07-31T14:00:13.748506Z","shell.execute_reply.started":"2022-07-31T14:00:04.614406Z","shell.execute_reply":"2022-07-31T14:00:13.747541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot frequency distribution\nimport matplotlib.pyplot as plt\ncount = 0\nfig, ax = plt.subplots(3, 4, figsize = (18, 20))\nfor i in range(test.shape[1]):\n    plt.subplot(3, 4, count + 1)\n    plt.hist(test.iloc[:, i].dropna(axis = 0), rwidth = 0.9, color = 'green')\n    plt.xlabel(test.columns[i], fontsize = 15)\n    count += 1\nplt.tight_layout()\nplt.show()","metadata":{"id":"WWzyJDBASyjl","execution":{"iopub.status.busy":"2022-07-31T14:00:13.750048Z","iopub.execute_input":"2022-07-31T14:00:13.750692Z","iopub.status.idle":"2022-07-31T14:00:19.471924Z","shell.execute_reply.started":"2022-07-31T14:00:13.750652Z","shell.execute_reply":"2022-07-31T14:00:19.470730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Family Survived'] = train['Family Survived'].astype('float64')\ntest['Family Survived'] = test['Family Survived'].astype('float64')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.473408Z","iopub.execute_input":"2022-07-31T14:00:19.473928Z","iopub.status.idle":"2022-07-31T14:00:19.481358Z","shell.execute_reply.started":"2022-07-31T14:00:19.473876Z","shell.execute_reply":"2022-07-31T14:00:19.479940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping with family column too\ntrain.drop(columns=['Ticket', 'With Family'], inplace=True)\ntest.drop(columns=['Ticket', 'With Family'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.482863Z","iopub.execute_input":"2022-07-31T14:00:19.483223Z","iopub.status.idle":"2022-07-31T14:00:19.502061Z","shell.execute_reply.started":"2022-07-31T14:00:19.483190Z","shell.execute_reply":"2022-07-31T14:00:19.500974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Grouping age\nfrom sklearn.preprocessing import LabelEncoder\n\ncombined = pd.concat([train, test], axis=0)\n\ncombined['AgeBin'] = pd.qcut(combined['Age'], 4)\n\nlabel = LabelEncoder()\ncombined['AgeBin_Code'] = label.fit_transform(combined['AgeBin'])\n\ntrain['AgeBin_Code'] = combined['AgeBin_Code'][:891]\ntest['AgeBin_Code'] = combined['AgeBin_Code'][891:]\n\ntrain.drop(['Age'], 1, inplace=True)\ntest.drop(['Age'], 1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.503285Z","iopub.execute_input":"2022-07-31T14:00:19.504692Z","iopub.status.idle":"2022-07-31T14:00:19.535737Z","shell.execute_reply.started":"2022-07-31T14:00:19.504643Z","shell.execute_reply":"2022-07-31T14:00:19.534492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Survived'] = y_train\ntrain_grouped = train.groupby(by = train['AgeBin_Code'])\ntrain_grouped.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.541180Z","iopub.execute_input":"2022-07-31T14:00:19.541565Z","iopub.status.idle":"2022-07-31T14:00:19.561517Z","shell.execute_reply.started":"2022-07-31T14:00:19.541529Z","shell.execute_reply":"2022-07-31T14:00:19.560350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(columns=['Survived'], inplace=True)\ntest_grouped = train.groupby(by = test['AgeBin_Code'])\ntest_grouped.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.562912Z","iopub.execute_input":"2022-07-31T14:00:19.563280Z","iopub.status.idle":"2022-07-31T14:00:19.584748Z","shell.execute_reply.started":"2022-07-31T14:00:19.563245Z","shell.execute_reply":"2022-07-31T14:00:19.583808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Grouping fare\n# train['Fare'].describe()\ntest['Fare'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.585913Z","iopub.execute_input":"2022-07-31T14:00:19.587015Z","iopub.status.idle":"2022-07-31T14:00:19.602313Z","shell.execute_reply.started":"2022-07-31T14:00:19.586964Z","shell.execute_reply":"2022-07-31T14:00:19.601214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined['FareBin'] = pd.qcut(combined['Fare'], 4)\n\nlabel = LabelEncoder()\ncombined['FareBin_Code'] = label.fit_transform(combined['FareBin'])\n\ntrain['FareBin_Code'] = combined['FareBin_Code'][:891]\ntest['FareBin_Code'] = combined['FareBin_Code'][891:]\n\ntrain.drop(['Fare'], 1, inplace=True)\ntest.drop(['Fare'], 1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.603847Z","iopub.execute_input":"2022-07-31T14:00:19.604519Z","iopub.status.idle":"2022-07-31T14:00:19.627279Z","shell.execute_reply.started":"2022-07-31T14:00:19.604477Z","shell.execute_reply":"2022-07-31T14:00:19.626408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Survived'] = y_train\ntrain_grouped = train.groupby(by = train['FareBin_Code'])\ntrain_grouped.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.628757Z","iopub.execute_input":"2022-07-31T14:00:19.629351Z","iopub.status.idle":"2022-07-31T14:00:19.647832Z","shell.execute_reply.started":"2022-07-31T14:00:19.629314Z","shell.execute_reply":"2022-07-31T14:00:19.646488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(columns=['Survived'], inplace=True)\ntest_grouped = train.groupby(by = test['AgeBin_Code'])\ntest_grouped.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.649307Z","iopub.execute_input":"2022-07-31T14:00:19.650476Z","iopub.status.idle":"2022-07-31T14:00:19.674640Z","shell.execute_reply.started":"2022-07-31T14:00:19.650408Z","shell.execute_reply":"2022-07-31T14:00:19.673330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode\ntrain_categorical = train.select_dtypes('object')\ntest_categorical = test.select_dtypes('object')\n\ndef categorical_to_dummy(data):\n    for column in categorical_columns:\n        dummy_columns = pd.get_dummies(data[column], prefix=i, drop_first=True)\n        for x in dummy_columns.columns:\n            data[x] = dummy_columns[x]\n    \n    data.drop(columns=categorical_columns, inplace=True)    \n    return data ","metadata":{"id":"0AxA0GnqTilo","execution":{"iopub.status.busy":"2022-07-31T14:00:19.676219Z","iopub.execute_input":"2022-07-31T14:00:19.676618Z","iopub.status.idle":"2022-07-31T14:00:19.688128Z","shell.execute_reply.started":"2022-07-31T14:00:19.676581Z","shell.execute_reply":"2022-07-31T14:00:19.686849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_columns = train_categorical\ncategorical_to_dummy(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.689754Z","iopub.execute_input":"2022-07-31T14:00:19.691026Z","iopub.status.idle":"2022-07-31T14:00:19.722742Z","shell.execute_reply.started":"2022-07-31T14:00:19.690984Z","shell.execute_reply":"2022-07-31T14:00:19.721505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_columns = test_categorical\ncategorical_to_dummy(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.724052Z","iopub.execute_input":"2022-07-31T14:00:19.725222Z","iopub.status.idle":"2022-07-31T14:00:19.750471Z","shell.execute_reply.started":"2022-07-31T14:00:19.725179Z","shell.execute_reply":"2022-07-31T14:00:19.749120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adding a scaler doesnt change accuracy for XGB\nfrom sklearn.preprocessing import StandardScaler\nstandard_scaler = StandardScaler()\ntrain = standard_scaler.fit_transform(train)\ntest = standard_scaler.transform(test)","metadata":{"id":"hdJFaTEWVSm6","execution":{"iopub.status.busy":"2022-07-31T14:00:19.752419Z","iopub.execute_input":"2022-07-31T14:00:19.753382Z","iopub.status.idle":"2022-07-31T14:00:19.767896Z","shell.execute_reply.started":"2022-07-31T14:00:19.753317Z","shell.execute_reply":"2022-07-31T14:00:19.766707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.769552Z","iopub.execute_input":"2022-07-31T14:00:19.770257Z","iopub.status.idle":"2022-07-31T14:00:19.777743Z","shell.execute_reply.started":"2022-07-31T14:00:19.770210Z","shell.execute_reply":"2022-07-31T14:00:19.776446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ANN\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:19.780738Z","iopub.execute_input":"2022-07-31T14:00:19.781148Z","iopub.status.idle":"2022-07-31T14:00:19.788302Z","shell.execute_reply.started":"2022-07-31T14:00:19.781100Z","shell.execute_reply":"2022-07-31T14:00:19.787066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = Sequential()\nclassifier.add(Dense(units = 16, kernel_initializer = 'uniform', activation = 'relu', input_dim = 9))\n# classifier.add(Dense(units = 12, kernel_initializer = 'uniform', activation = 'relu'))\nclassifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu'))\n# classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))\nclassifier.add(Dense(units = 5, kernel_initializer = 'uniform', activation = 'relu'))\n# classifier.add(Dense(units = 16, kernel_initializer = 'uniform', activation = 'relu'))\nclassifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:08:56.201585Z","iopub.execute_input":"2022-07-31T14:08:56.201989Z","iopub.status.idle":"2022-07-31T14:08:56.246958Z","shell.execute_reply.started":"2022-07-31T14:08:56.201956Z","shell.execute_reply":"2022-07-31T14:08:56.245765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:08:57.737166Z","iopub.execute_input":"2022-07-31T14:08:57.737629Z","iopub.status.idle":"2022-07-31T14:08:57.749287Z","shell.execute_reply.started":"2022-07-31T14:08:57.737593Z","shell.execute_reply":"2022-07-31T14:08:57.748113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier.fit(train, y_train, batch_size = 10, epochs = 200)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:08:59.519449Z","iopub.execute_input":"2022-07-31T14:08:59.520396Z","iopub.status.idle":"2022-07-31T14:09:29.477906Z","shell.execute_reply.started":"2022-07-31T14:08:59.520356Z","shell.execute_reply":"2022-07-31T14:09:29.476869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(test)\n# y_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:51.462539Z","iopub.execute_input":"2022-07-31T14:00:51.463440Z","iopub.status.idle":"2022-07-31T14:00:51.588931Z","shell.execute_reply.started":"2022-07-31T14:00:51.463389Z","shell.execute_reply":"2022-07-31T14:00:51.587961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.where(y_pred > 0.5, 1, 0)\ny_pred = y_pred.flatten()\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:51.590413Z","iopub.execute_input":"2022-07-31T14:00:51.590968Z","iopub.status.idle":"2022-07-31T14:00:51.599887Z","shell.execute_reply.started":"2022-07-31T14:00:51.590934Z","shell.execute_reply":"2022-07-31T14:00:51.598747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': passenger_id, 'Survived': y_pred})\noutput.to_csv('submission11.csv', index=False)\nprint(\"Submitted successfully!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-31T14:00:51.608184Z","iopub.execute_input":"2022-07-31T14:00:51.608901Z","iopub.status.idle":"2022-07-31T14:00:51.618551Z","shell.execute_reply.started":"2022-07-31T14:00:51.608839Z","shell.execute_reply":"2022-07-31T14:00:51.617584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}