{"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":" <div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     TITANIC DATASET ANALYSIS AND CLASSIFICATION\n</div>","metadata":{}},{"cell_type":"markdown","source":"<center><img src=\"https://cdn.suustunde.com//content/files/2021/2/M_195783747_NWS-j3gMsDDzJhHCVM7TPcJC.jpg\"></center>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Introduction\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:Beige;\n           font-size:110%;\n           letter-spacing:0.5px\">\n\n<p style=\"padding: 10px;\n              color:black;\">\nThe sinking of the Titanic is one of the most infamous shipwrecks in history.\n\nOn April 15, 1912, during her maiden voyage, the widely considered “unsinkable” RMS Titanic sank after colliding with an iceberg. Unfortunately, there weren’t enough lifeboats for everyone onboard, resulting in the death of 1502 out of 2224 passengers and crew.\n\nWhile there was some element of luck involved in surviving, it seems some groups of people were more likely to survive than others.\n\nIn this challenge, we ask you to build a predictive model that answers the question: “what sorts of people were more likely to survive?” using passenger data (ie name, age, gender, socio-economic class, etc).\n</p>\n</div> ","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Contents\n</div>","metadata":{}},{"cell_type":"markdown","source":"1. [Packages](#t1.)\n\n2. [Dataset](#t2.)\n\n3. [Data Cleaning](#t3.)\n\n4. [Data Visualization](#t4.)\n\n5. [Data Preprocessing](#t5.)\n\n6. [Classification](#t6.)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"t1.\"></a>\n<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Packages\n</div>","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport missingno\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nimport holoviews as hv","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-07T13:17:01.162947Z","iopub.execute_input":"2022-07-07T13:17:01.163399Z","iopub.status.idle":"2022-07-07T13:17:02.932232Z","shell.execute_reply.started":"2022-07-07T13:17:01.163367Z","shell.execute_reply":"2022-07-07T13:17:02.930726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.impute import KNNImputer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, plot_confusion_matrix, plot_roc_curve\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.171526Z","iopub.execute_input":"2022-07-07T13:14:02.172027Z","iopub.status.idle":"2022-07-07T13:14:02.554294Z","shell.execute_reply.started":"2022-07-07T13:14:02.171979Z","shell.execute_reply":"2022-07-07T13:14:02.553080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings(\"ignore\")\nsns.set_style(\"whitegrid\")\nplt.style.use('bmh')\nplt.rcParams['figure.dpi']=100","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.556088Z","iopub.execute_input":"2022-07-07T13:14:02.557342Z","iopub.status.idle":"2022-07-07T13:14:02.565949Z","shell.execute_reply.started":"2022-07-07T13:14:02.557284Z","shell.execute_reply":"2022-07-07T13:14:02.564190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"t2.\"></a>\n<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Dataset\n</div>","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.569727Z","iopub.execute_input":"2022-07-07T13:14:02.571061Z","iopub.status.idle":"2022-07-07T13:14:02.581936Z","shell.execute_reply.started":"2022-07-07T13:14:02.570975Z","shell.execute_reply":"2022-07-07T13:14:02.580291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/titanic/train.csv')\ntest = pd.read_csv('/kaggle/input/titanic/test.csv')\ngender_submission = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.583842Z","iopub.execute_input":"2022-07-07T13:14:02.585311Z","iopub.status.idle":"2022-07-07T13:14:02.624268Z","shell.execute_reply.started":"2022-07-07T13:14:02.585250Z","shell.execute_reply":"2022-07-07T13:14:02.622824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.626615Z","iopub.execute_input":"2022-07-07T13:14:02.627428Z","iopub.status.idle":"2022-07-07T13:14:02.657860Z","shell.execute_reply.started":"2022-07-07T13:14:02.627379Z","shell.execute_reply":"2022-07-07T13:14:02.656548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.660308Z","iopub.execute_input":"2022-07-07T13:14:02.660819Z","iopub.status.idle":"2022-07-07T13:14:02.683689Z","shell.execute_reply.started":"2022-07-07T13:14:02.660771Z","shell.execute_reply":"2022-07-07T13:14:02.682272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.685413Z","iopub.execute_input":"2022-07-07T13:14:02.687084Z","iopub.status.idle":"2022-07-07T13:14:02.700208Z","shell.execute_reply.started":"2022-07-07T13:14:02.687009Z","shell.execute_reply":"2022-07-07T13:14:02.698858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"t3.\"></a>\n<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Data Cleaning\n</div>","metadata":{}},{"cell_type":"code","source":"missingno.matrix(train)\nplt.title(\"Train\", fontsize=50)\nplt.show()\n\nmissingno.matrix(test)\nplt.title(\"Test\", fontsize=50)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:02.701837Z","iopub.execute_input":"2022-07-07T13:14:02.702775Z","iopub.status.idle":"2022-07-07T13:14:04.242524Z","shell.execute_reply.started":"2022-07-07T13:14:02.702731Z","shell.execute_reply":"2022-07-07T13:14:04.241283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(\"Cabin\",axis=1)\ntest = test.drop(\"Cabin\",axis=1)\n\ndf = pd.concat([train,test],axis=0).reset_index(drop=True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:04.247544Z","iopub.execute_input":"2022-07-07T13:14:04.248766Z","iopub.status.idle":"2022-07-07T13:14:04.289156Z","shell.execute_reply.started":"2022-07-07T13:14:04.248721Z","shell.execute_reply":"2022-07-07T13:14:04.287602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Imputer(entry, n_neighbors=2):\n    imputer = KNNImputer(n_neighbors=n_neighbors)\n    return pd.DataFrame(imputer.fit_transform(entry),columns=entry.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:04.291214Z","iopub.execute_input":"2022-07-07T13:14:04.291709Z","iopub.status.idle":"2022-07-07T13:14:04.299674Z","shell.execute_reply.started":"2022-07-07T13:14:04.291656Z","shell.execute_reply":"2022-07-07T13:14:04.298110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"entry = df[[\"Pclass\",\"Age\",\"SibSp\",\"Parch\",\"Fare\",]]\nentry = Imputer(entry, n_neighbors=3)\nentry.insert(5,\"Embarked\",df[\"Embarked\"].replace([\"S\",\"C\",\"Q\"],[0,1,2]))\nentry = Imputer(entry, n_neighbors=1)\nentry[\"Embarked\"] = entry[\"Embarked\"].replace([0,1,2],[\"S\",\"C\",\"Q\"])\nentry.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:04.301783Z","iopub.execute_input":"2022-07-07T13:14:04.303003Z","iopub.status.idle":"2022-07-07T13:14:04.465665Z","shell.execute_reply.started":"2022-07-07T13:14:04.302958Z","shell.execute_reply":"2022-07-07T13:14:04.464252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"Age\"] = entry[\"Age\"]\ndf[\"Fare\"] = entry[\"Fare\"]\ndf[\"Embarked\"] = entry[\"Embarked\"]\ndf[\"Title\"] = df[\"Name\"].str.split(\".\").str[0].str.split(\",\").str[1]\ncolumns = [\"Title\",\"Pclass\", \"Sex\", \"Age\", \"SibSp\", \"Parch\", \"Fare\", \"Embarked\",\"Survived\"]\n\ndataset = df[columns]\ndataset = dataset.drop(\"Sex\",axis=1)\ndataset[\"Sex\"] = df[\"Sex\"].str.title().tolist()\ndataset.insert(4,\"AgeRange\",pd.cut(dataset[\"Age\"], [0,18, 35, 50, 120], labels=[\"0-18\", \"18-35\", \"35-50\",\"65+\"]))\ndataset.insert(8,\"FareRange\",pd.cut(dataset[\"Fare\"],bins=4, labels=[\"0-130$\",\"130-255$\",\"255-385$\",\"385+$\"]))","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:04.467574Z","iopub.execute_input":"2022-07-07T13:14:04.468013Z","iopub.status.idle":"2022-07-07T13:14:04.499501Z","shell.execute_reply.started":"2022-07-07T13:14:04.467971Z","shell.execute_reply":"2022-07-07T13:14:04.498556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:04.500943Z","iopub.execute_input":"2022-07-07T13:14:04.501686Z","iopub.status.idle":"2022-07-07T13:14:04.521697Z","shell.execute_reply.started":"2022-07-07T13:14:04.501630Z","shell.execute_reply":"2022-07-07T13:14:04.520551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missingno.matrix(dataset.drop(\"Survived\",axis=1))\nplt.title(\"Dataset\", fontsize=50)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:04.522938Z","iopub.execute_input":"2022-07-07T13:14:04.523383Z","iopub.status.idle":"2022-07-07T13:14:05.096598Z","shell.execute_reply.started":"2022-07-07T13:14:04.523351Z","shell.execute_reply":"2022-07-07T13:14:05.095116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"t4.\"></a>\n<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Data Visualization\n</div>","metadata":{}},{"cell_type":"code","source":"def probability_plot(columns):\n    global dataset\n    dataset.groupby(columns)[\"Survived\"].mean().plot(kind=\"barh\",label=\"Survived Probability\",figsize=(15,4))\n    plt.vlines(x=dataset[\"Survived\"].mean(),ymin=-1, ymax=12, color=\"red\",\n               label=\"Average of Survived Probability\",ls=\"--\")\n    plt.legend(bbox_to_anchor=(0.65,1.15),ncol=2, fancybox=True, shadow=True)\n    plt.xlabel(\"PROBABILITY\")\n    plt.show()\n    \ndef func(pct, allvalues):\n    absolute = int(pct / 100.*np.sum(allvalues))\n    return \"{:.1f}%\\n({:d})\".format(pct, absolute)\n\ndef pie_chart(df,column,title=\"\"):\n    \n    label = df[column].value_counts().index \n    value = df[column].value_counts().values\n    wp = { 'linewidth' : 1, 'edgecolor' : \"white\" }\n    fig, ax = plt.subplots(figsize =(10, 7))\n    wedges, texts, autotexts = ax.pie(value,\n                                  autopct = lambda pct: func(pct, value),\n                                  labels = label,\n                                  shadow = True,\n                                  startangle = 90,\n                                  wedgeprops = wp,\n                                  textprops = dict(color =\"k\",fontsize=14))\n    ax.legend(wedges, label,\n          title =column,\n          loc =\"center left\",\n          bbox_to_anchor =(1, 0, 0.8, 1.6))\n    plt.setp(autotexts, size = 8, weight =\"bold\")\n    ax.set_title(title,fontsize=20)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:05.098414Z","iopub.execute_input":"2022-07-07T13:14:05.098865Z","iopub.status.idle":"2022-07-07T13:14:05.114988Z","shell.execute_reply.started":"2022-07-07T13:14:05.098825Z","shell.execute_reply":"2022-07-07T13:14:05.114171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_chart(dataset,\"Sex\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:05.116154Z","iopub.execute_input":"2022-07-07T13:14:05.117105Z","iopub.status.idle":"2022-07-07T13:14:05.361000Z","shell.execute_reply.started":"2022-07-07T13:14:05.117065Z","shell.execute_reply":"2022-07-07T13:14:05.359872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_chart(dataset,\"Embarked\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:05.365927Z","iopub.execute_input":"2022-07-07T13:14:05.367122Z","iopub.status.idle":"2022-07-07T13:14:05.573592Z","shell.execute_reply.started":"2022-07-07T13:14:05.367073Z","shell.execute_reply":"2022-07-07T13:14:05.572211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_chart(dataset,\"AgeRange\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:05.575520Z","iopub.execute_input":"2022-07-07T13:14:05.576451Z","iopub.status.idle":"2022-07-07T13:14:05.815590Z","shell.execute_reply.started":"2022-07-07T13:14:05.576400Z","shell.execute_reply":"2022-07-07T13:14:05.814276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_chart(dataset,\"Survived\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:05.818559Z","iopub.execute_input":"2022-07-07T13:14:05.818991Z","iopub.status.idle":"2022-07-07T13:14:06.008372Z","shell.execute_reply.started":"2022-07-07T13:14:05.818955Z","shell.execute_reply":"2022-07-07T13:14:06.005341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,4))\nsns.stripplot(x=\"Fare\", y=\"Embarked\", hue=\"Survived\",data=dataset, dodge=True, alpha=.25, zorder=1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:06.011638Z","iopub.execute_input":"2022-07-07T13:14:06.013778Z","iopub.status.idle":"2022-07-07T13:14:06.421361Z","shell.execute_reply.started":"2022-07-07T13:14:06.013698Z","shell.execute_reply":"2022-07-07T13:14:06.420432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,4))\nsns.scatterplot(x=\"Age\", y=\"Fare\", hue=\"Survived\", size=\"Pclass\", palette=\"muted\", data=dataset, alpha=.5,sizes=(50,200))\nplt.yscale(\"log\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:06.422504Z","iopub.execute_input":"2022-07-07T13:14:06.423875Z","iopub.status.idle":"2022-07-07T13:14:07.486832Z","shell.execute_reply.started":"2022-07-07T13:14:06.423839Z","shell.execute_reply":"2022-07-07T13:14:07.485264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"table = dataset.groupby([\"Title\"]).agg({\"Survived\":\"mean\",\"Title\":\"count\"}).rename(columns={\"Title\":\"Count\"})\ntable.sort_values(\"Count\",ascending=False).style.background_gradient(cmap='viridis',subset=[\"Survived\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.488972Z","iopub.execute_input":"2022-07-07T13:14:07.489340Z","iopub.status.idle":"2022-07-07T13:14:07.577930Z","shell.execute_reply.started":"2022-07-07T13:14:07.489309Z","shell.execute_reply":"2022-07-07T13:14:07.576580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.pivot_table(index=\"Sex\",columns=\"Embarked\",values=\"Fare\",aggfunc=\"mean\").style.background_gradient(cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.579679Z","iopub.execute_input":"2022-07-07T13:14:07.580682Z","iopub.status.idle":"2022-07-07T13:14:07.618223Z","shell.execute_reply.started":"2022-07-07T13:14:07.580645Z","shell.execute_reply":"2022-07-07T13:14:07.617209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.pivot_table(index=\"Sex\",columns=\"Embarked\",values=\"Age\",aggfunc=\"mean\").style.background_gradient(cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.619628Z","iopub.execute_input":"2022-07-07T13:14:07.620945Z","iopub.status.idle":"2022-07-07T13:14:07.648340Z","shell.execute_reply.started":"2022-07-07T13:14:07.620906Z","shell.execute_reply":"2022-07-07T13:14:07.647457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.pivot_table(index=\"Title\",columns=\"Embarked\",values=\"Fare\",aggfunc=\"mean\").style.background_gradient(cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.649625Z","iopub.execute_input":"2022-07-07T13:14:07.650530Z","iopub.status.idle":"2022-07-07T13:14:07.685049Z","shell.execute_reply.started":"2022-07-07T13:14:07.650494Z","shell.execute_reply":"2022-07-07T13:14:07.683610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.pivot_table(index=\"Title\",columns=\"Embarked\",values=\"Age\",aggfunc=\"mean\").style.background_gradient(cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.687298Z","iopub.execute_input":"2022-07-07T13:14:07.687708Z","iopub.status.idle":"2022-07-07T13:14:07.721561Z","shell.execute_reply.started":"2022-07-07T13:14:07.687676Z","shell.execute_reply":"2022-07-07T13:14:07.720200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.pivot_table(index=\"Pclass\",columns=\"Embarked\",values=\"Fare\",aggfunc=\"mean\").style.background_gradient(cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.727843Z","iopub.execute_input":"2022-07-07T13:14:07.728265Z","iopub.status.idle":"2022-07-07T13:14:07.761009Z","shell.execute_reply.started":"2022-07-07T13:14:07.728235Z","shell.execute_reply":"2022-07-07T13:14:07.759451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.pivot_table(index=\"Pclass\",columns=\"Embarked\",values=\"Age\",aggfunc=\"mean\").style.background_gradient(cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.762786Z","iopub.execute_input":"2022-07-07T13:14:07.763897Z","iopub.status.idle":"2022-07-07T13:14:07.793550Z","shell.execute_reply.started":"2022-07-07T13:14:07.763862Z","shell.execute_reply":"2022-07-07T13:14:07.792520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_groupby = dataset.groupby([\"Sex\",\"Pclass\"])[\"Survived\"].mean().reset_index()\ndataset_groupby[\"Pclass\"] = dataset_groupby[\"Pclass\"].astype(str)\nsankey1 = hv.Sankey(dataset_groupby, kdims=[\"Sex\",\"Pclass\"], vdims=[\"Survived\"])\nhv.extension(\"bokeh\", \"matplotlib\")\nsankey1.opts(cmap='Colorblind',label_position='left',\n                                 edge_color='Pclass',\n                                 width=800, height=400, bgcolor=\"snow\",\n                                 title=\"Probability of Survival by Sex and Pclass\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:21:02.464607Z","iopub.execute_input":"2022-07-07T13:21:02.465021Z","iopub.status.idle":"2022-07-07T13:21:02.971714Z","shell.execute_reply.started":"2022-07-07T13:21:02.464987Z","shell.execute_reply":"2022-07-07T13:21:02.970618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_groupby = dataset.groupby([\"AgeRange\",\"Embarked\"])[\"Survived\"].mean().reset_index()\nsankey1 = hv.Sankey(dataset_groupby, kdims=[\"AgeRange\",\"Embarked\"], vdims=[\"Survived\"])\nhv.extension(\"bokeh\", \"matplotlib\")\nsankey1.opts(cmap='Colorblind',label_position='left',\n                                 edge_color='Embarked',\n                                 width=800, height=400, bgcolor=\"snow\",\n                                 title=\"Probability of Survival by Age and Embarked\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:21:48.594375Z","iopub.execute_input":"2022-07-07T13:21:48.595347Z","iopub.status.idle":"2022-07-07T13:21:49.117977Z","shell.execute_reply.started":"2022-07-07T13:21:48.595305Z","shell.execute_reply":"2022-07-07T13:21:49.117231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_groupby = dataset.groupby([\"Embarked\",\"AgeRange\"])[\"Fare\"].mean().reset_index()\nsankey1 = hv.Sankey(dataset_groupby, kdims=[\"Embarked\",\"AgeRange\"], vdims=[\"Fare\"])\nhv.extension(\"bokeh\", \"matplotlib\")\nsankey1.opts(cmap='Colorblind',label_position='left',\n                                 edge_color='AgeRange',\n                                 width=800, height=400, bgcolor=\"snow\",\n                                 title=\"Average of Fare by Embarked and AgeRange\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:23:42.889741Z","iopub.execute_input":"2022-07-07T13:23:42.890169Z","iopub.status.idle":"2022-07-07T13:23:43.435769Z","shell.execute_reply.started":"2022-07-07T13:23:42.890139Z","shell.execute_reply":"2022-07-07T13:23:43.434420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_plot([\"Sex\",\"Embarked\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:07.794717Z","iopub.execute_input":"2022-07-07T13:14:07.795719Z","iopub.status.idle":"2022-07-07T13:14:08.112176Z","shell.execute_reply.started":"2022-07-07T13:14:07.795680Z","shell.execute_reply":"2022-07-07T13:14:08.111104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_plot([\"Sex\",\"AgeRange\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:08.113672Z","iopub.execute_input":"2022-07-07T13:14:08.114048Z","iopub.status.idle":"2022-07-07T13:14:08.435607Z","shell.execute_reply.started":"2022-07-07T13:14:08.113997Z","shell.execute_reply":"2022-07-07T13:14:08.434110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_plot([\"Sex\",\"FareRange\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:08.437699Z","iopub.execute_input":"2022-07-07T13:14:08.438226Z","iopub.status.idle":"2022-07-07T13:14:08.779477Z","shell.execute_reply.started":"2022-07-07T13:14:08.438179Z","shell.execute_reply":"2022-07-07T13:14:08.778023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_plot([\"Sex\",\"Pclass\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:08.781704Z","iopub.execute_input":"2022-07-07T13:14:08.782240Z","iopub.status.idle":"2022-07-07T13:14:09.101368Z","shell.execute_reply.started":"2022-07-07T13:14:08.782163Z","shell.execute_reply":"2022-07-07T13:14:09.099817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"t5.\"></a>\n<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Data Preprocessing\n</div>","metadata":{}},{"cell_type":"code","source":"title = pd.get_dummies(df[columns][\"Title\"])\nembarked = pd.get_dummies(df[columns][\"Embarked\"])\ndata = pd.concat([title,embarked,df[columns].drop([\"Title\",\"Embarked\"],axis=1)],axis=1).replace([\"male\",\"female\"],[0,1])\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:09.103532Z","iopub.execute_input":"2022-07-07T13:14:09.104977Z","iopub.status.idle":"2022-07-07T13:14:09.146129Z","shell.execute_reply.started":"2022-07-07T13:14:09.104923Z","shell.execute_reply":"2022-07-07T13:14:09.145144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = data[:-418]\ntest = data[-418:]","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:09.147289Z","iopub.execute_input":"2022-07-07T13:14:09.147639Z","iopub.status.idle":"2022-07-07T13:14:09.154157Z","shell.execute_reply.started":"2022-07-07T13:14:09.147608Z","shell.execute_reply":"2022-07-07T13:14:09.152606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nscaler.fit(train.drop(\"Survived\",axis=1))\nX = scaler.transform(train.drop(\"Survived\",axis=1))\ny = train[\"Survived\"]\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=38)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:09.155806Z","iopub.execute_input":"2022-07-07T13:14:09.156238Z","iopub.status.idle":"2022-07-07T13:14:09.176754Z","shell.execute_reply.started":"2022-07-07T13:14:09.156137Z","shell.execute_reply":"2022-07-07T13:14:09.175656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"t6.\"></a>\n<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:LightSlateGray;\n           font-size:150%;\n           text-align:center;\n           letter-spacing:0.5px\">\n     Classification\n</div>","metadata":{}},{"cell_type":"code","source":"net = MLPClassifier()\nnet.hidden_layer_sizes=(60,120,60)\nnet.activation='relu' # 'identity', 'logistic', 'tanh', 'relu'\nnet.solver='adam' # 'lbfgs', 'sgd', 'adam'\nnet.max_iter=150\nnet.verbose = True\nnet.batch_size = 32\nnet.learning_rate = 'invscaling' # 'constant', 'invscaling', 'adaptive'","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:09.180146Z","iopub.execute_input":"2022-07-07T13:14:09.180518Z","iopub.status.idle":"2022-07-07T13:14:09.186495Z","shell.execute_reply.started":"2022-07-07T13:14:09.180487Z","shell.execute_reply":"2022-07-07T13:14:09.185616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:09.187711Z","iopub.execute_input":"2022-07-07T13:14:09.188097Z","iopub.status.idle":"2022-07-07T13:14:12.147851Z","shell.execute_reply.started":"2022-07-07T13:14:09.188056Z","shell.execute_reply":"2022-07-07T13:14:12.146607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc_curve(net, X_test, y_test)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:12.149310Z","iopub.execute_input":"2022-07-07T13:14:12.149645Z","iopub.status.idle":"2022-07-07T13:14:12.435847Z","shell.execute_reply.started":"2022-07-07T13:14:12.149616Z","shell.execute_reply":"2022-07-07T13:14:12.434705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(net, X_test, y_test,cmap='Blues')\nplt.grid(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:12.437289Z","iopub.execute_input":"2022-07-07T13:14:12.437609Z","iopub.status.idle":"2022-07-07T13:14:12.745866Z","shell.execute_reply.started":"2022-07-07T13:14:12.437580Z","shell.execute_reply":"2022-07-07T13:14:12.744423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(net.loss_curve_, marker=\"o\", label=\"Loss\", lw=0.4, ms=2, ls=\"--\", color=\"red\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:12.747488Z","iopub.execute_input":"2022-07-07T13:14:12.747843Z","iopub.status.idle":"2022-07-07T13:14:13.020985Z","shell.execute_reply.started":"2022-07-07T13:14:12.747810Z","shell.execute_reply":"2022-07-07T13:14:13.019381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cr = pd.DataFrame(classification_report(net.predict(X_test), \n                                        y_test, digits=2,\n                                        output_dict=True)).T\n\ncr['support'] = cr.support.apply(int)\n\ncr.style.background_gradient(cmap='viridis',\n                             subset=pd.IndexSlice['0.0':'1.0', :'f1-score'])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:13.023452Z","iopub.execute_input":"2022-07-07T13:14:13.023944Z","iopub.status.idle":"2022-07-07T13:14:13.098024Z","shell.execute_reply.started":"2022-07-07T13:14:13.023897Z","shell.execute_reply":"2022-07-07T13:14:13.096334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_submission[\"Survived\"]=net.predict(scaler.transform(test.drop(\"Survived\",axis=1)))\ngender_submission","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:14:13.099832Z","iopub.execute_input":"2022-07-07T13:14:13.100395Z","iopub.status.idle":"2022-07-07T13:14:13.166074Z","shell.execute_reply.started":"2022-07-07T13:14:13.100348Z","shell.execute_reply":"2022-07-07T13:14:13.164100Z"},"trusted":true},"execution_count":null,"outputs":[]}]}