{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:19:57.871380Z","iopub.execute_input":"2022-07-06T00:19:57.871878Z","iopub.status.idle":"2022-07-06T00:19:58.542660Z","shell.execute_reply.started":"2022-07-06T00:19:57.871781Z","shell.execute_reply":"2022-07-06T00:19:58.541397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('../input/titanic/train.csv')\ntest=  pd.read_csv('../input/titanic/test.csv')\ndftitanic = data.copy()\ndfcomp = test.copy() ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:50:52.353743Z","iopub.execute_input":"2022-07-06T00:50:52.354778Z","iopub.status.idle":"2022-07-06T00:50:52.373199Z","shell.execute_reply.started":"2022-07-06T00:50:52.354724Z","shell.execute_reply":"2022-07-06T00:50:52.372030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**What's in the name?**","metadata":{}},{"cell_type":"code","source":"dftitanic['Name'].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:19:59.172101Z","iopub.execute_input":"2022-07-06T00:19:59.173067Z","iopub.status.idle":"2022-07-06T00:19:59.187987Z","shell.execute_reply.started":"2022-07-06T00:19:59.173029Z","shell.execute_reply":"2022-07-06T00:19:59.187163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Extracting & Analyzing Titles**\n\nThis is called feature engineering where you derive additional columns as per your domain knowledge like creating 'Title in the following example.\n","metadata":{}},{"cell_type":"code","source":"import re \na='Braund, Mr.Owen Harris'\nre.search(' ([A-Z][a-z]+)\\.', a).group(1)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:19:59.749340Z","iopub.execute_input":"2022-07-06T00:19:59.750372Z","iopub.status.idle":"2022-07-06T00:19:59.757082Z","shell.execute_reply.started":"2022-07-06T00:19:59.750331Z","shell.execute_reply":"2022-07-06T00:19:59.755969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['Title'] = dftitanic['Name'].apply(lambda x: re.search(' ([A-Z][a-z]+)\\.', x).group(1))\ndftitanic.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:00.072419Z","iopub.execute_input":"2022-07-06T00:20:00.073251Z","iopub.status.idle":"2022-07-06T00:20:00.105114Z","shell.execute_reply.started":"2022-07-06T00:20:00.073198Z","shell.execute_reply":"2022-07-06T00:20:00.103619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:00.367530Z","iopub.execute_input":"2022-07-06T00:20:00.367928Z","iopub.status.idle":"2022-07-06T00:20:00.381073Z","shell.execute_reply.started":"2022-07-06T00:20:00.367895Z","shell.execute_reply":"2022-07-06T00:20:00.379882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The term Mademoiselle is a French familiar title, abbreviated Mlle, traditionally given to an unmarried woman. The equivalent in English is \"Miss\". However, the courtesy title \"Madame\" is accorded women where their marital status is unknown.","metadata":{}},{"cell_type":"code","source":"dftitanic['Title'] = dftitanic['Title'].replace('Mlle', 'Miss')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:00.986140Z","iopub.execute_input":"2022-07-06T00:20:00.987194Z","iopub.status.idle":"2022-07-06T00:20:00.994150Z","shell.execute_reply.started":"2022-07-06T00:20:00.987147Z","shell.execute_reply":"2022-07-06T00:20:00.992997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In France, one traditionally calls a young, unmarried woman Mademoiselle – Mlle for short – and an older, married woman _Madame_, whose abbreviation is Mme.","metadata":{}},{"cell_type":"code","source":"dftitanic['Title'] = dftitanic['Title'].replace('Mme', 'Mrs')\ndftitanic.loc[(~dftitanic['Title'].isin(['Mr', 'Mrs', 'Miss', 'Master'])), 'Title'] = 'Rare Title'\ndftitanic['Title'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:01.590275Z","iopub.execute_input":"2022-07-06T00:20:01.590669Z","iopub.status.idle":"2022-07-06T00:20:01.603625Z","shell.execute_reply.started":"2022-07-06T00:20:01.590636Z","shell.execute_reply":"2022-07-06T00:20:01.602466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=dftitanic,x='Title', hue='Survived');","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:01.910801Z","iopub.execute_input":"2022-07-06T00:20:01.911634Z","iopub.status.idle":"2022-07-06T00:20:02.131923Z","shell.execute_reply.started":"2022-07-06T00:20:01.911598Z","shell.execute_reply":"2022-07-06T00:20:02.131046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp['Title'] = dfcomp['Name'].apply(lambda x: re.search(' ([A-Z][a-z]+)\\.', x).group(1))\ndfcomp.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:52:46.517589Z","iopub.execute_input":"2022-07-06T00:52:46.518050Z","iopub.status.idle":"2022-07-06T00:52:46.539875Z","shell.execute_reply.started":"2022-07-06T00:52:46.518011Z","shell.execute_reply":"2022-07-06T00:52:46.538749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Travelling Alone vs. Travelling in groups**","metadata":{}},{"cell_type":"code","source":"dfcomp['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:53:14.455197Z","iopub.execute_input":"2022-07-06T00:53:14.455582Z","iopub.status.idle":"2022-07-06T00:53:14.464402Z","shell.execute_reply.started":"2022-07-06T00:53:14.455553Z","shell.execute_reply":"2022-07-06T00:53:14.463539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp['Title'] = dfcomp['Title'].replace('Mlle', 'Miss')\ndfcomp['Title'] = dfcomp['Title'].replace('Mme', 'Mrs')\ndfcomp.loc[(~dfcomp['Title'].isin(['Mr', 'Mrs', 'Miss', 'Master'])), 'Title'] = 'Rare Title'\ndfcomp['Title'].unique()\nsns.countplot(data=dfcomp,x='Title');","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:54:36.081594Z","iopub.execute_input":"2022-07-06T00:54:36.082626Z","iopub.status.idle":"2022-07-06T00:54:36.291537Z","shell.execute_reply.started":"2022-07-06T00:54:36.082566Z","shell.execute_reply":"2022-07-06T00:54:36.290376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Family Size***","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['Fsize'] = dftitanic['SibSp'] + dftitanic['Parch']+1","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:02.792557Z","iopub.execute_input":"2022-07-06T00:20:02.793258Z","iopub.status.idle":"2022-07-06T00:20:02.799445Z","shell.execute_reply.started":"2022-07-06T00:20:02.793218Z","shell.execute_reply":"2022-07-06T00:20:02.798228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=dftitanic,x='Fsize',hue='Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:03.117716Z","iopub.execute_input":"2022-07-06T00:20:03.118127Z","iopub.status.idle":"2022-07-06T00:20:03.406386Z","shell.execute_reply.started":"2022-07-06T00:20:03.118092Z","shell.execute_reply":"2022-07-06T00:20:03.405178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic.groupby('Fsize')['Survived'].value_counts(normalize=True).reset_index(name='perc')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:03.408531Z","iopub.execute_input":"2022-07-06T00:20:03.409325Z","iopub.status.idle":"2022-07-06T00:20:03.423810Z","shell.execute_reply.started":"2022-07-06T00:20:03.409277Z","shell.execute_reply":"2022-07-06T00:20:03.422964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = dftitanic.groupby('Fsize')['Survived'].value_counts(normalize=True).reset_index(name='Perc')\n\nplt.figure(figsize=(15,6));\nsns.barplot(data=temp,x='Fsize', y='Perc', hue='Survived', dodge=True);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:03.855499Z","iopub.execute_input":"2022-07-06T00:20:03.856519Z","iopub.status.idle":"2022-07-06T00:20:04.187782Z","shell.execute_reply.started":"2022-07-06T00:20:03.856481Z","shell.execute_reply":"2022-07-06T00:20:04.186672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**How many people booked together?**\n\nApart from families, there could be groups of friends travelling together.","metadata":{}},{"cell_type":"code","source":"dftitanic['Ticket'].value_counts().head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:04.614544Z","iopub.execute_input":"2022-07-06T00:20:04.614941Z","iopub.status.idle":"2022-07-06T00:20:04.624704Z","shell.execute_reply.started":"2022-07-06T00:20:04.614909Z","shell.execute_reply":"2022-07-06T00:20:04.623746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['Ticket'].value_counts().reset_index().head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:05.114393Z","iopub.execute_input":"2022-07-06T00:20:05.115047Z","iopub.status.idle":"2022-07-06T00:20:05.127477Z","shell.execute_reply.started":"2022-07-06T00:20:05.114999Z","shell.execute_reply":"2022-07-06T00:20:05.126601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = dftitanic['Ticket'].value_counts().reset_index(name='Tsize')\ntemp","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:05.616663Z","iopub.execute_input":"2022-07-06T00:20:05.617470Z","iopub.status.idle":"2022-07-06T00:20:05.632805Z","shell.execute_reply.started":"2022-07-06T00:20:05.617431Z","shell.execute_reply":"2022-07-06T00:20:05.631611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic = dftitanic.merge(temp, left_on='Ticket', right_on='index',how='inner').drop('index', axis=1)\ndftitanic.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:06.093381Z","iopub.execute_input":"2022-07-06T00:20:06.094201Z","iopub.status.idle":"2022-07-06T00:20:06.125281Z","shell.execute_reply.started":"2022-07-06T00:20:06.094154Z","shell.execute_reply":"2022-07-06T00:20:06.124197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Tsize', hue='Survived', data=dftitanic);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:06.596828Z","iopub.execute_input":"2022-07-06T00:20:06.597237Z","iopub.status.idle":"2022-07-06T00:20:06.873516Z","shell.execute_reply.started":"2022-07-06T00:20:06.597204Z","shell.execute_reply":"2022-07-06T00:20:06.872244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = dftitanic.groupby('Tsize')['Survived'].value_counts(normalize=True).reset_index(name='Perc')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:07.141511Z","iopub.execute_input":"2022-07-06T00:20:07.141893Z","iopub.status.idle":"2022-07-06T00:20:07.151599Z","shell.execute_reply.started":"2022-07-06T00:20:07.141863Z","shell.execute_reply":"2022-07-06T00:20:07.150405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x='Tsize', y='Perc', hue='Survived', data=temp, dodge=True);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:07.581764Z","iopub.execute_input":"2022-07-06T00:20:07.582170Z","iopub.status.idle":"2022-07-06T00:20:07.866653Z","shell.execute_reply.started":"2022-07-06T00:20:07.582135Z","shell.execute_reply":"2022-07-06T00:20:07.865704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Consolidating family & friends**","metadata":{}},{"cell_type":"code","source":"dftitanic['Group'] = dftitanic[['Tsize', 'Fsize']].max(axis=1)\n\nplt.figure(figsize=(15,6));\nsns.countplot(x='Group', hue='Survived', data=dftitanic);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:08.350473Z","iopub.execute_input":"2022-07-06T00:20:08.351094Z","iopub.status.idle":"2022-07-06T00:20:08.666308Z","shell.execute_reply.started":"2022-07-06T00:20:08.351055Z","shell.execute_reply":"2022-07-06T00:20:08.664931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['GrpSize'] = ''\ndftitanic.loc[dftitanic['Group']==1, 'GrpSize'] = dftitanic.loc[dftitanic['Group']==1, 'GrpSize'].replace('', 'solo')\ndftitanic.loc[dftitanic['Group']==2, 'GrpSize'] = dftitanic.loc[dftitanic['Group']==2, 'GrpSize'].replace('', 'couple')\ndftitanic.loc[(dftitanic['Group']<=4) & (dftitanic['Group']>=3), 'GrpSize'] = dftitanic.loc[(dftitanic['Group']<=4) & (dftitanic['Group']>=3), 'GrpSize'].replace('', 'group')\ndftitanic.loc[dftitanic['Group']>4, 'GrpSize'] = dftitanic.loc[dftitanic['Group']>4, 'GrpSize'].replace('', 'large group')\ndftitanic.head()\ndftitanic['GrpSize'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:08.668117Z","iopub.execute_input":"2022-07-06T00:20:08.668436Z","iopub.status.idle":"2022-07-06T00:20:08.691584Z","shell.execute_reply.started":"2022-07-06T00:20:08.668408Z","shell.execute_reply":"2022-07-06T00:20:08.689987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6));\nsns.countplot(x='GrpSize', order=['solo', 'couple', 'group', 'large group'], hue='Survived', data=dftitanic);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:08.937876Z","iopub.execute_input":"2022-07-06T00:20:08.938280Z","iopub.status.idle":"2022-07-06T00:20:09.194120Z","shell.execute_reply.started":"2022-07-06T00:20:08.938246Z","shell.execute_reply":"2022-07-06T00:20:09.193183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fsize\ndfcomp['Fsize']=dfcomp['SibSp']+dfcomp['Parch']+1\n\n#Ticket Size\ntemp=dfcomp['Ticket'].value_counts().reset_index(name='Tsize')\ndfcomp=dfcomp.merge(temp, left_on='Ticket', right_on='index', how='inner').drop('index',axis=1)\n\n# Grouping\ndfcomp['Group']=dfcomp[['Tsize','Fsize']].max(axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:55:40.777165Z","iopub.execute_input":"2022-07-06T00:55:40.777940Z","iopub.status.idle":"2022-07-06T00:55:40.796701Z","shell.execute_reply.started":"2022-07-06T00:55:40.777891Z","shell.execute_reply":"2022-07-06T00:55:40.795562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp['GrpSize'] = ''\ndfcomp.loc[dfcomp['Group']==1, 'GrpSize'] = dfcomp.loc[dfcomp['Group']==1, 'GrpSize'].replace('', 'solo')\ndfcomp.loc[dfcomp['Group']==2, 'GrpSize'] = dfcomp.loc[dfcomp['Group']==2, 'GrpSize'].replace('', 'couple')\ndfcomp.loc[(dfcomp['Group']<=4) & (dfcomp['Group']>=3), 'GrpSize'] = dfcomp.loc[(dfcomp['Group']<=4) & (dfcomp['Group']>=3), 'GrpSize'].replace('', 'group')\ndfcomp.loc[dfcomp['Group']>4, 'GrpSize'] = dfcomp.loc[dfcomp['Group']>4, 'GrpSize'].replace('', 'large group')\ndfcomp.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:55:55.994623Z","iopub.execute_input":"2022-07-06T00:55:55.995030Z","iopub.status.idle":"2022-07-06T00:55:56.030464Z","shell.execute_reply.started":"2022-07-06T00:55:55.995000Z","shell.execute_reply":"2022-07-06T00:55:56.028823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Effect of Fare**","metadata":{}},{"cell_type":"code","source":"dftitanic['Fare'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:33.125715Z","iopub.execute_input":"2022-07-06T00:20:33.129466Z","iopub.status.idle":"2022-07-06T00:20:33.137748Z","shell.execute_reply.started":"2022-07-06T00:20:33.129414Z","shell.execute_reply":"2022-07-06T00:20:33.136531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(figsize=(15,6))\nsns.distplot(dftitanic['Fare']);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:34.307597Z","iopub.execute_input":"2022-07-06T00:20:34.308680Z","iopub.status.idle":"2022-07-06T00:20:34.683576Z","shell.execute_reply.started":"2022-07-06T00:20:34.308637Z","shell.execute_reply":"2022-07-06T00:20:34.682435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic[dftitanic['Fare'] < 0]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:35.988621Z","iopub.execute_input":"2022-07-06T00:20:35.989563Z","iopub.status.idle":"2022-07-06T00:20:36.003380Z","shell.execute_reply.started":"2022-07-06T00:20:35.989521Z","shell.execute_reply":"2022-07-06T00:20:36.002463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic[dftitanic['Fare'] == 0]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:37.020913Z","iopub.execute_input":"2022-07-06T00:20:37.021377Z","iopub.status.idle":"2022-07-06T00:20:37.051079Z","shell.execute_reply.started":"2022-07-06T00:20:37.021341Z","shell.execute_reply":"2022-07-06T00:20:37.050012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic.loc[(dftitanic['Fare'] == 0) & (dftitanic['Pclass'] == 1), 'Fare'] = dftitanic[dftitanic['Pclass'] == 1]['Fare'].median()\ndftitanic.loc[(dftitanic['Fare'] == 0) & (dftitanic['Pclass'] == 2), 'Fare'] = dftitanic[dftitanic['Pclass'] == 2]['Fare'].median()\ndftitanic.loc[(dftitanic['Fare'] == 0) & (dftitanic['Pclass'] == 3), 'Fare'] = dftitanic[dftitanic['Pclass'] == 3]['Fare'].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:37.730496Z","iopub.execute_input":"2022-07-06T00:20:37.730880Z","iopub.status.idle":"2022-07-06T00:20:37.747829Z","shell.execute_reply.started":"2022-07-06T00:20:37.730847Z","shell.execute_reply":"2022-07-06T00:20:37.746974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic[dftitanic['Fare']==0]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:38.964052Z","iopub.execute_input":"2022-07-06T00:20:38.964853Z","iopub.status.idle":"2022-07-06T00:20:38.978081Z","shell.execute_reply.started":"2022-07-06T00:20:38.964810Z","shell.execute_reply":"2022-07-06T00:20:38.976778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Assigning categories to fare","metadata":{}},{"cell_type":"code","source":"dftitanic['FareCat'] = ''\ndftitanic.loc[dftitanic['Fare']<=10, 'FareCat'] = '0-10'\ndftitanic.loc[(dftitanic['Fare']>10) & (dftitanic['Fare']<=25), 'FareCat'] = '10-25'\ndftitanic.loc[(dftitanic['Fare']>25) & (dftitanic['Fare']<=40), 'FareCat'] = '25-40'\ndftitanic.loc[(dftitanic['Fare']>40) & (dftitanic['Fare']<=70), 'FareCat'] = '40-70'\ndftitanic.loc[(dftitanic['Fare']>70) & (dftitanic['Fare']<=100), 'FareCat'] = '70-100'\ndftitanic.loc[dftitanic['Fare']>100, 'FareCat'] = '100+'\ndftitanic[['Fare', 'FareCat']].head()\ndftitanic['FareCat'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:41.516517Z","iopub.execute_input":"2022-07-06T00:20:41.517289Z","iopub.status.idle":"2022-07-06T00:20:41.537553Z","shell.execute_reply.started":"2022-07-06T00:20:41.517236Z","shell.execute_reply":"2022-07-06T00:20:41.536677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(figsize=(15,4))\nsns.countplot(x='FareCat', order=['0-10', '10-25', '25-40', '40-70', '70-100', '100+'], hue='Survived', data=dftitanic);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:42.956104Z","iopub.execute_input":"2022-07-06T00:20:42.956744Z","iopub.status.idle":"2022-07-06T00:20:43.205832Z","shell.execute_reply.started":"2022-07-06T00:20:42.956707Z","shell.execute_reply":"2022-07-06T00:20:43.204710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp[dfcomp['Fare'] == 0]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:57:10.041550Z","iopub.execute_input":"2022-07-06T00:57:10.042838Z","iopub.status.idle":"2022-07-06T00:57:10.064214Z","shell.execute_reply.started":"2022-07-06T00:57:10.042777Z","shell.execute_reply":"2022-07-06T00:57:10.063149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write the code to find the median\ndfcomp.loc[(dfcomp['Fare'] == 0) & (dfcomp['Pclass'] == 1), 'Fare'] = dfcomp[dfcomp['Pclass'] == 1]['Fare'].median()\ndfcomp.loc[(dfcomp['Fare'] == 0) & (dfcomp['Pclass'] == 2), 'Fare'] = dfcomp[dfcomp['Pclass'] == 2]['Fare'].median()\ndfcomp.loc[(dfcomp['Fare'] == 0) & (dfcomp['Pclass'] == 3), 'Fare'] = dfcomp[dfcomp['Pclass'] == 3]['Fare'].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:57:10.737533Z","iopub.execute_input":"2022-07-06T00:57:10.738722Z","iopub.status.idle":"2022-07-06T00:57:10.752917Z","shell.execute_reply.started":"2022-07-06T00:57:10.738681Z","shell.execute_reply":"2022-07-06T00:57:10.751899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Write the code for Assigning Categories\ndfcomp['FareCat'] = ''\ndfcomp.loc[dfcomp['Fare']<=10, 'FareCat'] = '0-10'\ndfcomp.loc[(dfcomp['Fare']>10) & (dfcomp['Fare']<=25), 'FareCat'] = '10-25'\ndfcomp.loc[(dfcomp['Fare']>25) & (dfcomp['Fare']<=40), 'FareCat'] = '25-40'\ndfcomp.loc[(dfcomp['Fare']>40) & (dfcomp['Fare']<=70), 'FareCat'] = '40-70'\ndfcomp.loc[(dfcomp['Fare']>70) & (dfcomp['Fare']<=100), 'FareCat'] = '70-100'\ndfcomp.loc[dfcomp['Fare']>100, 'FareCat'] = '100+'\n\n\n\ndfcomp[['Fare', 'FareCat']].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:57:33.161249Z","iopub.execute_input":"2022-07-06T00:57:33.161659Z","iopub.status.idle":"2022-07-06T00:57:33.184867Z","shell.execute_reply.started":"2022-07-06T00:57:33.161625Z","shell.execute_reply":"2022-07-06T00:57:33.184039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Effect of Embarked variable**","metadata":{}},{"cell_type":"code","source":"plt.subplots(figsize=(12,4))\nsns.countplot(x='Embarked',hue='Survived', data=dftitanic);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:45.940982Z","iopub.execute_input":"2022-07-06T00:20:45.941723Z","iopub.status.idle":"2022-07-06T00:20:46.152309Z","shell.execute_reply.started":"2022-07-06T00:20:45.941670Z","shell.execute_reply":"2022-07-06T00:20:46.151196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Missing Values**\n\n***Age***","metadata":{}},{"cell_type":"code","source":"sns.kdeplot(dftitanic[dftitanic['Survived'] == 0]['Age'].dropna(), shade=True,label=\"Not survived\");\nsns.kdeplot(dftitanic[dftitanic['Survived'] == 1]['Age'].dropna(), shade=True,label=\"Survived\");","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:47.891928Z","iopub.execute_input":"2022-07-06T00:20:47.892376Z","iopub.status.idle":"2022-07-06T00:20:48.145686Z","shell.execute_reply.started":"2022-07-06T00:20:47.892343Z","shell.execute_reply":"2022-07-06T00:20:48.143532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = dftitanic[dftitanic['Age'].isnull() == False]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:48.728674Z","iopub.execute_input":"2022-07-06T00:20:48.729128Z","iopub.status.idle":"2022-07-06T00:20:48.736497Z","shell.execute_reply.started":"2022-07-06T00:20:48.729091Z","shell.execute_reply":"2022-07-06T00:20:48.735006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_context('poster')\nsns.factorplot(kind='box', x='Age', col='Title', row='Pclass', data=temp);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:49.631772Z","iopub.execute_input":"2022-07-06T00:20:49.632198Z","iopub.status.idle":"2022-07-06T00:20:52.168588Z","shell.execute_reply.started":"2022-07-06T00:20:49.632163Z","shell.execute_reply":"2022-07-06T00:20:52.167415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for t in dftitanic['Title'].unique():\n    for p in dftitanic['Pclass'].unique():\n        dftitanic.loc[(dftitanic['Title'] == t) & (dftitanic['Pclass'] == p) & (dftitanic['Age'].isnull()), 'Age'] = dftitanic.loc[(dftitanic['Title'] == t) & (dftitanic['Pclass'] == p), 'Age'].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:52.171104Z","iopub.execute_input":"2022-07-06T00:20:52.171672Z","iopub.status.idle":"2022-07-06T00:20:52.211992Z","shell.execute_reply.started":"2022-07-06T00:20:52.171621Z","shell.execute_reply":"2022-07-06T00:20:52.210968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(figsize=(10,6))\nsns.kdeplot(dftitanic[dftitanic['Survived'] == 0]['Age'], shade=True,label='Not Survived');\nsns.kdeplot(dftitanic[dftitanic['Survived'] == 1]['Age'], shade=True,label='Survived');","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:52.213361Z","iopub.execute_input":"2022-07-06T00:20:52.213690Z","iopub.status.idle":"2022-07-06T00:20:52.479473Z","shell.execute_reply.started":"2022-07-06T00:20:52.213659Z","shell.execute_reply":"2022-07-06T00:20:52.478436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:52.481673Z","iopub.execute_input":"2022-07-06T00:20:52.482013Z","iopub.status.idle":"2022-07-06T00:20:52.490588Z","shell.execute_reply.started":"2022-07-06T00:20:52.481983Z","shell.execute_reply":"2022-07-06T00:20:52.489434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['AgeCat']=''\ndftitanic.loc[ dftitanic['Age'] <= 16, 'AgeCat'] = '0-16'\ndftitanic.loc[(dftitanic['Age'] > 16) & (dftitanic['Age'] <= 32), 'AgeCat'] = '16-32'\ndftitanic.loc[(dftitanic['Age'] > 32) & (dftitanic['Age'] <= 48), 'AgeCat'] = '32-48'\ndftitanic.loc[(dftitanic['Age'] > 48) & (dftitanic['Age'] <= 64), 'AgeCat'] = '48-64'\ndftitanic.loc[ dftitanic['Age'] > 64, 'AgeCat']= '64+'","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:52.491966Z","iopub.execute_input":"2022-07-06T00:20:52.492327Z","iopub.status.idle":"2022-07-06T00:20:52.508224Z","shell.execute_reply.started":"2022-07-06T00:20:52.492296Z","shell.execute_reply":"2022-07-06T00:20:52.507010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Embarkment and null values**","metadata":{}},{"cell_type":"code","source":"dftitanic.describe(include='O')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:53.788681Z","iopub.execute_input":"2022-07-06T00:20:53.789152Z","iopub.status.idle":"2022-07-06T00:20:53.825240Z","shell.execute_reply.started":"2022-07-06T00:20:53.789112Z","shell.execute_reply":"2022-07-06T00:20:53.823624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['Embarked'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:54.062708Z","iopub.execute_input":"2022-07-06T00:20:54.063113Z","iopub.status.idle":"2022-07-06T00:20:54.071509Z","shell.execute_reply.started":"2022-07-06T00:20:54.063079Z","shell.execute_reply":"2022-07-06T00:20:54.070304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic.loc[(dftitanic['Embarked'].isnull()),'Embarked']='S'","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:54.319142Z","iopub.execute_input":"2022-07-06T00:20:54.319565Z","iopub.status.idle":"2022-07-06T00:20:54.326748Z","shell.execute_reply.started":"2022-07-06T00:20:54.319530Z","shell.execute_reply":"2022-07-06T00:20:54.325316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic['Embarked'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:21:03.805537Z","iopub.execute_input":"2022-07-06T00:21:03.806274Z","iopub.status.idle":"2022-07-06T00:21:03.814092Z","shell.execute_reply.started":"2022-07-06T00:21:03.806231Z","shell.execute_reply":"2022-07-06T00:21:03.812912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for t in dfcomp['Title'].unique():\n    for p in dfcomp['Pclass'].unique():\n        dfcomp.loc[(dfcomp['Title'] == t) & (dfcomp['Pclass'] == p) & (dfcomp['Age'].isnull()), 'Age'] = dfcomp.loc[(dfcomp['Title'] == t) & (dfcomp['Pclass'] == p), 'Age'].median()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:00:23.027323Z","iopub.execute_input":"2022-07-06T01:00:23.027781Z","iopub.status.idle":"2022-07-06T01:00:23.069751Z","shell.execute_reply.started":"2022-07-06T01:00:23.027744Z","shell.execute_reply":"2022-07-06T01:00:23.068518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:00:41.576222Z","iopub.execute_input":"2022-07-06T01:00:41.576646Z","iopub.status.idle":"2022-07-06T01:00:41.585584Z","shell.execute_reply.started":"2022-07-06T01:00:41.576611Z","shell.execute_reply":"2022-07-06T01:00:41.584176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp.loc[(dfcomp['Age'].isnull()),'Pclass':'Title'] ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:00:51.895243Z","iopub.execute_input":"2022-07-06T01:00:51.895775Z","iopub.status.idle":"2022-07-06T01:00:51.920211Z","shell.execute_reply.started":"2022-07-06T01:00:51.895731Z","shell.execute_reply":"2022-07-06T01:00:51.919000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp.loc[(dfcomp['Age'].isnull()) & (dfcomp['Pclass'] == 3) & (dfcomp['Title'] == 'Rare Title'), 'Age'] = dfcomp.loc[(dfcomp['Title'] == 'Rare Title') & (dfcomp['Pclass'] == 3), 'Age'].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:01:08.664860Z","iopub.execute_input":"2022-07-06T01:01:08.665380Z","iopub.status.idle":"2022-07-06T01:01:08.677925Z","shell.execute_reply.started":"2022-07-06T01:01:08.665337Z","shell.execute_reply":"2022-07-06T01:01:08.676655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Write the code to check if there is any null value \ndfcomp['Fare'].isnull().sum()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:01:22.959930Z","iopub.execute_input":"2022-07-06T01:01:22.960482Z","iopub.status.idle":"2022-07-06T01:01:22.969232Z","shell.execute_reply.started":"2022-07-06T01:01:22.960439Z","shell.execute_reply":"2022-07-06T01:01:22.968325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Write code to check the pclass and title for which the null value exists\n\ndfcomp.loc[(dfcomp['Fare'].isnull()),'Pclass':'Title']","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:01:36.276556Z","iopub.execute_input":"2022-07-06T01:01:36.277075Z","iopub.status.idle":"2022-07-06T01:01:36.299366Z","shell.execute_reply.started":"2022-07-06T01:01:36.277026Z","shell.execute_reply":"2022-07-06T01:01:36.298120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Write code to fill in the missing value\ndfcomp.loc[(dfcomp['Fare'].isnull()) & (dfcomp['Pclass']==3) & (dfcomp['Title']=='Mr'), 'Fare']= dfcomp.loc[(dfcomp['Title']=='Mr') & (dfcomp['Pclass']==3), 'Fare'].median()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:01:53.760082Z","iopub.execute_input":"2022-07-06T01:01:53.760462Z","iopub.status.idle":"2022-07-06T01:01:53.770229Z","shell.execute_reply.started":"2022-07-06T01:01:53.760433Z","shell.execute_reply":"2022-07-06T01:01:53.769302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp.loc[dfcomp['Fare']<=10, 'FareCat'] = '0-10'","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:02:09.814633Z","iopub.execute_input":"2022-07-06T01:02:09.815025Z","iopub.status.idle":"2022-07-06T01:02:09.822177Z","shell.execute_reply.started":"2022-07-06T01:02:09.814993Z","shell.execute_reply":"2022-07-06T01:02:09.820732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp['AgeCat']=''\ndfcomp.loc[dfcomp['Age'] <=16, 'AgeCat'] = '0-16'\ndfcomp.loc[(dfcomp['Age'] > 16) & (dfcomp['AgeCat'] <= '32'), 'AgeCat'] = '16-32'\ndfcomp.loc[(dfcomp['Age'] > 32) & (dfcomp['AgeCat'] <= '48'), 'AgeCat'] = '32-48'\ndfcomp.loc[(dfcomp['Age'] > 48) & (dfcomp['AgeCat'] <= '64'), 'AgeCat'] = '48-64'\ndfcomp.loc[dfcomp['Age'] > 64, 'AgeCat']= '64+'","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:02:26.715890Z","iopub.execute_input":"2022-07-06T01:02:26.716289Z","iopub.status.idle":"2022-07-06T01:02:26.730137Z","shell.execute_reply.started":"2022-07-06T01:02:26.716260Z","shell.execute_reply":"2022-07-06T01:02:26.729046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp['Embarked'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:02:57.619913Z","iopub.execute_input":"2022-07-06T01:02:57.620743Z","iopub.status.idle":"2022-07-06T01:02:57.629056Z","shell.execute_reply.started":"2022-07-06T01:02:57.620704Z","shell.execute_reply":"2022-07-06T01:02:57.627903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dropping Cabin**\n\nDefining the cabin type","metadata":{}},{"cell_type":"code","source":"dftitanic['CabinType'] = dftitanic['Cabin'].str[0]\nplt.figure(figsize=(15,6))\nsns.countplot(x='CabinType', hue='Survived', data=dftitanic);","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:55.103935Z","iopub.execute_input":"2022-07-06T00:20:55.104396Z","iopub.status.idle":"2022-07-06T00:20:55.398905Z","shell.execute_reply.started":"2022-07-06T00:20:55.104354Z","shell.execute_reply":"2022-07-06T00:20:55.397628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic.groupby(['CabinType', 'Pclass'])['Pclass'].count()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:20:55.400619Z","iopub.execute_input":"2022-07-06T00:20:55.400970Z","iopub.status.idle":"2022-07-06T00:20:55.413821Z","shell.execute_reply.started":"2022-07-06T00:20:55.400925Z","shell.execute_reply":"2022-07-06T00:20:55.412628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic.drop('Cabin',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:21:21.151144Z","iopub.execute_input":"2022-07-06T00:21:21.151551Z","iopub.status.idle":"2022-07-06T00:21:21.158408Z","shell.execute_reply.started":"2022-07-06T00:21:21.151517Z","shell.execute_reply":"2022-07-06T00:21:21.157597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp.drop('Cabin',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:03:37.929255Z","iopub.execute_input":"2022-07-06T01:03:37.929700Z","iopub.status.idle":"2022-07-06T01:03:37.936832Z","shell.execute_reply.started":"2022-07-06T01:03:37.929666Z","shell.execute_reply":"2022-07-06T01:03:37.935926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check missing values in dftitanic","metadata":{}},{"cell_type":"code","source":"dftitanic.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:21:28.912704Z","iopub.execute_input":"2022-07-06T00:21:28.913170Z","iopub.status.idle":"2022-07-06T00:21:28.925504Z","shell.execute_reply.started":"2022-07-06T00:21:28.913131Z","shell.execute_reply":"2022-07-06T00:21:28.924418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Feature Selection**","metadata":{}},{"cell_type":"code","source":"sns.set_context('poster')\nsns.set_context('poster')\nplt.figure(figsize=(15,6))\ncor = dftitanic.drop('PassengerId',axis=1).corr()\nsns.heatmap(cor, annot=True, fmt='.1g');","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:22:27.022583Z","iopub.execute_input":"2022-07-06T00:22:27.023489Z","iopub.status.idle":"2022-07-06T00:22:27.674606Z","shell.execute_reply.started":"2022-07-06T00:22:27.023443Z","shell.execute_reply":"2022-07-06T00:22:27.673757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_features = ['Name','Age','Fare','Ticket','Fsize','Tsize','Group']","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:22:33.933964Z","iopub.execute_input":"2022-07-06T00:22:33.934595Z","iopub.status.idle":"2022-07-06T00:22:33.939337Z","shell.execute_reply.started":"2022-07-06T00:22:33.934563Z","shell.execute_reply":"2022-07-06T00:22:33.938541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftitanic.to_csv('titanic_clean.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:22:35.478890Z","iopub.execute_input":"2022-07-06T00:22:35.479462Z","iopub.status.idle":"2022-07-06T00:22:35.493854Z","shell.execute_reply.started":"2022-07-06T00:22:35.479431Z","shell.execute_reply":"2022-07-06T00:22:35.493092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Write the code for heat map here\nsns.set_context('poster')\nplt.figure(figsize=(15,6))\ncor = dfcomp.drop('PassengerId',axis=1).corr()\nsns.heatmap(cor, annot=True, fmt='.1g');\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:04:21.619753Z","iopub.execute_input":"2022-07-06T01:04:21.620185Z","iopub.status.idle":"2022-07-06T01:04:22.150796Z","shell.execute_reply.started":"2022-07-06T01:04:21.620149Z","shell.execute_reply":"2022-07-06T01:04:22.149699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_features = ['Name','Age','Fare','Ticket','Fsize','Tsize','Group']\ndfcomp.drop(drop_features,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:04:46.211777Z","iopub.execute_input":"2022-07-06T01:04:46.212230Z","iopub.status.idle":"2022-07-06T01:04:46.219763Z","shell.execute_reply.started":"2022-07-06T01:04:46.212194Z","shell.execute_reply":"2022-07-06T01:04:46.218572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfcomp.to_csv('competetion_clean.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:05:09.657644Z","iopub.execute_input":"2022-07-06T01:05:09.658442Z","iopub.status.idle":"2022-07-06T01:05:09.666649Z","shell.execute_reply.started":"2022-07-06T01:05:09.658396Z","shell.execute_reply":"2022-07-06T01:05:09.665608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. # Advanced Classification","metadata":{}},{"cell_type":"markdown","source":"In this notebook, we will cover:\n\n* Decision Tree Classifier\n* Random Forest Classifier\n* XGBoost Classifier\n* AdaBoost\n* Hyperparameter Tuning by GridSearchCV\n* Tuning by Randomize Search\n* Voting Classifier","metadata":{}},{"cell_type":"markdown","source":"Importing all necessary packages","metadata":{}},{"cell_type":"code","source":"!pip install xgboost","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:23:23.350787Z","iopub.execute_input":"2022-07-06T00:23:23.351230Z","iopub.status.idle":"2022-07-06T00:23:35.661073Z","shell.execute_reply.started":"2022-07-06T00:23:23.351195Z","shell.execute_reply":"2022-07-06T00:23:35.659693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:26:03.722394Z","iopub.execute_input":"2022-07-06T00:26:03.722844Z","iopub.status.idle":"2022-07-06T00:26:03.736029Z","shell.execute_reply.started":"2022-07-06T00:26:03.722806Z","shell.execute_reply":"2022-07-06T00:26:03.734756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 1: Load Data 'titanic_clean.csv'","metadata":{}},{"cell_type":"code","source":"#write code here\ndata = pd.read_csv('../input/d/umairaslam/titanic/titanic_clean.csv')\ndf = data.copy()\ndf.head()\ndf.dtypes","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:34:04.264788Z","iopub.execute_input":"2022-07-06T00:34:04.265223Z","iopub.status.idle":"2022-07-06T00:34:04.290730Z","shell.execute_reply.started":"2022-07-06T00:34:04.265191Z","shell.execute_reply":"2022-07-06T00:34:04.289427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 2: Do One Hot encoding for categorical varaibles and store in df","metadata":{}},{"cell_type":"code","source":"#write code here\ndf_OneHot=pd.get_dummies(df, columns=['Pclass','Sex','Embarked','Title','GrpSize', 'FareCat', 'AgeCat'])\ndf_OneHot.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:06.887735Z","iopub.execute_input":"2022-07-06T00:34:06.888632Z","iopub.status.idle":"2022-07-06T00:34:06.917633Z","shell.execute_reply.started":"2022-07-06T00:34:06.888571Z","shell.execute_reply":"2022-07-06T00:34:06.916867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Write code here to copy into df\ndf=df_OneHot.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:07.935668Z","iopub.execute_input":"2022-07-06T00:34:07.936670Z","iopub.status.idle":"2022-07-06T00:34:07.943270Z","shell.execute_reply.started":"2022-07-06T00:34:07.936619Z","shell.execute_reply":"2022-07-06T00:34:07.942059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Defining function to plot ROC AUC Curve","metadata":{}},{"cell_type":"code","source":"def plot_roc_curve(fpr, tpr, label=None):\n    plt.figure(figsize=(8,6))\n    plt.title('ROC Curve')\n    plt.plot(fpr, tpr, linewidth=2, label=label)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.axis([-0.005, 1, 0, 1.005])\n    plt.xticks(np.arange(0,1, 0.05), rotation=90)\n    plt.xlabel(\"False Positive Rate\")\n    plt.ylabel(\"True Positive Rate\")\n    plt.legend(loc='best')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:08.509665Z","iopub.execute_input":"2022-07-06T00:34:08.510095Z","iopub.status.idle":"2022-07-06T00:34:08.518106Z","shell.execute_reply.started":"2022-07-06T00:34:08.510055Z","shell.execute_reply":"2022-07-06T00:34:08.517032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import auc,roc_curve,roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:08.668684Z","iopub.execute_input":"2022-07-06T00:34:08.669124Z","iopub.status.idle":"2022-07-06T00:34:08.674002Z","shell.execute_reply.started":"2022-07-06T00:34:08.669088Z","shell.execute_reply":"2022-07-06T00:34:08.673097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 3: Create dependent and independent varaibles","metadata":{}},{"cell_type":"code","source":"#write code here\nX = df.drop(['PassengerId','Survived'], axis=1)\nY = df['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:09.162968Z","iopub.execute_input":"2022-07-06T00:34:09.163690Z","iopub.status.idle":"2022-07-06T00:34:09.170309Z","shell.execute_reply.started":"2022-07-06T00:34:09.163657Z","shell.execute_reply":"2022-07-06T00:34:09.169271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 4: Split the data into training and testing set and set the random state to 100","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:11.811649Z","iopub.execute_input":"2022-07-06T00:34:11.812561Z","iopub.status.idle":"2022-07-06T00:34:11.817087Z","shell.execute_reply.started":"2022-07-06T00:34:11.812523Z","shell.execute_reply":"2022-07-06T00:34:11.815921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Write code here \nxtrain, xtest, ytrain, ytest=train_test_split(X,Y, test_size=0.3, random_state=100, shuffle=True)\nprint(xtrain.shape, ytrain.shape)\nprint(xtest.shape, ytest.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:12.369524Z","iopub.execute_input":"2022-07-06T00:34:12.369893Z","iopub.status.idle":"2022-07-06T00:34:12.379217Z","shell.execute_reply.started":"2022-07-06T00:34:12.369863Z","shell.execute_reply":"2022-07-06T00:34:12.377996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ytest","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:12.949427Z","iopub.execute_input":"2022-07-06T00:34:12.950066Z","iopub.status.idle":"2022-07-06T00:34:12.959634Z","shell.execute_reply.started":"2022-07-06T00:34:12.950030Z","shell.execute_reply":"2022-07-06T00:34:12.958522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"## Defining Decision Tree classifier","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:14.514648Z","iopub.execute_input":"2022-07-06T00:34:14.515573Z","iopub.status.idle":"2022-07-06T00:34:14.519756Z","shell.execute_reply.started":"2022-07-06T00:34:14.515530Z","shell.execute_reply":"2022-07-06T00:34:14.518939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_dt = DecisionTreeClassifier(max_depth=3,criterion='gini',random_state=100)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:14.883606Z","iopub.execute_input":"2022-07-06T00:34:14.884582Z","iopub.status.idle":"2022-07-06T00:34:14.890224Z","shell.execute_reply.started":"2022-07-06T00:34:14.884535Z","shell.execute_reply":"2022-07-06T00:34:14.888913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_dt.fit(xtrain, ytrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:15.317760Z","iopub.execute_input":"2022-07-06T00:34:15.318202Z","iopub.status.idle":"2022-07-06T00:34:15.335358Z","shell.execute_reply.started":"2022-07-06T00:34:15.318164Z","shell.execute_reply":"2022-07-06T00:34:15.333913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt_pred = clf_dt.predict(xtest)\ndt_pred_prb=clf_dt.predict_proba(xtest)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:15.892463Z","iopub.execute_input":"2022-07-06T00:34:15.893493Z","iopub.status.idle":"2022-07-06T00:34:15.904969Z","shell.execute_reply.started":"2022-07-06T00:34:15.893440Z","shell.execute_reply":"2022-07-06T00:34:15.904148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 5: Find accuracy for Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:18.814127Z","iopub.execute_input":"2022-07-06T00:34:18.815088Z","iopub.status.idle":"2022-07-06T00:34:18.820933Z","shell.execute_reply.started":"2022-07-06T00:34:18.815035Z","shell.execute_reply":"2022-07-06T00:34:18.820012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write code here \naccuracy_dt = accuracy_score(ytest, dt_pred)\nprint(\"Accuracy: {}\".format(accuracy_dt))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:19.111079Z","iopub.execute_input":"2022-07-06T00:34:19.111478Z","iopub.status.idle":"2022-07-06T00:34:19.118456Z","shell.execute_reply.started":"2022-07-06T00:34:19.111445Z","shell.execute_reply":"2022-07-06T00:34:19.117279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 6: Find precision for Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import precision_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:19.525863Z","iopub.execute_input":"2022-07-06T00:34:19.526807Z","iopub.status.idle":"2022-07-06T00:34:19.530823Z","shell.execute_reply.started":"2022-07-06T00:34:19.526766Z","shell.execute_reply":"2022-07-06T00:34:19.530061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write code here \nprecision_dt=precision_score(ytest, dt_pred)\nprint(\"Precision: {}\".format(precision_dt))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:19.852559Z","iopub.execute_input":"2022-07-06T00:34:19.852990Z","iopub.status.idle":"2022-07-06T00:34:19.862392Z","shell.execute_reply.started":"2022-07-06T00:34:19.852929Z","shell.execute_reply":"2022-07-06T00:34:19.861250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 7: Find Recall for Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import recall_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:20.154033Z","iopub.execute_input":"2022-07-06T00:34:20.154772Z","iopub.status.idle":"2022-07-06T00:34:20.159363Z","shell.execute_reply.started":"2022-07-06T00:34:20.154737Z","shell.execute_reply":"2022-07-06T00:34:20.158194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write code here \nrecall_dt = recall_score(ytest, dt_pred)\nprint(\"Recall: {}\".format(recall_dt))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:20.488441Z","iopub.execute_input":"2022-07-06T00:34:20.489233Z","iopub.status.idle":"2022-07-06T00:34:20.497257Z","shell.execute_reply.started":"2022-07-06T00:34:20.489195Z","shell.execute_reply":"2022-07-06T00:34:20.496209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 8: Find F1 Score for Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import f1_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:20.838563Z","iopub.execute_input":"2022-07-06T00:34:20.839312Z","iopub.status.idle":"2022-07-06T00:34:20.843578Z","shell.execute_reply.started":"2022-07-06T00:34:20.839276Z","shell.execute_reply":"2022-07-06T00:34:20.842711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write code here \ndt_f1=f1_score(ytest,dt_pred)\nprint(\"F1 Score: {}\".format(dt_f1))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:21.135888Z","iopub.execute_input":"2022-07-06T00:34:21.136663Z","iopub.status.idle":"2022-07-06T00:34:21.148278Z","shell.execute_reply.started":"2022-07-06T00:34:21.136621Z","shell.execute_reply":"2022-07-06T00:34:21.147177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 9: Print Classification Report for Decision Tree","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:21.501587Z","iopub.execute_input":"2022-07-06T00:34:21.502259Z","iopub.status.idle":"2022-07-06T00:34:21.506835Z","shell.execute_reply.started":"2022-07-06T00:34:21.502220Z","shell.execute_reply":"2022-07-06T00:34:21.505755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write code here \nprint(classification_report(ytest,dt_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:21.939906Z","iopub.execute_input":"2022-07-06T00:34:21.940577Z","iopub.status.idle":"2022-07-06T00:34:21.950614Z","shell.execute_reply.started":"2022-07-06T00:34:21.940541Z","shell.execute_reply":"2022-07-06T00:34:21.949645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ROC Curve<br>\nDefining function to plot ROC AUC Curve","metadata":{}},{"cell_type":"code","source":"def plot_roc_curve(fpr, tpr, label=None):\n    plt.figure(figsize=(8,6))\n    plt.title('ROC Curve')\n    plt.plot(fpr, tpr, linewidth=2, label=label)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.axis([-0.005, 1, 0, 1.005])\n    plt.xticks(np.arange(0,1, 0.05), rotation=90)\n    plt.xlabel(\"False Positive Rate\")\n    plt.ylabel(\"True Positive Rate\")\n    plt.legend(loc='best')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:22.441410Z","iopub.execute_input":"2022-07-06T00:34:22.442125Z","iopub.status.idle":"2022-07-06T00:34:22.449834Z","shell.execute_reply.started":"2022-07-06T00:34:22.442087Z","shell.execute_reply":"2022-07-06T00:34:22.448421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import auc,roc_curve,roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:22.753095Z","iopub.execute_input":"2022-07-06T00:34:22.753752Z","iopub.status.idle":"2022-07-06T00:34:22.758325Z","shell.execute_reply.started":"2022-07-06T00:34:22.753717Z","shell.execute_reply":"2022-07-06T00:34:22.757236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_context('poster')\nauc_dt=roc_auc_score(ytest,dt_pred_prb)\nfpr,tpr,threshold=roc_curve(ytest,dt_pred_prb)\nplot_roc_curve(fpr,tpr,label='AUC = %0.3f'% auc_dt)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:34:23.202661Z","iopub.execute_input":"2022-07-06T00:34:23.203063Z","iopub.status.idle":"2022-07-06T00:34:23.546623Z","shell.execute_reply.started":"2022-07-06T00:34:23.203029Z","shell.execute_reply":"2022-07-06T00:34:23.545513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Features Importance","metadata":{}},{"cell_type":"code","source":"clf_dt.feature_importances_","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:34.911666Z","iopub.execute_input":"2022-07-06T00:35:34.912148Z","iopub.status.idle":"2022-07-06T00:35:34.920139Z","shell.execute_reply.started":"2022-07-06T00:35:34.912109Z","shell.execute_reply":"2022-07-06T00:35:34.919139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_tuple=list(zip(X.columns,clf_dt.feature_importances_))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:36.127739Z","iopub.execute_input":"2022-07-06T00:35:36.129009Z","iopub.status.idle":"2022-07-06T00:35:36.134966Z","shell.execute_reply.started":"2022-07-06T00:35:36.128921Z","shell.execute_reply":"2022-07-06T00:35:36.133801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_tuple","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:36.666911Z","iopub.execute_input":"2022-07-06T00:35:36.667712Z","iopub.status.idle":"2022-07-06T00:35:36.678299Z","shell.execute_reply.started":"2022-07-06T00:35:36.667655Z","shell.execute_reply":"2022-07-06T00:35:36.677316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp=pd.DataFrame(features_tuple,columns=[\"Feature Names\",\"Importance\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:37.026477Z","iopub.execute_input":"2022-07-06T00:35:37.027225Z","iopub.status.idle":"2022-07-06T00:35:37.033676Z","shell.execute_reply.started":"2022-07-06T00:35:37.027171Z","shell.execute_reply":"2022-07-06T00:35:37.032620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp=feature_imp.sort_values(\"Importance\",ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:37.421122Z","iopub.execute_input":"2022-07-06T00:35:37.422199Z","iopub.status.idle":"2022-07-06T00:35:37.428650Z","shell.execute_reply.started":"2022-07-06T00:35:37.422152Z","shell.execute_reply":"2022-07-06T00:35:37.427795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsns.barplot(x=\"Feature Names\",y=\"Importance\", data=feature_imp, color='b')\nplt.xlabel(\"Titanic Features\")\nplt.ylabel(\"Importance\")\nplt.xticks(rotation=90)\nplt.title(\"Decision Classifier - Features Importance\")","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:37.803506Z","iopub.execute_input":"2022-07-06T00:35:37.804829Z","iopub.status.idle":"2022-07-06T00:35:38.514213Z","shell.execute_reply.started":"2022-07-06T00:35:37.804780Z","shell.execute_reply":"2022-07-06T00:35:38.513085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest","metadata":{}},{"cell_type":"markdown","source":"### Defining Random Forest classifier","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:39.806728Z","iopub.execute_input":"2022-07-06T00:35:39.807158Z","iopub.status.idle":"2022-07-06T00:35:39.864138Z","shell.execute_reply.started":"2022-07-06T00:35:39.807125Z","shell.execute_reply":"2022-07-06T00:35:39.863220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_rf = RandomForestClassifier(random_state=100)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:40.650916Z","iopub.execute_input":"2022-07-06T00:35:40.651354Z","iopub.status.idle":"2022-07-06T00:35:40.656291Z","shell.execute_reply.started":"2022-07-06T00:35:40.651319Z","shell.execute_reply":"2022-07-06T00:35:40.655213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_rf.fit(xtrain, ytrain)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:35:41.204698Z","iopub.execute_input":"2022-07-06T00:35:41.205835Z","iopub.status.idle":"2022-07-06T00:35:41.417147Z","shell.execute_reply.started":"2022-07-06T00:35:41.205794Z","shell.execute_reply":"2022-07-06T00:35:41.416027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_pred = clf_rf.predict(xtest)\nrf_pred_prb=clf_rf.predict_proba(xtest)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:41.571765Z","iopub.execute_input":"2022-07-06T00:35:41.572485Z","iopub.status.idle":"2022-07-06T00:35:41.619867Z","shell.execute_reply.started":"2022-07-06T00:35:41.572447Z","shell.execute_reply":"2022-07-06T00:35:41.618924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 10: Find Precision for Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \nprecision_rf=precision_score(ytest, rf_pred)\nprint(\"Precision: {}\".format(precision_rf))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:41.940146Z","iopub.execute_input":"2022-07-06T00:35:41.940515Z","iopub.status.idle":"2022-07-06T00:35:41.949998Z","shell.execute_reply.started":"2022-07-06T00:35:41.940485Z","shell.execute_reply":"2022-07-06T00:35:41.949000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 11: Find Accuracy for Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \naccuracy_rf = accuracy_score(ytest, rf_pred)\nprint(\"Accuracy: {}\".format(accuracy_rf))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:35:42.385328Z","iopub.execute_input":"2022-07-06T00:35:42.386052Z","iopub.status.idle":"2022-07-06T00:35:42.393158Z","shell.execute_reply.started":"2022-07-06T00:35:42.386002Z","shell.execute_reply":"2022-07-06T00:35:42.392023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 12: Find Recall for Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \nrecall_rf = recall_score(ytest, rf_pred)\nprint(\"Recall: {}\".format(recall_rf))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:42.700108Z","iopub.execute_input":"2022-07-06T00:35:42.700485Z","iopub.status.idle":"2022-07-06T00:35:42.708706Z","shell.execute_reply.started":"2022-07-06T00:35:42.700455Z","shell.execute_reply":"2022-07-06T00:35:42.707349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 13: Find F1 Score for Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \nrf_f1=f1_score(ytest, rf_pred)\nprint(\"F1 Score: {}\".format(rf_f1))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:43.001805Z","iopub.execute_input":"2022-07-06T00:35:43.002433Z","iopub.status.idle":"2022-07-06T00:35:43.009527Z","shell.execute_reply.started":"2022-07-06T00:35:43.002388Z","shell.execute_reply":"2022-07-06T00:35:43.008460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 14: Print Classification Report for Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:43.303540Z","iopub.execute_input":"2022-07-06T00:35:43.304669Z","iopub.status.idle":"2022-07-06T00:35:43.308737Z","shell.execute_reply.started":"2022-07-06T00:35:43.304624Z","shell.execute_reply":"2022-07-06T00:35:43.307988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write code here \nprint(classification_report(ytest, rf_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:43.635330Z","iopub.execute_input":"2022-07-06T00:35:43.635911Z","iopub.status.idle":"2022-07-06T00:35:43.645056Z","shell.execute_reply.started":"2022-07-06T00:35:43.635878Z","shell.execute_reply":"2022-07-06T00:35:43.643891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ROC Curve for Random Forest<br>\nDefining the function","metadata":{}},{"cell_type":"code","source":"def plot_roc_curve(fpr, tpr, label=None):\n    plt.figure(figsize=(8,6))\n    plt.title('ROC Curve')\n    plt.plot(fpr, tpr, linewidth=2, label=label)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.axis([-0.005, 1, 0, 1.005])\n    plt.xticks(np.arange(0,1, 0.05), rotation=90)\n    plt.xlabel(\"False Positive Rate\")\n    plt.ylabel(\"True Positive Rate\")\n    plt.legend(loc='best')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:44.038909Z","iopub.execute_input":"2022-07-06T00:35:44.040059Z","iopub.status.idle":"2022-07-06T00:35:44.048063Z","shell.execute_reply.started":"2022-07-06T00:35:44.040015Z","shell.execute_reply":"2022-07-06T00:35:44.046887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import auc,roc_curve,roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:44.441879Z","iopub.execute_input":"2022-07-06T00:35:44.442342Z","iopub.status.idle":"2022-07-06T00:35:44.446825Z","shell.execute_reply.started":"2022-07-06T00:35:44.442296Z","shell.execute_reply":"2022-07-06T00:35:44.446012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auc_rf=roc_auc_score(ytest,rf_pred_prb)\nfpr,tpr,threshold=roc_curve(ytest,rf_pred_prb)\nplot_roc_curve(fpr,tpr,label='AUC = %0.3f'% auc_rf)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:44.988070Z","iopub.execute_input":"2022-07-06T00:35:44.988656Z","iopub.status.idle":"2022-07-06T00:35:45.337275Z","shell.execute_reply.started":"2022-07-06T00:35:44.988624Z","shell.execute_reply":"2022-07-06T00:35:45.335873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Features Importance","metadata":{}},{"cell_type":"code","source":"features_tuple=list(zip(X.columns,clf_rf.feature_importances_))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:45.490328Z","iopub.execute_input":"2022-07-06T00:35:45.491225Z","iopub.status.idle":"2022-07-06T00:35:45.506715Z","shell.execute_reply.started":"2022-07-06T00:35:45.491180Z","shell.execute_reply":"2022-07-06T00:35:45.505412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp=pd.DataFrame(features_tuple,columns=[\"Feature Names\",\"Importance\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:45.938189Z","iopub.execute_input":"2022-07-06T00:35:45.938755Z","iopub.status.idle":"2022-07-06T00:35:45.943675Z","shell.execute_reply.started":"2022-07-06T00:35:45.938722Z","shell.execute_reply":"2022-07-06T00:35:45.942633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp=feature_imp.sort_values(\"Importance\",ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:46.428932Z","iopub.execute_input":"2022-07-06T00:35:46.429509Z","iopub.status.idle":"2022-07-06T00:35:46.434132Z","shell.execute_reply.started":"2022-07-06T00:35:46.429478Z","shell.execute_reply":"2022-07-06T00:35:46.433329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsns.barplot(x=\"Feature Names\",y=\"Importance\", data=feature_imp, color='b')\nplt.xlabel(\"Titanic Features\")\nplt.ylabel(\"Importance\")\nplt.xticks(rotation=90)\nplt.title(\"Random Forest Classifier - Features Importance\")","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:47.002547Z","iopub.execute_input":"2022-07-06T00:35:47.003309Z","iopub.status.idle":"2022-07-06T00:35:47.545609Z","shell.execute_reply.started":"2022-07-06T00:35:47.003272Z","shell.execute_reply":"2022-07-06T00:35:47.544101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Hyperparameter Tuning using GridSearchCV","metadata":{}},{"cell_type":"markdown","source":"Importing GridSearchCV","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:47.953628Z","iopub.execute_input":"2022-07-06T00:35:47.954048Z","iopub.status.idle":"2022-07-06T00:35:47.959466Z","shell.execute_reply.started":"2022-07-06T00:35:47.954014Z","shell.execute_reply":"2022-07-06T00:35:47.958622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Making grid of parameters and running GridSearch CV","metadata":{}},{"cell_type":"code","source":"param_grid1 = {\"n_estimators\" : [9, 18, 27, 36, 45, 54, 63],\n           \"max_depth\" : [1, 5, 10, 15, 20, 25, 30],\n           \"min_samples_leaf\" : [1, 2, 4, 6, 8, 10]}\n\nRF = RandomForestClassifier(random_state=100)\n# Instantiate the GridSearchCV object: logreg_cv\nRF_cv1 = GridSearchCV(RF, param_grid1, cv=5,scoring='accuracy',n_jobs=4)\n\n# Fit it to the data\nRF_cv1.fit(xtrain,ytrain)\n\n#RF_cv1.cv_results_, \nRF_cv1.best_params_, RF_cv1.best_score_","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:35:48.834792Z","iopub.execute_input":"2022-07-06T00:35:48.835527Z","iopub.status.idle":"2022-07-06T00:36:37.026369Z","shell.execute_reply.started":"2022-07-06T00:35:48.835492Z","shell.execute_reply":"2022-07-06T00:36:37.024959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So we found **'max_depth': 10, 'min_samples_leaf': 2, 'n_estimators': 18** as optimum parameters","metadata":{}},{"cell_type":"markdown","source":"Let's try new set of parameter values near to optimum.","metadata":{}},{"cell_type":"code","source":"param_grid2 = {\"n_estimators\" : [9,12,15,18,21,24,27],\n           \"max_depth\" : [5,6,7,8,9,10,11,12,13,14,15],\n           \"min_samples_leaf\" : [1,2,3,4]}\n\nRF = RandomForestClassifier(random_state=100)\n# Instantiate the GridSearchCV object: logreg_cv\nRF_cv2 = GridSearchCV(RF, param_grid2, cv=5,scoring='accuracy',n_jobs=4)\n\n# Fit it to the data\nRF_cv2.fit(xtrain,ytrain)\n\n#RF_cv2.grid_scores_, \nRF_cv2.best_params_, RF_cv2.best_score_","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:36:37.029078Z","iopub.execute_input":"2022-07-06T00:36:37.029551Z","iopub.status.idle":"2022-07-06T00:37:03.358689Z","shell.execute_reply.started":"2022-07-06T00:36:37.029503Z","shell.execute_reply":"2022-07-06T00:37:03.357614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" So we found **'max_depth': 10, 'min_samples_leaf': 2, 'n_estimators': 15** as optimum parameters","metadata":{}},{"cell_type":"markdown","source":"### Tuned Random Forest","metadata":{}},{"cell_type":"code","source":"RF_cv2.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:03.359859Z","iopub.execute_input":"2022-07-06T00:37:03.360187Z","iopub.status.idle":"2022-07-06T00:37:03.367078Z","shell.execute_reply.started":"2022-07-06T00:37:03.360159Z","shell.execute_reply":"2022-07-06T00:37:03.366083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RF_grid = RF_cv2.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:03.369125Z","iopub.execute_input":"2022-07-06T00:37:03.369960Z","iopub.status.idle":"2022-07-06T00:37:03.380265Z","shell.execute_reply.started":"2022-07-06T00:37:03.369925Z","shell.execute_reply":"2022-07-06T00:37:03.379172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RF_grid.fit(xtrain, ytrain)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:37:13.832999Z","iopub.execute_input":"2022-07-06T00:37:13.834003Z","iopub.status.idle":"2022-07-06T00:37:13.866268Z","shell.execute_reply.started":"2022-07-06T00:37:13.833925Z","shell.execute_reply":"2022-07-06T00:37:13.865099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_pred_t = RF_grid.predict(xtest)\nrf_pred_prb_t=RF_grid.predict_proba(xtest)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:15.383101Z","iopub.execute_input":"2022-07-06T00:37:15.384158Z","iopub.status.idle":"2022-07-06T00:37:15.398610Z","shell.execute_reply.started":"2022-07-06T00:37:15.384108Z","shell.execute_reply":"2022-07-06T00:37:15.397768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 15: Find Accuracy for Tuned Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \naccuracy_rf_t = accuracy_score(ytest, rf_pred_t)\nprint(\"Accuracy after tuning: {}\".format(accuracy_rf_t))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:16.773493Z","iopub.execute_input":"2022-07-06T00:37:16.774151Z","iopub.status.idle":"2022-07-06T00:37:16.780066Z","shell.execute_reply.started":"2022-07-06T00:37:16.774114Z","shell.execute_reply":"2022-07-06T00:37:16.779169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 16: Find Recall for Tuned Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \nrecall_rf_t=recall_score(ytest, rf_pred_t)\nrecall_rf_t","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:17.372908Z","iopub.execute_input":"2022-07-06T00:37:17.373537Z","iopub.status.idle":"2022-07-06T00:37:17.381239Z","shell.execute_reply.started":"2022-07-06T00:37:17.373504Z","shell.execute_reply":"2022-07-06T00:37:17.380376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 17: Find Precision for Tuned Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \nprecision_rf_t=precision_score(ytest,rf_pred_t)\nprecision_rf_t","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:17.830754Z","iopub.execute_input":"2022-07-06T00:37:17.831159Z","iopub.status.idle":"2022-07-06T00:37:17.839438Z","shell.execute_reply.started":"2022-07-06T00:37:17.831122Z","shell.execute_reply":"2022-07-06T00:37:17.838528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 18: Find F1 Score","metadata":{}},{"cell_type":"code","source":"#write code here \nrf_t_f1=f1_score(ytest,rf_pred_t)\nrf_t_f1","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:18.240150Z","iopub.execute_input":"2022-07-06T00:37:18.241267Z","iopub.status.idle":"2022-07-06T00:37:18.251738Z","shell.execute_reply.started":"2022-07-06T00:37:18.241223Z","shell.execute_reply":"2022-07-06T00:37:18.250734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 19: Print Classification Report for Tuned Random Forest","metadata":{}},{"cell_type":"code","source":"#write code here \nprint(classification_report(ytest, rf_pred_t))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:37:18.602209Z","iopub.execute_input":"2022-07-06T00:37:18.602634Z","iopub.status.idle":"2022-07-06T00:37:18.613771Z","shell.execute_reply.started":"2022-07-06T00:37:18.602599Z","shell.execute_reply":"2022-07-06T00:37:18.612706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ROC Curve for Tuned Random Forest","metadata":{}},{"cell_type":"code","source":"auc_rf_t=roc_auc_score(ytest,rf_pred_prb_t)\nfpr,tpr,threshold=roc_curve(ytest,rf_pred_prb_t)\nplot_roc_curve(fpr,tpr,label='AUC Tuned = %0.3f'% auc_rf_t)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:19.151900Z","iopub.execute_input":"2022-07-06T00:37:19.152984Z","iopub.status.idle":"2022-07-06T00:37:19.495159Z","shell.execute_reply.started":"2022-07-06T00:37:19.152918Z","shell.execute_reply":"2022-07-06T00:37:19.493997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Hyperparameters Tuning Using Randomized Search","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import RandomizedSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:19.530563Z","iopub.execute_input":"2022-07-06T00:37:19.531276Z","iopub.status.idle":"2022-07-06T00:37:19.536461Z","shell.execute_reply.started":"2022-07-06T00:37:19.531223Z","shell.execute_reply":"2022-07-06T00:37:19.535576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_rs = RandomForestClassifier(random_state=100)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:37:19.948892Z","iopub.execute_input":"2022-07-06T00:37:19.949791Z","iopub.status.idle":"2022-07-06T00:37:19.955216Z","shell.execute_reply.started":"2022-07-06T00:37:19.949740Z","shell.execute_reply":"2022-07-06T00:37:19.954415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of trees in random forest\nn_estimators = [int(x) for x in np.linspace(start = 10, stop = 1000, num = 10)]\n# Maximum number of levels in tree\nmax_depth = [int(x) for x in np.linspace(10, 110, num = 11)]\nmax_depth.append(None)\n# Minimum number of samples required to split a node\nmin_samples_split = [2, 5, 10]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:23:40.082509Z","iopub.execute_input":"2022-07-06T00:23:40.083171Z","iopub.status.idle":"2022-07-06T00:23:40.092441Z","shell.execute_reply.started":"2022-07-06T00:23:40.083119Z","shell.execute_reply":"2022-07-06T00:23:40.090677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_grid1 = {\"n_estimators\" : n_estimators,\n           \"max_depth\" : max_depth,\n           \"min_samples_split\" :min_samples_split}","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:23:40.310046Z","iopub.execute_input":"2022-07-06T00:23:40.310415Z","iopub.status.idle":"2022-07-06T00:23:40.315638Z","shell.execute_reply.started":"2022-07-06T00:23:40.310387Z","shell.execute_reply":"2022-07-06T00:23:40.314773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_grid1","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:23:40.581272Z","iopub.execute_input":"2022-07-06T00:23:40.582264Z","iopub.status.idle":"2022-07-06T00:23:40.590383Z","shell.execute_reply.started":"2022-07-06T00:23:40.582205Z","shell.execute_reply":"2022-07-06T00:23:40.589353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_random = RandomizedSearchCV(estimator = rf_rs, param_distributions = random_grid1, n_iter = 50, cv = 5, verbose=2, random_state=100)\n# Fit the random search model\nrf_random.fit(xtrain, ytrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:42:20.684555Z","iopub.execute_input":"2022-07-06T00:42:20.684934Z","iopub.status.idle":"2022-07-06T00:46:52.314374Z","shell.execute_reply.started":"2022-07-06T00:42:20.684905Z","shell.execute_reply":"2022-07-06T00:46:52.313302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Selecting the best classifier**","metadata":{}},{"cell_type":"code","source":"clf_rf_rand=rf_random.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.316652Z","iopub.execute_input":"2022-07-06T00:46:52.317242Z","iopub.status.idle":"2022-07-06T00:46:52.324253Z","shell.execute_reply.started":"2022-07-06T00:46:52.317194Z","shell.execute_reply":"2022-07-06T00:46:52.323038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_rf_rand.fit(xtrain,ytrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.326199Z","iopub.execute_input":"2022-07-06T00:46:52.326621Z","iopub.status.idle":"2022-07-06T00:46:52.359962Z","shell.execute_reply.started":"2022-07-06T00:46:52.326579Z","shell.execute_reply":"2022-07-06T00:46:52.359145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_rf_rand=clf_rf_rand.predict(xtest)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.361994Z","iopub.execute_input":"2022-07-06T00:46:52.362465Z","iopub.status.idle":"2022-07-06T00:46:52.370547Z","shell.execute_reply.started":"2022-07-06T00:46:52.362435Z","shell.execute_reply":"2022-07-06T00:46:52.369641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prb_rf_rand=clf_rf_rand.predict_proba(xtest)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.371813Z","iopub.execute_input":"2022-07-06T00:46:52.372271Z","iopub.status.idle":"2022-07-06T00:46:52.383247Z","shell.execute_reply.started":"2022-07-06T00:46:52.372237Z","shell.execute_reply":"2022-07-06T00:46:52.382233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 20: Find the Accuracy of Tuned Random Forest through Randomized Search","metadata":{}},{"cell_type":"code","source":"#write code here \naccuracy_rf_r= accuracy_score(ytest,pred_rf_rand)\naccuracy_rf_r","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.384452Z","iopub.execute_input":"2022-07-06T00:46:52.384970Z","iopub.status.idle":"2022-07-06T00:46:52.395559Z","shell.execute_reply.started":"2022-07-06T00:46:52.384926Z","shell.execute_reply":"2022-07-06T00:46:52.394475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 21: Find the Precision of Tuned Random Forest through Randomized Search","metadata":{}},{"cell_type":"code","source":"#write code here \nprecision_rf_r=precision_score(ytest,pred_rf_rand)\nprecision_rf_r","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.396777Z","iopub.execute_input":"2022-07-06T00:46:52.397124Z","iopub.status.idle":"2022-07-06T00:46:52.410022Z","shell.execute_reply.started":"2022-07-06T00:46:52.397093Z","shell.execute_reply":"2022-07-06T00:46:52.408831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 22: Find the Recall of Tuned Random Forest through Randomized Search","metadata":{}},{"cell_type":"code","source":"#write code here \nrecall_rf_r=recall_score(ytest,pred_rf_rand)\nrecall_rf_r","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.411779Z","iopub.execute_input":"2022-07-06T00:46:52.412263Z","iopub.status.idle":"2022-07-06T00:46:52.423533Z","shell.execute_reply.started":"2022-07-06T00:46:52.412220Z","shell.execute_reply":"2022-07-06T00:46:52.422727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 23: Find the F1-Score of Tuned Random Forest through Randomized Search","metadata":{}},{"cell_type":"code","source":"#write code here \nf1_rf_r=accuracy_score(ytest,pred_rf_rand)\nf1_rf_r","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.424755Z","iopub.execute_input":"2022-07-06T00:46:52.425776Z","iopub.status.idle":"2022-07-06T00:46:52.433626Z","shell.execute_reply.started":"2022-07-06T00:46:52.425740Z","shell.execute_reply":"2022-07-06T00:46:52.432548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 24: Print the Classification Report of Tuned Random Forest through Randomized Search","metadata":{}},{"cell_type":"code","source":"#write code here \nprint(classification_report(ytest,pred_rf_rand))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.439033Z","iopub.execute_input":"2022-07-06T00:46:52.439472Z","iopub.status.idle":"2022-07-06T00:46:52.450117Z","shell.execute_reply.started":"2022-07-06T00:46:52.439439Z","shell.execute_reply":"2022-07-06T00:46:52.448992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ROC Curve","metadata":{}},{"cell_type":"code","source":"auc_rf_r=roc_auc_score(ytest,prb_rf_rand)\nfpr,tpr,threshold=roc_curve(ytest,prb_rf_rand)\nplot_roc_curve(fpr,tpr,label='AUC Tuned Random = %0.3f'% auc_rf_r)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.451784Z","iopub.execute_input":"2022-07-06T00:46:52.452404Z","iopub.status.idle":"2022-07-06T00:46:52.799057Z","shell.execute_reply.started":"2022-07-06T00:46:52.452358Z","shell.execute_reply":"2022-07-06T00:46:52.797823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ada Boost","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import AdaBoostClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.800643Z","iopub.execute_input":"2022-07-06T00:46:52.800993Z","iopub.status.idle":"2022-07-06T00:46:52.805981Z","shell.execute_reply.started":"2022-07-06T00:46:52.800963Z","shell.execute_reply":"2022-07-06T00:46:52.804811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_adb = AdaBoostClassifier(random_state=100)\nclf_adb.fit(xtrain, ytrain) ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.807725Z","iopub.execute_input":"2022-07-06T00:46:52.808193Z","iopub.status.idle":"2022-07-06T00:46:52.919716Z","shell.execute_reply.started":"2022-07-06T00:46:52.808157Z","shell.execute_reply":"2022-07-06T00:46:52.918709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_clf_adb=clf_adb.predict(xtest)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.921088Z","iopub.execute_input":"2022-07-06T00:46:52.921390Z","iopub.status.idle":"2022-07-06T00:46:52.941842Z","shell.execute_reply.started":"2022-07-06T00:46:52.921362Z","shell.execute_reply":"2022-07-06T00:46:52.940812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adb_pred_prb=clf_adb.predict_proba(xtest)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.944933Z","iopub.execute_input":"2022-07-06T00:46:52.945840Z","iopub.status.idle":"2022-07-06T00:46:52.964779Z","shell.execute_reply.started":"2022-07-06T00:46:52.945801Z","shell.execute_reply":"2022-07-06T00:46:52.963369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 25: Find Accuracy for Ada Boost","metadata":{}},{"cell_type":"code","source":"#write code here \naccuracy_adb=accuracy_score(ytest,pred_clf_adb)\naccuracy_adb","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.966182Z","iopub.execute_input":"2022-07-06T00:46:52.966532Z","iopub.status.idle":"2022-07-06T00:46:52.974456Z","shell.execute_reply.started":"2022-07-06T00:46:52.966502Z","shell.execute_reply":"2022-07-06T00:46:52.973635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 26: Find Precision for Ada Boost","metadata":{}},{"cell_type":"code","source":"#write code here \nprecision_adb=precision_score(ytest,pred_clf_adb)\nprecision_adb","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.975933Z","iopub.execute_input":"2022-07-06T00:46:52.976308Z","iopub.status.idle":"2022-07-06T00:46:52.989169Z","shell.execute_reply.started":"2022-07-06T00:46:52.976277Z","shell.execute_reply":"2022-07-06T00:46:52.988006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 27: Find Recall for Ada Boost","metadata":{}},{"cell_type":"code","source":"#write code here \nrecall_adb=recall_score(ytest,pred_clf_adb)\nrecall_adb","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:52.991162Z","iopub.execute_input":"2022-07-06T00:46:52.991743Z","iopub.status.idle":"2022-07-06T00:46:53.000271Z","shell.execute_reply.started":"2022-07-06T00:46:52.991693Z","shell.execute_reply":"2022-07-06T00:46:52.999200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 28: Find F1 Score for Ada Boost","metadata":{}},{"cell_type":"code","source":"#write code here \nf1_adb=f1_score(ytest,pred_clf_adb)\nf1_adb","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.002102Z","iopub.execute_input":"2022-07-06T00:46:53.003006Z","iopub.status.idle":"2022-07-06T00:46:53.011968Z","shell.execute_reply.started":"2022-07-06T00:46:53.002935Z","shell.execute_reply":"2022-07-06T00:46:53.011013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 29: Print Classification Report for Ada Boost","metadata":{}},{"cell_type":"code","source":"#write code here \nprint(classification_report(ytest,pred_clf_adb))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.013072Z","iopub.execute_input":"2022-07-06T00:46:53.013681Z","iopub.status.idle":"2022-07-06T00:46:53.025475Z","shell.execute_reply.started":"2022-07-06T00:46:53.013648Z","shell.execute_reply":"2022-07-06T00:46:53.024360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ROC Curve","metadata":{}},{"cell_type":"code","source":"auc_adb=roc_auc_score(ytest,adb_pred_prb)\nfpr,tpr,threshold=roc_curve(ytest,adb_pred_prb)\nplot_roc_curve(fpr,tpr,label='AUC = %0.3f'% auc_adb)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.027009Z","iopub.execute_input":"2022-07-06T00:46:53.027673Z","iopub.status.idle":"2022-07-06T00:46:53.372202Z","shell.execute_reply.started":"2022-07-06T00:46:53.027629Z","shell.execute_reply":"2022-07-06T00:46:53.371069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# XGBoost","metadata":{}},{"cell_type":"markdown","source":"## Extreme Gradient Boosting Algorithm","metadata":{}},{"cell_type":"code","source":"import xgboost as xgb     ## dont contains LAsso and redge algorithm","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.373529Z","iopub.execute_input":"2022-07-06T00:46:53.373850Z","iopub.status.idle":"2022-07-06T00:46:53.477726Z","shell.execute_reply.started":"2022-07-06T00:46:53.373821Z","shell.execute_reply":"2022-07-06T00:46:53.476798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_xgb = xgb.XGBClassifier(seed=25,nthread=1,random_state=100)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.479154Z","iopub.execute_input":"2022-07-06T00:46:53.479488Z","iopub.status.idle":"2022-07-06T00:46:53.485415Z","shell.execute_reply.started":"2022-07-06T00:46:53.479459Z","shell.execute_reply":"2022-07-06T00:46:53.484220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_xgb.fit(xtrain, ytrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.487299Z","iopub.execute_input":"2022-07-06T00:46:53.487832Z","iopub.status.idle":"2022-07-06T00:46:53.835691Z","shell.execute_reply.started":"2022-07-06T00:46:53.487788Z","shell.execute_reply":"2022-07-06T00:46:53.834706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_pred = clf_xgb.predict(xtest)\nxgb_pred_prb=clf_xgb.predict_proba(xtest)[:,1]","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:46:53.837122Z","iopub.execute_input":"2022-07-06T00:46:53.837436Z","iopub.status.idle":"2022-07-06T00:46:53.856258Z","shell.execute_reply.started":"2022-07-06T00:46:53.837398Z","shell.execute_reply":"2022-07-06T00:46:53.855088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 30: Find Accuracy of XGBoost","metadata":{}},{"cell_type":"code","source":"#write code here \naccuracy_xgb = accuracy_score(ytest,xgb_pred)\nprint(\"Accuracy: {}\".format(accuracy_xgb))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.857857Z","iopub.execute_input":"2022-07-06T00:46:53.858477Z","iopub.status.idle":"2022-07-06T00:46:53.864632Z","shell.execute_reply.started":"2022-07-06T00:46:53.858441Z","shell.execute_reply":"2022-07-06T00:46:53.863768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 31: Find  Recall of XGBoost","metadata":{}},{"cell_type":"code","source":"#write code here \nrecall_xgb = accuracy_score(ytest,xgb_pred)\nrecall_xgb","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.866056Z","iopub.execute_input":"2022-07-06T00:46:53.866561Z","iopub.status.idle":"2022-07-06T00:46:53.876736Z","shell.execute_reply.started":"2022-07-06T00:46:53.866521Z","shell.execute_reply":"2022-07-06T00:46:53.875893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  Task 32: Find  Precision of XGBoost","metadata":{}},{"cell_type":"code","source":"#write code here \nprecision_xgb = accuracy_score(ytest,xgb_pred)\nprecision_xgb","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.882676Z","iopub.execute_input":"2022-07-06T00:46:53.883352Z","iopub.status.idle":"2022-07-06T00:46:53.891395Z","shell.execute_reply.started":"2022-07-06T00:46:53.883303Z","shell.execute_reply":"2022-07-06T00:46:53.890152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 33: Find F1 Score XGB","metadata":{}},{"cell_type":"code","source":"#write code here \nxgb_f1=f1_score(ytest,xgb_pred)\nxgb_f1","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:53.892933Z","iopub.execute_input":"2022-07-06T00:46:53.893305Z","iopub.status.idle":"2022-07-06T00:46:53.904302Z","shell.execute_reply.started":"2022-07-06T00:46:53.893274Z","shell.execute_reply":"2022-07-06T00:46:53.903163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 34: Print Classification Report of XGBoost","metadata":{}},{"cell_type":"code","source":"#write code here \nprint(classification_report(ytest,xgb_pred))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:46:53.906178Z","iopub.execute_input":"2022-07-06T00:46:53.907072Z","iopub.status.idle":"2022-07-06T00:46:53.920162Z","shell.execute_reply.started":"2022-07-06T00:46:53.906869Z","shell.execute_reply":"2022-07-06T00:46:53.919031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ROC Curve","metadata":{}},{"cell_type":"code","source":"auc_xgb=roc_auc_score(ytest,xgb_pred_prb)\nfpr,tpr,threshold=roc_curve(ytest,xgb_pred_prb)\nplot_roc_curve(fpr,tpr,label='AUC = %0.3f'% auc_xgb)\n ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T00:46:53.921971Z","iopub.execute_input":"2022-07-06T00:46:53.922686Z","iopub.status.idle":"2022-07-06T00:46:54.231024Z","shell.execute_reply.started":"2022-07-06T00:46:53.922643Z","shell.execute_reply":"2022-07-06T00:46:54.229799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Comparison","metadata":{}},{"cell_type":"code","source":"comparison_dict={\"Algorithm\":[\"Decision Tree\",\"Random Forest\",\"Tuned Random Forest(Grid)\",\"Tuned Random Forest(Random)\",\"XGBoost\",\"Ada Boost\"],\n                 \"Accuracy\":[accuracy_dt,accuracy_rf,accuracy_rf_t,accuracy_rf_r,accuracy_xgb,accuracy_adb],\n                 \"Precision\":[precision_dt,precision_rf,precision_rf_t,precision_rf_r,precision_xgb,precision_adb],\n                 \"Recall\":[recall_dt,recall_rf,recall_rf_t,recall_rf_r,recall_xgb,recall_adb],\n                 \"AUC\":[auc_dt,auc_rf,auc_rf_t,auc_rf_r,auc_xgb,auc_adb],\n                 \"F1 Score\":[dt_f1,rf_f1,rf_t_f1,f1_rf_r,xgb_f1,f1_adb]\n                }","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:54.232984Z","iopub.execute_input":"2022-07-06T00:46:54.233753Z","iopub.status.idle":"2022-07-06T00:46:54.240625Z","shell.execute_reply.started":"2022-07-06T00:46:54.233708Z","shell.execute_reply":"2022-07-06T00:46:54.239569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"comparison=pd.DataFrame(comparison_dict)\ncomparison.sort_values(['Recall','Accuracy', 'AUC'], ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T00:46:54.242490Z","iopub.execute_input":"2022-07-06T00:46:54.242940Z","iopub.status.idle":"2022-07-06T00:46:54.265889Z","shell.execute_reply.started":"2022-07-06T00:46:54.242904Z","shell.execute_reply":"2022-07-06T00:46:54.265087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission on Kaggle","metadata":{}},{"cell_type":"markdown","source":"### Task 35: Import test data","metadata":{}},{"cell_type":"code","source":"#Write code here\ntest = pd.read_csv('./competetion_clean.csv')\ndf_test = test.copy()\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:05:37.196797Z","iopub.execute_input":"2022-07-06T01:05:37.197215Z","iopub.status.idle":"2022-07-06T01:05:37.216422Z","shell.execute_reply.started":"2022-07-06T01:05:37.197181Z","shell.execute_reply":"2022-07-06T01:05:37.215337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 36: Do One Hot encoding of test data","metadata":{}},{"cell_type":"code","source":"#write code here\ndf_OneHot= pd.get_dummies(test, columns=['Pclass','Sex','Embarked','Title','GrpSize', 'FareCat', 'AgeCat']) \ndf_OneHot.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:05:43.126334Z","iopub.execute_input":"2022-07-06T01:05:43.126765Z","iopub.status.idle":"2022-07-06T01:05:43.156361Z","shell.execute_reply.started":"2022-07-06T01:05:43.126731Z","shell.execute_reply":"2022-07-06T01:05:43.155522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test= df_OneHot.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:05:44.822048Z","iopub.execute_input":"2022-07-06T01:05:44.823146Z","iopub.status.idle":"2022-07-06T01:05:44.828798Z","shell.execute_reply.started":"2022-07-06T01:05:44.823096Z","shell.execute_reply":"2022-07-06T01:05:44.828025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 37: Separate Passenger ID for submission","metadata":{}},{"cell_type":"code","source":"# Write code here\nPassengerID=df_test['PassengerId']\ndf_test.drop(['PassengerId'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:05:50.260764Z","iopub.execute_input":"2022-07-06T01:05:50.261148Z","iopub.status.idle":"2022-07-06T01:05:50.268349Z","shell.execute_reply.started":"2022-07-06T01:05:50.261118Z","shell.execute_reply":"2022-07-06T01:05:50.266740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Task 37: Do prediction through final model","metadata":{}},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:05:53.774982Z","iopub.execute_input":"2022-07-06T01:05:53.775714Z","iopub.status.idle":"2022-07-06T01:05:53.783324Z","shell.execute_reply.started":"2022-07-06T01:05:53.775674Z","shell.execute_reply":"2022-07-06T01:05:53.781995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#write code here\npred_final=clf_adb.predict(df_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:05:57.192314Z","iopub.execute_input":"2022-07-06T01:05:57.192957Z","iopub.status.idle":"2022-07-06T01:05:57.218234Z","shell.execute_reply.started":"2022-07-06T01:05:57.192898Z","shell.execute_reply":"2022-07-06T01:05:57.217057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Creating Data Frame for submission","metadata":{}},{"cell_type":"code","source":"submission=pd.DataFrame({'PassengerId':PassengerID,'Survived':pred_final})","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:06:03.760291Z","iopub.execute_input":"2022-07-06T01:06:03.760663Z","iopub.status.idle":"2022-07-06T01:06:03.767107Z","shell.execute_reply.started":"2022-07-06T01:06:03.760634Z","shell.execute_reply":"2022-07-06T01:06:03.765778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:06:12.704449Z","iopub.execute_input":"2022-07-06T01:06:12.704816Z","iopub.status.idle":"2022-07-06T01:06:12.714823Z","shell.execute_reply.started":"2022-07-06T01:06:12.704787Z","shell.execute_reply":"2022-07-06T01:06:12.713883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Advanced Classification### Task 10: Export the dataset into csv file","metadata":{}},{"cell_type":"code","source":"# Write code here\nsubmission.to_csv('my_submission v2.0.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T01:06:21.281784Z","iopub.execute_input":"2022-07-06T01:06:21.282635Z","iopub.status.idle":"2022-07-06T01:06:21.290192Z","shell.execute_reply.started":"2022-07-06T01:06:21.282584Z","shell.execute_reply":"2022-07-06T01:06:21.289026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}