{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-25T17:43:24.842542Z","iopub.execute_input":"2022-07-25T17:43:24.843430Z","iopub.status.idle":"2022-07-25T17:43:24.853799Z","shell.execute_reply.started":"2022-07-25T17:43:24.843374Z","shell.execute_reply":"2022-07-25T17:43:24.852943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**I am a Beginner and this is my first attempt using everything I have learned without following a guide.**\n**I might have made many rookie mistakes. Feel free to comment on them. I will be delighted**","metadata":{}},{"cell_type":"code","source":"df_orig_train = pd.read_csv(os.path.join(dirname, \"train.csv\"))\ndf_orig_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.855388Z","iopub.execute_input":"2022-07-25T17:43:24.856272Z","iopub.status.idle":"2022-07-25T17:43:24.885376Z","shell.execute_reply.started":"2022-07-25T17:43:24.856236Z","shell.execute_reply":"2022-07-25T17:43:24.884315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test = pd.read_csv(os.path.join(dirname, \"test.csv\"))\ndf_test_passengerid = df_orig_test[\"PassengerId\"]\ndf_orig_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.886790Z","iopub.execute_input":"2022-07-25T17:43:24.887480Z","iopub.status.idle":"2022-07-25T17:43:24.912065Z","shell.execute_reply.started":"2022-07-25T17:43:24.887440Z","shell.execute_reply":"2022-07-25T17:43:24.910819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.913893Z","iopub.execute_input":"2022-07-25T17:43:24.914341Z","iopub.status.idle":"2022-07-25T17:43:24.929914Z","shell.execute_reply.started":"2022-07-25T17:43:24.914302Z","shell.execute_reply":"2022-07-25T17:43:24.928828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.931329Z","iopub.execute_input":"2022-07-25T17:43:24.932349Z","iopub.status.idle":"2022-07-25T17:43:24.944204Z","shell.execute_reply.started":"2022-07-25T17:43:24.932305Z","shell.execute_reply":"2022-07-25T17:43:24.942989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.945777Z","iopub.execute_input":"2022-07-25T17:43:24.946492Z","iopub.status.idle":"2022-07-25T17:43:24.956077Z","shell.execute_reply.started":"2022-07-25T17:43:24.946452Z","shell.execute_reply":"2022-07-25T17:43:24.955142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"Cabin\"].isnull().sum()/len(df_orig_train[\"Cabin\"]) #drop cabin lots of missing data","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.958422Z","iopub.execute_input":"2022-07-25T17:43:24.959614Z","iopub.status.idle":"2022-07-25T17:43:24.968794Z","shell.execute_reply.started":"2022-07-25T17:43:24.959576Z","shell.execute_reply":"2022-07-25T17:43:24.967782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test[\"Cabin\"].isnull().sum()/len(df_orig_test[\"Cabin\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.971088Z","iopub.execute_input":"2022-07-25T17:43:24.972075Z","iopub.status.idle":"2022-07-25T17:43:24.980631Z","shell.execute_reply.started":"2022-07-25T17:43:24.972007Z","shell.execute_reply":"2022-07-25T17:43:24.979817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train.drop(['Ticket','Cabin',\"Name\",\"PassengerId\"], axis=1, inplace=True)\ndf_orig_test.drop(['Ticket','Cabin',\"Name\",\"PassengerId\"], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.981873Z","iopub.execute_input":"2022-07-25T17:43:24.982395Z","iopub.status.idle":"2022-07-25T17:43:24.992866Z","shell.execute_reply.started":"2022-07-25T17:43:24.982363Z","shell.execute_reply":"2022-07-25T17:43:24.992060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:24.994177Z","iopub.execute_input":"2022-07-25T17:43:24.994696Z","iopub.status.idle":"2022-07-25T17:43:25.013401Z","shell.execute_reply.started":"2022-07-25T17:43:24.994662Z","shell.execute_reply":"2022-07-25T17:43:25.012097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"Embarked\"][:5,]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.014637Z","iopub.execute_input":"2022-07-25T17:43:25.015185Z","iopub.status.idle":"2022-07-25T17:43:25.027481Z","shell.execute_reply.started":"2022-07-25T17:43:25.015148Z","shell.execute_reply":"2022-07-25T17:43:25.026304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.029010Z","iopub.execute_input":"2022-07-25T17:43:25.029610Z","iopub.status.idle":"2022-07-25T17:43:25.034957Z","shell.execute_reply.started":"2022-07-25T17:43:25.029576Z","shell.execute_reply":"2022-07-25T17:43:25.033512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"Embarked\"].value_counts().plot.pie(explode=[0.1, 0.1,0.1], autopct=\"%1.1f%%\", shadow=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.036446Z","iopub.execute_input":"2022-07-25T17:43:25.037106Z","iopub.status.idle":"2022-07-25T17:43:25.173979Z","shell.execute_reply.started":"2022-07-25T17:43:25.037060Z","shell.execute_reply":"2022-07-25T17:43:25.172711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"Embarked\"].fillna(df_orig_train['Embarked'].mode()[0], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.176092Z","iopub.execute_input":"2022-07-25T17:43:25.176758Z","iopub.status.idle":"2022-07-25T17:43:25.186151Z","shell.execute_reply.started":"2022-07-25T17:43:25.176722Z","shell.execute_reply":"2022-07-25T17:43:25.184846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"Embarked\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.188475Z","iopub.execute_input":"2022-07-25T17:43:25.189541Z","iopub.status.idle":"2022-07-25T17:43:25.199291Z","shell.execute_reply.started":"2022-07-25T17:43:25.189482Z","shell.execute_reply":"2022-07-25T17:43:25.197998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(x=\"Embarked\", hue=\"Survived\", kind=\"count\", data=df_orig_train, palette=\"pastel\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.201921Z","iopub.execute_input":"2022-07-25T17:43:25.202958Z","iopub.status.idle":"2022-07-25T17:43:25.552276Z","shell.execute_reply.started":"2022-07-25T17:43:25.202890Z","shell.execute_reply":"2022-07-25T17:43:25.551154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## The above Graph shows the number of Passengers that survived or not survived according to the class\n## We can also note that S class had the most casuality and C is the only class that has more number of survided compared to the not survived\n## This huge amount of not survived can be chalked up to S having the most number of People and maybe they didn't get the appropriate facility to survive,\n## But S also has the highest number of survived among other class (More than C and Q combined)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.553700Z","iopub.execute_input":"2022-07-25T17:43:25.554015Z","iopub.status.idle":"2022-07-25T17:43:25.559261Z","shell.execute_reply.started":"2022-07-25T17:43:25.553985Z","shell.execute_reply":"2022-07-25T17:43:25.558091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Lets analyse Pclass now\nfig,ax = plt.subplots(2,2,figsize=(15,15))\nsns.set_palette(\"pastel\")\ndf_orig_train[\"Pclass\"].value_counts().plot.pie(explode=[0.1, 0.1,0.1], autopct=\"%1.1f%%\", shadow=True,ax=ax[0][0])\nsns.countplot(x=\"Pclass\", hue=\"Survived\", data=df_orig_train, palette=\"pastel\",ax=ax[0][1])\nsns.countplot(x=\"Pclass\", hue=\"Embarked\", data=df_orig_train, palette=\"pastel\",ax=ax[1][0])\nsns.countplot(x=\"Pclass\", hue=\"Sex\", data=df_orig_train, palette=\"pastel\",ax=ax[1][1])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:25.560811Z","iopub.execute_input":"2022-07-25T17:43:25.561172Z","iopub.status.idle":"2022-07-25T17:43:26.167908Z","shell.execute_reply.started":"2022-07-25T17:43:25.561138Z","shell.execute_reply":"2022-07-25T17:43:26.166991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Most of the people from Queenstown are in Class 3, while people from Southampton are in highest number in all the classes\n# Pclass 1 sees most people saved, while plcass 3 sees more people dead, this might be due to more priority given to them","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:26.169434Z","iopub.execute_input":"2022-07-25T17:43:26.170049Z","iopub.status.idle":"2022-07-25T17:43:26.174524Z","shell.execute_reply.started":"2022-07-25T17:43:26.169991Z","shell.execute_reply":"2022-07-25T17:43:26.173411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#before proceeding with Fare and Pclass and Embarked comparision Lets check Outliers in Fare\nsns.boxplot(x=\"Fare\", data=df_orig_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:26.175833Z","iopub.execute_input":"2022-07-25T17:43:26.176229Z","iopub.status.idle":"2022-07-25T17:43:26.342899Z","shell.execute_reply.started":"2022-07-25T17:43:26.176168Z","shell.execute_reply":"2022-07-25T17:43:26.341695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2,2,figsize=(20,15))\nsns.histplot(x=\"Fare\", hue=\"Pclass\", data= df_orig_train, ax=ax[0][0],palette=\"tab10\")\nsns.histplot(x=\"Fare\", hue=\"Embarked\", data= df_orig_train, ax=ax[0][1],palette=\"tab10\")\nsns.boxplot(x=\"Pclass\", y=\"Fare\", data= df_orig_train, ax=ax[1][0],palette=\"tab10\")\nsns.boxplot(x=\"Embarked\", y=\"Fare\", data= df_orig_train, ax=ax[1][1],palette=\"tab10\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:26.344634Z","iopub.execute_input":"2022-07-25T17:43:26.346021Z","iopub.status.idle":"2022-07-25T17:43:28.721160Z","shell.execute_reply.started":"2022-07-25T17:43:26.345966Z","shell.execute_reply":"2022-07-25T17:43:28.719844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## It is easily seen that most of the people that boarded the Ship from Queenstown paid the moderate ammonts of fare\n## We can also see that Class 1 payed mostly higer sums of fare implying that maybe they were in a luxury class\n## People who boarded form the port Cherbourg payed higer amount as there 25%ile is above the median for Southampton and the 75th%ile of Queenstown\n## class distribution is easier to read as it follows the same pattern of class 3 being the lowest, 2 being the middle and 1 being the most Expensive","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:28.722680Z","iopub.execute_input":"2022-07-25T17:43:28.723067Z","iopub.status.idle":"2022-07-25T17:43:28.728790Z","shell.execute_reply.started":"2022-07-25T17:43:28.723009Z","shell.execute_reply":"2022-07-25T17:43:28.727460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Lets see how does Fare relates with Survivabilty\nfig, ax = plt.subplots(1,1,figsize=(15,8))\nsns.distplot(a=df_orig_train[df_orig_train[\"Survived\"]==1][\"Fare\"],color=\"#3bf9a0\")\nsns.distplot(a=df_orig_train[df_orig_train[\"Survived\"]!=1][\"Fare\"],color=\"#f9a13b\")\nax.legend(labels=[\"Survived\",\"Not Survived\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:28.730436Z","iopub.execute_input":"2022-07-25T17:43:28.730864Z","iopub.status.idle":"2022-07-25T17:43:29.195483Z","shell.execute_reply.started":"2022-07-25T17:43:28.730829Z","shell.execute_reply":"2022-07-25T17:43:29.194627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors = [\"#f9a13b\", \"#3bf9a0\"]\nfig, ax = plt.subplots(1,3,figsize=(20,8))\nsns.set_palette(sns.color_palette(colors))\nsns.countplot(x=\"Sex\", hue=\"Survived\", data=df_orig_train,ax=ax[1])\nsns.boxplot(x=\"Sex\", y=\"Fare\", data= df_orig_train,ax=ax[2])\nax[0].pie(df_orig_train[\"Sex\"].value_counts(),explode=[0.1, 0.1],shadow=True,\n          labels= [\"Male\",\"Female\"],autopct=\"%1.1f%%\",colors = sns.color_palette(colors))\nax[0].legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:29.196562Z","iopub.execute_input":"2022-07-25T17:43:29.197256Z","iopub.status.idle":"2022-07-25T17:43:29.679115Z","shell.execute_reply.started":"2022-07-25T17:43:29.197218Z","shell.execute_reply":"2022-07-25T17:43:29.677898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Number of Men is substantially huge\n## We can see that more number of men died\n## Many men paid no or Almost not Fare,it is possible that many people men on board were ship workers(That is something that I didn't check as of now)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:29.680551Z","iopub.execute_input":"2022-07-25T17:43:29.681311Z","iopub.status.idle":"2022-07-25T17:43:29.686093Z","shell.execute_reply.started":"2022-07-25T17:43:29.681271Z","shell.execute_reply":"2022-07-25T17:43:29.684929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:29.687737Z","iopub.execute_input":"2022-07-25T17:43:29.688450Z","iopub.status.idle":"2022-07-25T17:43:29.708100Z","shell.execute_reply.started":"2022-07-25T17:43:29.688416Z","shell.execute_reply":"2022-07-25T17:43:29.707140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets do EDA on Age column \n## Lets see how does Fare relates with Survivabilty\nfig, ax = plt.subplots(2,2,figsize=(20,15))\nsns.histplot(data=df_orig_train,kde=True,x=\"Age\",hue=\"Survived\",ax=ax[0][0],color=\"#3bf9a0\")\nsns.histplot(data=df_orig_train,x=\"Age\",hue=\"Sex\",ax=ax[0][1],color=\"#3bf9a0\",kde=True)\n#sns.displot(a=df_orig_train[df_orig_train[\"Sex\"]==\"female\"][\"Age\"],ax=ax[1],color=\"#f9a13b\")\nsns.boxplot(x=\"Sex\",y=\"Age\", data= df_orig_train,ax=ax[1][0])\nsns.boxplot(x=\"Age\", data= df_orig_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:29.709478Z","iopub.execute_input":"2022-07-25T17:43:29.710350Z","iopub.status.idle":"2022-07-25T17:43:30.593920Z","shell.execute_reply.started":"2022-07-25T17:43:29.710310Z","shell.execute_reply":"2022-07-25T17:43:30.592768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Age shows a right skewness\n# Number of people who were saved was more for younger people, but they also had the most number of casualities","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.595645Z","iopub.execute_input":"2022-07-25T17:43:30.596128Z","iopub.status.idle":"2022-07-25T17:43:30.601856Z","shell.execute_reply.started":"2022-07-25T17:43:30.596082Z","shell.execute_reply":"2022-07-25T17:43:30.600736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(1,1,figsize=(10,10))\nsns.histplot(x=\"Age\", data= df_orig_train,kde=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.603177Z","iopub.execute_input":"2022-07-25T17:43:30.603539Z","iopub.status.idle":"2022-07-25T17:43:30.878544Z","shell.execute_reply.started":"2022-07-25T17:43:30.603506Z","shell.execute_reply":"2022-07-25T17:43:30.877545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We check the value of the outliers to find any patterns if there are none we can just cap the outliers\nIQR = df_orig_train[\"Age\"].quantile(0.75) - df_orig_train[\"Age\"].quantile(0.25)\ndf_orig_train[df_orig_train[\"Age\"]>(df_orig_train[\"Age\"].quantile(0.75)+(IQR*1.5))]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.879887Z","iopub.execute_input":"2022-07-25T17:43:30.880456Z","iopub.status.idle":"2022-07-25T17:43:30.903886Z","shell.execute_reply.started":"2022-07-25T17:43:30.880418Z","shell.execute_reply":"2022-07-25T17:43:30.902630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train_all_fields = pd.read_csv(os.path.join(dirname, \"train.csv\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.905668Z","iopub.execute_input":"2022-07-25T17:43:30.906916Z","iopub.status.idle":"2022-07-25T17:43:30.920520Z","shell.execute_reply.started":"2022-07-25T17:43:30.906868Z","shell.execute_reply":"2022-07-25T17:43:30.918874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IQR = df_orig_train_all_fields[\"Age\"].quantile(0.75) - df_orig_train_all_fields[\"Age\"].quantile(0.25)\ndf_orig_train_all_fields[df_orig_train_all_fields[\"Age\"]>(df_orig_train_all_fields[\"Age\"].quantile(0.75)+(IQR*1.5))]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.922150Z","iopub.execute_input":"2022-07-25T17:43:30.923302Z","iopub.status.idle":"2022-07-25T17:43:30.947872Z","shell.execute_reply.started":"2022-07-25T17:43:30.923260Z","shell.execute_reply":"2022-07-25T17:43:30.946888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## I don't see any patters sorry(Except all were male)\n## Lets deal with the outliers by capping the values","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.949362Z","iopub.execute_input":"2022-07-25T17:43:30.949997Z","iopub.status.idle":"2022-07-25T17:43:30.954897Z","shell.execute_reply.started":"2022-07-25T17:43:30.949955Z","shell.execute_reply":"2022-07-25T17:43:30.953981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Yup, all null values from the age column is removed. Median was used because data was continuous and had outlier\n## Median is not affected by outliers so it was a good option(Eventhough I have removed Outliers)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.956498Z","iopub.execute_input":"2022-07-25T17:43:30.957113Z","iopub.status.idle":"2022-07-25T17:43:30.964012Z","shell.execute_reply.started":"2022-07-25T17:43:30.957072Z","shell.execute_reply":"2022-07-25T17:43:30.963122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nsns.stripplot(x=\"Embarked\",y=\"Age\",hue=\"Pclass\",data= df_orig_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:30.965599Z","iopub.execute_input":"2022-07-25T17:43:30.965993Z","iopub.status.idle":"2022-07-25T17:43:31.315272Z","shell.execute_reply.started":"2022-07-25T17:43:30.965957Z","shell.execute_reply":"2022-07-25T17:43:31.314105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We can see that most people in Pclass 3 were in there lower to middle ages","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:31.317016Z","iopub.execute_input":"2022-07-25T17:43:31.317868Z","iopub.status.idle":"2022-07-25T17:43:31.323360Z","shell.execute_reply.started":"2022-07-25T17:43:31.317813Z","shell.execute_reply":"2022-07-25T17:43:31.322061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Now we move to the columns that deal with family relationships\ndf_orig_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:31.325361Z","iopub.execute_input":"2022-07-25T17:43:31.326463Z","iopub.status.idle":"2022-07-25T17:43:31.348252Z","shell.execute_reply.started":"2022-07-25T17:43:31.326410Z","shell.execute_reply":"2022-07-25T17:43:31.347045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"SibSp\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:31.349918Z","iopub.execute_input":"2022-07-25T17:43:31.350294Z","iopub.status.idle":"2022-07-25T17:43:31.361950Z","shell.execute_reply.started":"2022-07-25T17:43:31.350258Z","shell.execute_reply":"2022-07-25T17:43:31.360710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\ncolors = [\"#ffd700\",\"#ffb14e\",\"#fa8775\",\"#ea5f94\",\"#cd34b5\",\"#9d02d7\",\"#0000ff\"]\ncolor_bar = [\"#f9a13b\",\"#3bf9a0\"]\nsns.set_palette(sns.color_palette(colors))\nsns.countplot(x=\"SibSp\",data=df_orig_train,ax=ax[0])\nsns.countplot(x=\"SibSp\", hue=\"Survived\", data=df_orig_train,ax=ax[1],palette=sns.color_palette(color_bar))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:31.363801Z","iopub.execute_input":"2022-07-25T17:43:31.364186Z","iopub.status.idle":"2022-07-25T17:43:31.749509Z","shell.execute_reply.started":"2022-07-25T17:43:31.364153Z","shell.execute_reply":"2022-07-25T17:43:31.748245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[df_orig_train[\"SibSp\"]==8]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:31.751226Z","iopub.execute_input":"2022-07-25T17:43:31.751579Z","iopub.status.idle":"2022-07-25T17:43:31.767750Z","shell.execute_reply.started":"2022-07-25T17:43:31.751547Z","shell.execute_reply":"2022-07-25T17:43:31.766529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# From the looks of it they all seem to belong the same family. no body survived sadly","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:31.769167Z","iopub.execute_input":"2022-07-25T17:43:31.769530Z","iopub.status.idle":"2022-07-25T17:43:31.777561Z","shell.execute_reply.started":"2022-07-25T17:43:31.769485Z","shell.execute_reply":"2022-07-25T17:43:31.776186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(1,2,figsize=(25, 10))\ncolors = [\"#f9a13b\",\"#3bf9a0\",\"#9D02D7\"]\nsns.set_palette(sns.color_palette(colors))\nsns.stripplot(y=\"Age\",x=\"SibSp\",hue=\"Sex\",data=df_orig_train,ax=ax[0])\nsns.stripplot(y=\"Fare\",x=\"SibSp\",hue=\"Embarked\",data=df_orig_train,ax=ax[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:31.780117Z","iopub.execute_input":"2022-07-25T17:43:31.780738Z","iopub.status.idle":"2022-07-25T17:43:32.670946Z","shell.execute_reply.started":"2022-07-25T17:43:31.780681Z","shell.execute_reply":"2022-07-25T17:43:32.669822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most people seem to have no to one sibling or spouse. The ones with more than 3 siblings/spouse are of ages below 35, with peolpe with more than 4 sibling/spouse having lower than 20 age. We can't rely on the family with 8 siblings/spouse, as they had null values and I filled it already.Density of people below 15 years of age if low, and most of the females don't have sibling/spouse(Under the 15 years age group)","metadata":{}},{"cell_type":"code","source":"df_orig_train[\"Parch\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:32.672313Z","iopub.execute_input":"2022-07-25T17:43:32.672655Z","iopub.status.idle":"2022-07-25T17:43:32.680601Z","shell.execute_reply.started":"2022-07-25T17:43:32.672623Z","shell.execute_reply":"2022-07-25T17:43:32.679361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\ncolors = [\"#ffd700\",\"#ffb14e\",\"#fa8775\",\"#ea5f94\",\"#cd34b5\",\"#9d02d7\",\"#0000ff\"]\ncolor_bar = [\"#f9a13b\",\"#3bf9a0\"]\nsns.set_palette(sns.color_palette(colors))\nsns.countplot(x=\"Parch\",data=df_orig_train,ax=ax[0])\nsns.countplot(x=\"Parch\", hue=\"Survived\", data=df_orig_train,ax=ax[1],palette=sns.color_palette(color_bar))\nax[1].legend(loc='upper right', title='Team')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:32.682108Z","iopub.execute_input":"2022-07-25T17:43:32.683087Z","iopub.status.idle":"2022-07-25T17:43:33.039813Z","shell.execute_reply.started":"2022-07-25T17:43:32.683037Z","shell.execute_reply":"2022-07-25T17:43:33.038557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train.pivot_table('Parch', index='SibSp')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:33.041394Z","iopub.execute_input":"2022-07-25T17:43:33.041936Z","iopub.status.idle":"2022-07-25T17:43:33.059085Z","shell.execute_reply.started":"2022-07-25T17:43:33.041900Z","shell.execute_reply":"2022-07-25T17:43:33.058040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(1,2,figsize=(25, 10))\ncolors = [\"#f9a13b\",\"#3bf9a0\",\"#9D02D7\"]\nsns.set_palette(sns.color_palette(colors))\nsns.stripplot(y=\"Age\",x=\"Parch\",hue=\"Sex\",data=df_orig_train,ax=ax[0])\nsns.stripplot(y=\"Fare\",x=\"Parch\",hue=\"Embarked\",data=df_orig_train,ax=ax[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:33.060306Z","iopub.execute_input":"2022-07-25T17:43:33.060713Z","iopub.status.idle":"2022-07-25T17:43:33.954737Z","shell.execute_reply.started":"2022-07-25T17:43:33.060680Z","shell.execute_reply":"2022-07-25T17:43:33.953241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lot's of single men\n# Most people with childeren or parent are female","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:33.956237Z","iopub.execute_input":"2022-07-25T17:43:33.956815Z","iopub.status.idle":"2022-07-25T17:43:33.960611Z","shell.execute_reply.started":"2022-07-25T17:43:33.956776Z","shell.execute_reply":"2022-07-25T17:43:33.959699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(df_orig_train.corr())","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:33.961913Z","iopub.execute_input":"2022-07-25T17:43:33.962560Z","iopub.status.idle":"2022-07-25T17:43:34.220854Z","shell.execute_reply.started":"2022-07-25T17:43:33.962523Z","shell.execute_reply":"2022-07-25T17:43:34.219532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IQR = df_orig_train[\"Fare\"].quantile(0.75) - df_orig_train[\"Fare\"].quantile(0.25)\n#calculate the boundries\nlower = df_orig_train[\"Fare\"].quantile(0.25) - (IQR * 1.5)\nupper = df_orig_train[\"Fare\"].quantile(0.75) + (IQR * 1.5)\nprint(f\"lower = {lower} upper={upper}\")\n# replacing the outliers\ndf_orig_train[\"Fare\"] = np.where(df_orig_train[\"Fare\"] > upper, upper, np.where(df_orig_train[\"Fare\"] < lower, lower, df_orig_train[\"Fare\"]))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:34.222255Z","iopub.execute_input":"2022-07-25T17:43:34.222586Z","iopub.status.idle":"2022-07-25T17:43:34.237244Z","shell.execute_reply.started":"2022-07-25T17:43:34.222547Z","shell.execute_reply":"2022-07-25T17:43:34.236011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(x=\"Fare\",data=df_orig_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:34.238877Z","iopub.execute_input":"2022-07-25T17:43:34.239280Z","iopub.status.idle":"2022-07-25T17:43:34.410226Z","shell.execute_reply.started":"2022-07-25T17:43:34.239242Z","shell.execute_reply":"2022-07-25T17:43:34.409020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IQR = df_orig_test[\"Fare\"].quantile(0.75) - df_orig_test[\"Fare\"].quantile(0.25)\n#calculate the boundries\nlower = df_orig_test[\"Fare\"].quantile(0.25) - (IQR * 1.5)\nupper = df_orig_test[\"Fare\"].quantile(0.75) + (IQR * 1.5)\nprint(f\"lower = {lower} upper={upper}\")\n# replacing the outliers\ndf_orig_test[\"Fare\"] = np.where(df_orig_test[\"Fare\"] > upper, upper, np.where(df_orig_test[\"Fare\"] < lower, lower, df_orig_test[\"Fare\"]))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:34.411826Z","iopub.execute_input":"2022-07-25T17:43:34.412430Z","iopub.status.idle":"2022-07-25T17:43:34.427916Z","shell.execute_reply.started":"2022-07-25T17:43:34.412379Z","shell.execute_reply":"2022-07-25T17:43:34.426902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(x=\"Fare\",data=df_orig_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:34.429599Z","iopub.execute_input":"2022-07-25T17:43:34.430436Z","iopub.status.idle":"2022-07-25T17:43:34.885968Z","shell.execute_reply.started":"2022-07-25T17:43:34.430396Z","shell.execute_reply":"2022-07-25T17:43:34.884782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IQR = df_orig_train[\"Age\"].quantile(0.75) - df_orig_train[\"Age\"].quantile(0.25)\n#calculate the boundries\nlower = df_orig_train[\"Age\"].quantile(0.25) - (IQR * 1.5)\nupper = df_orig_train[\"Age\"].quantile(0.75) + (IQR * 1.5)\nprint(f\"lower = {lower} upper={upper}\")\n# replacing the outliers\ndf_orig_train[\"Age\"] = np.where(df_orig_train[\"Age\"] > upper, upper\n                                                   ,np.where(df_orig_train[\"Age\"] < lower, lower, df_orig_train[\"Age\"]))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:34.887850Z","iopub.execute_input":"2022-07-25T17:43:34.888314Z","iopub.status.idle":"2022-07-25T17:43:34.903557Z","shell.execute_reply.started":"2022-07-25T17:43:34.888267Z","shell.execute_reply":"2022-07-25T17:43:34.902196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(x=\"Age\",data=df_orig_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:34.905325Z","iopub.execute_input":"2022-07-25T17:43:34.906290Z","iopub.status.idle":"2022-07-25T17:43:35.068738Z","shell.execute_reply.started":"2022-07-25T17:43:34.906250Z","shell.execute_reply":"2022-07-25T17:43:35.067793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IQR = df_orig_test[\"Age\"].quantile(0.75) - df_orig_test[\"Age\"].quantile(0.25)\n#calculate the boundries\nlower = df_orig_test[\"Age\"].quantile(0.25) - (IQR * 1.5)\nupper = df_orig_test[\"Age\"].quantile(0.75) + (IQR * 1.5)\nprint(f\"lower = {lower} upper={upper}\")\n# replacing the outliers\ndf_orig_test[\"Age\"] = np.where(df_orig_test[\"Age\"] > upper, upper ,np.where(df_orig_test[\"Age\"] < lower, lower, df_orig_test[\"Age\"]))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.070728Z","iopub.execute_input":"2022-07-25T17:43:35.071602Z","iopub.status.idle":"2022-07-25T17:43:35.086260Z","shell.execute_reply.started":"2022-07-25T17:43:35.071540Z","shell.execute_reply":"2022-07-25T17:43:35.084773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(x=\"Age\",data=df_orig_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.088442Z","iopub.execute_input":"2022-07-25T17:43:35.089485Z","iopub.status.idle":"2022-07-25T17:43:35.254750Z","shell.execute_reply.started":"2022-07-25T17:43:35.089433Z","shell.execute_reply":"2022-07-25T17:43:35.253386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"Age\"].fillna(df_orig_train[\"Age\"].median(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.256349Z","iopub.execute_input":"2022-07-25T17:43:35.256782Z","iopub.status.idle":"2022-07-25T17:43:35.265796Z","shell.execute_reply.started":"2022-07-25T17:43:35.256742Z","shell.execute_reply":"2022-07-25T17:43:35.264480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(x=\"Age\",data=df_orig_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.267668Z","iopub.execute_input":"2022-07-25T17:43:35.268738Z","iopub.status.idle":"2022-07-25T17:43:35.434444Z","shell.execute_reply.started":"2022-07-25T17:43:35.268688Z","shell.execute_reply":"2022-07-25T17:43:35.433627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test[\"Age\"].fillna(df_orig_test[\"Age\"].median(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.435688Z","iopub.execute_input":"2022-07-25T17:43:35.436260Z","iopub.status.idle":"2022-07-25T17:43:35.441791Z","shell.execute_reply.started":"2022-07-25T17:43:35.436226Z","shell.execute_reply":"2022-07-25T17:43:35.441070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test[df_orig_test[\"Fare\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.443380Z","iopub.execute_input":"2022-07-25T17:43:35.444136Z","iopub.status.idle":"2022-07-25T17:43:35.464685Z","shell.execute_reply.started":"2022-07-25T17:43:35.444089Z","shell.execute_reply":"2022-07-25T17:43:35.463204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test[df_orig_test[\"Age\"]>55][df_orig_test[\"Sex\"]==\"male\"][df_orig_test[\"Embarked\"]==\"S\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.466628Z","iopub.execute_input":"2022-07-25T17:43:35.467496Z","iopub.status.idle":"2022-07-25T17:43:35.487280Z","shell.execute_reply.started":"2022-07-25T17:43:35.467444Z","shell.execute_reply":"2022-07-25T17:43:35.485954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test[\"Fare\"].fillna(\n    df_orig_test[df_orig_test[\"Age\"]>55][df_orig_test[\"Sex\"]==\"male\"][df_orig_test[\"Embarked\"]==\"S\"][\"Fare\"]\n    .median(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.488675Z","iopub.execute_input":"2022-07-25T17:43:35.489538Z","iopub.status.idle":"2022-07-25T17:43:35.501393Z","shell.execute_reply.started":"2022-07-25T17:43:35.489422Z","shell.execute_reply":"2022-07-25T17:43:35.500111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.502845Z","iopub.execute_input":"2022-07-25T17:43:35.503201Z","iopub.status.idle":"2022-07-25T17:43:35.514800Z","shell.execute_reply.started":"2022-07-25T17:43:35.503168Z","shell.execute_reply":"2022-07-25T17:43:35.513664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_train[\"Pclass\"] = df_orig_train[\"Pclass\"].astype('category')\ndf_orig_train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.516893Z","iopub.execute_input":"2022-07-25T17:43:35.517682Z","iopub.status.idle":"2022-07-25T17:43:35.530083Z","shell.execute_reply.started":"2022-07-25T17:43:35.517632Z","shell.execute_reply":"2022-07-25T17:43:35.529008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Lets remove the correlated values\nX = df_orig_train.drop(\"Survived\",axis=1) # Inputs\ny = df_orig_train[\"Survived\"] #Outputs","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.531963Z","iopub.execute_input":"2022-07-25T17:43:35.532340Z","iopub.status.idle":"2022-07-25T17:43:35.538492Z","shell.execute_reply.started":"2022-07-25T17:43:35.532307Z","shell.execute_reply":"2022-07-25T17:43:35.537601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.539740Z","iopub.execute_input":"2022-07-25T17:43:35.540946Z","iopub.status.idle":"2022-07-25T17:43:35.561209Z","shell.execute_reply.started":"2022-07-25T17:43:35.540893Z","shell.execute_reply":"2022-07-25T17:43:35.559933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\ncorr = X.corr()\nsns.heatmap(corr,annot=True,cmap=plt.cm.CMRmap_r)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.563017Z","iopub.execute_input":"2022-07-25T17:43:35.564052Z","iopub.status.idle":"2022-07-25T17:43:35.861303Z","shell.execute_reply.started":"2022-07-25T17:43:35.563972Z","shell.execute_reply":"2022-07-25T17:43:35.859983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## There is not much correlation between any feature \n## Also I am going to use XGBOOST for classification so unimportant features will not effect the model","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.862743Z","iopub.execute_input":"2022-07-25T17:43:35.863079Z","iopub.status.idle":"2022-07-25T17:43:35.867840Z","shell.execute_reply.started":"2022-07-25T17:43:35.863047Z","shell.execute_reply":"2022-07-25T17:43:35.866683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sex and Embarked need encoding\nonehot_columns = [\"Sex\",\"Embarked\"]\ny = pd.get_dummies(df_orig_train[onehot_columns])\nnew_df = df_orig_train.join(y)\nnew_df.drop(onehot_columns,inplace=True,axis=1)\nnew_df.drop([\"Sex_female\",\"Embarked_S\"],inplace=True,axis=1)\nnew_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.869416Z","iopub.execute_input":"2022-07-25T17:43:35.870009Z","iopub.status.idle":"2022-07-25T17:43:35.900343Z","shell.execute_reply.started":"2022-07-25T17:43:35.869963Z","shell.execute_reply":"2022-07-25T17:43:35.899119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## When using StratifiedKfold I was getting an error to fix that I had to covert \"Pclass\" to and Int or String or bool etc\nfrom sklearn.preprocessing import LabelEncoder\n\nnew_df[\"Pclass\"] = LabelEncoder().fit_transform(new_df[\"Pclass\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.902270Z","iopub.execute_input":"2022-07-25T17:43:35.903162Z","iopub.status.idle":"2022-07-25T17:43:35.910443Z","shell.execute_reply.started":"2022-07-25T17:43:35.903040Z","shell.execute_reply":"2022-07-25T17:43:35.909554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = new_df.drop(\"Survived\",axis=1) # Inputs\ny = new_df[\"Survived\"] #Outputs","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.912076Z","iopub.execute_input":"2022-07-25T17:43:35.912811Z","iopub.status.idle":"2022-07-25T17:43:35.921953Z","shell.execute_reply.started":"2022-07-25T17:43:35.912762Z","shell.execute_reply":"2022-07-25T17:43:35.921087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBClassifier\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.model_selection import GridSearchCV\n\nlearning_rate = [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3]\nn_estimators = [100, 200, 300, 400, 500]\n\nparams = {\n            'objective':'binary:logistic',\n            'max_depth': 5\n        }\n\nmodel = XGBClassifier(**params)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.923831Z","iopub.execute_input":"2022-07-25T17:43:35.924669Z","iopub.status.idle":"2022-07-25T17:43:35.934547Z","shell.execute_reply.started":"2022-07-25T17:43:35.924617Z","shell.execute_reply":"2022-07-25T17:43:35.933352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kfold = StratifiedKFold(n_splits=10,shuffle=True,random_state=5)\n\nparam_grid = dict(learning_rate=learning_rate, n_estimators=n_estimators)\n\ngrid_search = GridSearchCV(model,param_grid, scoring=\"precision\", n_jobs=-1, cv=kfold)\ngrid_result = grid_search.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:43:35.935961Z","iopub.execute_input":"2022-07-25T17:43:35.936355Z","iopub.status.idle":"2022-07-25T17:45:11.946117Z","shell.execute_reply.started":"2022-07-25T17:43:35.936323Z","shell.execute_reply":"2022-07-25T17:45:11.945087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Best: %f using %s\" % (grid_result.best_score_, grid_result.best_params_))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:11.948140Z","iopub.execute_input":"2022-07-25T17:45:11.948988Z","iopub.status.idle":"2022-07-25T17:45:11.958364Z","shell.execute_reply.started":"2022-07-25T17:45:11.948938Z","shell.execute_reply":"2022-07-25T17:45:11.957108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {\n            'objective':'binary:logistic',\n            'max_depth': 5,\n            'learning_rate': 0.01,\n            'n_estimators': 300\n        }\n\nmodel = XGBClassifier(**params)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:11.959711Z","iopub.execute_input":"2022-07-25T17:45:11.960303Z","iopub.status.idle":"2022-07-25T17:45:11.966821Z","shell.execute_reply.started":"2022-07-25T17:45:11.960261Z","shell.execute_reply":"2022-07-25T17:45:11.965968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets use Kfold once again to test the params\nscore = []\ndef training(train, test, fold_no):\n  x_train = train.drop(['Survived'],axis=1)\n  y_train = train.Survived\n  x_test = test.drop(['Survived'],axis=1)\n  y_test = test.Survived\n  model.fit(x_train, y_train)\n  score.append(model.score(x_test,y_test))\n  print('For Fold {} the accuracy is {}'.format(str(fold_no),score[fold_no-1]))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:11.968319Z","iopub.execute_input":"2022-07-25T17:45:11.968902Z","iopub.status.idle":"2022-07-25T17:45:11.978234Z","shell.execute_reply.started":"2022-07-25T17:45:11.968868Z","shell.execute_reply":"2022-07-25T17:45:11.977140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random \nfold_no = 1\nrandom.seed(5)\nfor train_index,test_index in kfold.split(X, y):\n  train = new_df.iloc[train_index,:]\n  test = new_df.iloc[test_index,:]\n  training(train, test, fold_no)\n  fold_no += 1","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:11.979679Z","iopub.execute_input":"2022-07-25T17:45:11.980086Z","iopub.status.idle":"2022-07-25T17:45:21.509577Z","shell.execute_reply.started":"2022-07-25T17:45:11.980021Z","shell.execute_reply":"2022-07-25T17:45:21.508620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from statistics import mean\nmean(score)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:21.513616Z","iopub.execute_input":"2022-07-25T17:45:21.514269Z","iopub.status.idle":"2022-07-25T17:45:21.524596Z","shell.execute_reply.started":"2022-07-25T17:45:21.514227Z","shell.execute_reply":"2022-07-25T17:45:21.523501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orig_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:21.526655Z","iopub.execute_input":"2022-07-25T17:45:21.527367Z","iopub.status.idle":"2022-07-25T17:45:21.546671Z","shell.execute_reply.started":"2022-07-25T17:45:21.527328Z","shell.execute_reply":"2022-07-25T17:45:21.545782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onehot_columns = [\"Sex\",\"Embarked\"]\ny = pd.get_dummies(df_orig_test[onehot_columns])\nnew_df_test = df_orig_test.join(y)\nnew_df_test.drop(onehot_columns,inplace=True,axis=1)\nnew_df_test.drop([\"Sex_female\",\"Embarked_S\"],inplace=True,axis=1)\nnew_df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:21.547934Z","iopub.execute_input":"2022-07-25T17:45:21.548317Z","iopub.status.idle":"2022-07-25T17:45:21.574512Z","shell.execute_reply.started":"2022-07-25T17:45:21.548284Z","shell.execute_reply":"2022-07-25T17:45:21.573346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df_test[\"Pclass\"] = LabelEncoder().fit_transform(new_df_test[\"Pclass\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:21.576003Z","iopub.execute_input":"2022-07-25T17:45:21.576378Z","iopub.status.idle":"2022-07-25T17:45:21.581292Z","shell.execute_reply.started":"2022-07-25T17:45:21.576346Z","shell.execute_reply":"2022-07-25T17:45:21.580386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(new_df_test)\n\noutput = pd.DataFrame({'PassengerId': df_test_passengerid, 'Survived': predictions})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T17:45:21.582873Z","iopub.execute_input":"2022-07-25T17:45:21.583609Z","iopub.status.idle":"2022-07-25T17:45:21.606157Z","shell.execute_reply.started":"2022-07-25T17:45:21.583563Z","shell.execute_reply":"2022-07-25T17:45:21.605157Z"},"trusted":true},"execution_count":null,"outputs":[]}]}