{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-13T12:29:55.018936Z","iopub.execute_input":"2022-07-13T12:29:55.019336Z","iopub.status.idle":"2022-07-13T12:29:55.030395Z","shell.execute_reply.started":"2022-07-13T12:29:55.019293Z","shell.execute_reply":"2022-07-13T12:29:55.029190Z"},"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\n%matplotlib inline\npd.set_option('display.max_columns', None)\nsns.set(style='whitegrid')\n\nfrom sklearn.experimental import enable_iterative_imputer\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler\nfrom sklearn.impute import SimpleImputer, IterativeImputer\nfrom sklearn.metrics import roc_curve, auc,confusion_matrix,accuracy_score\nfrom sklearn.model_selection import cross_val_score, StratifiedKFold,train_test_split,KFold,cross_val_predict\nfrom sklearn.pipeline import Pipeline\nimport pylab\nimport scipy.stats as stats\nSEED=42","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:55.035967Z","iopub.execute_input":"2022-07-13T12:29:55.037064Z","iopub.status.idle":"2022-07-13T12:29:55.064563Z","shell.execute_reply.started":"2022-07-13T12:29:55.037018Z","shell.execute_reply":"2022-07-13T12:29:55.063051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.read_csv('../input/titanic/train.csv')\ndf_test=pd.read_csv('../input/titanic/test.csv')\ndf_all=pd.concat([df_train,df_test],axis=0)\n\nsubmission_df=pd.DataFrame(columns=['PassengerId','Survived'])\nsubmission_df['PassengerId']=df_test['PassengerId']","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:55.067214Z","iopub.execute_input":"2022-07-13T12:29:55.068096Z","iopub.status.idle":"2022-07-13T12:29:55.101129Z","shell.execute_reply.started":"2022-07-13T12:29:55.068048Z","shell.execute_reply":"2022-07-13T12:29:55.100131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.reset_index(drop=True,inplace=True)\ndf_all","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:55.102563Z","iopub.execute_input":"2022-07-13T12:29:55.103029Z","iopub.status.idle":"2022-07-13T12:29:55.130996Z","shell.execute_reply.started":"2022-07-13T12:29:55.102987Z","shell.execute_reply":"2022-07-13T12:29:55.130160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.Now we will start basic EDA","metadata":{}},{"cell_type":"code","source":"print('Number of Training Examples =',df_train.shape[0])\nprint('Number of Testing Examples =',df_test.shape[0])\nprint('Columns of training data: ',df_train.columns,'Number of columns:',len(df_train.columns))\nprint('Columns of testing data: ',df_test.columns,'Number of columns:',len(df_test.columns))\nprint('The extra column in training data is',set(df_train.columns)-set(df_test.columns))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:55.133049Z","iopub.execute_input":"2022-07-13T12:29:55.133521Z","iopub.status.idle":"2022-07-13T12:29:55.141375Z","shell.execute_reply.started":"2022-07-13T12:29:55.133491Z","shell.execute_reply":"2022-07-13T12:29:55.140535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The target variable here is **Survived**","metadata":{}},{"cell_type":"code","source":"print(df_all.Pclass.value_counts())\nprint(df_all.Embarked.value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:55.142463Z","iopub.execute_input":"2022-07-13T12:29:55.143426Z","iopub.status.idle":"2022-07-13T12:29:55.157790Z","shell.execute_reply.started":"2022-07-13T12:29:55.143392Z","shell.execute_reply":"2022-07-13T12:29:55.156894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 1.1 **Overview of features**\n* PassengerId has no effect on target var.\n* Survived is the target variable(0 or 1):\n    * 1=Survived\n    * 0=Deceased\n* Pclass( Passenger Class) is a categorical variable(ordinal) having 3 unique values (1,2,3):\n    * 1=Upper Class\n    * 2=Middle Class\n    * 3=Lower Class\n* Name, Sex and Age are self-explanatory\n* SibSp is the total number of the passengers' siblings and spouse\n* Parch is the total number of the passengers' parents and children\n* Ticket is the ticket number of the passenger\n* Fare is the passenger fare\n* Cabin is the cabin number of the passenger\n* Embarked is port of embarkation and it is a categorical feature which has 3 unique values (C, Q or S):\n     * S is Southampton\n     * C is Cherbourg\n     * Q is Queenstown","metadata":{}},{"cell_type":"code","source":"def bar_plot(variable):\n    \"\"\"\n        input: variable ex: \"Sex\"\n        output: bar plot & value count\n    \"\"\"\n    # get feature\n    var = df_all[variable]\n    # count number of categorical variable(value/sample)\n    varValue = var.value_counts()\n    \n    # visualize\n    plt.figure(figsize = (9,3))\n    plt.bar(varValue.index, varValue)\n    plt.xticks(varValue.index, varValue.index.values)\n    plt.ylabel(\"Frequency\")\n    plt.title(variable)\n    plt.show()\n    print(\"{}: \\n {}\".format(variable,varValue))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:55.159135Z","iopub.execute_input":"2022-07-13T12:29:55.159808Z","iopub.status.idle":"2022-07-13T12:29:55.171128Z","shell.execute_reply.started":"2022-07-13T12:29:55.159773Z","shell.execute_reply":"2022-07-13T12:29:55.169987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category1 = [\"Survived\",\"Sex\",\"Pclass\",\"Embarked\",\"SibSp\", \"Parch\"]\nfor c in category1:\n    bar_plot(c)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:55.173089Z","iopub.execute_input":"2022-07-13T12:29:55.173948Z","iopub.status.idle":"2022-07-13T12:29:56.173207Z","shell.execute_reply.started":"2022-07-13T12:29:55.173901Z","shell.execute_reply":"2022-07-13T12:29:56.172021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ncategory2 = [\"Cabin\", \"Name\", \"Ticket\"]\nfor c in category2:\n    print(\"{} \\n\".format(df_all[c].value_counts()))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:56.174943Z","iopub.execute_input":"2022-07-13T12:29:56.175413Z","iopub.status.idle":"2022-07-13T12:29:56.191529Z","shell.execute_reply.started":"2022-07-13T12:29:56.175366Z","shell.execute_reply":"2022-07-13T12:29:56.190197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_hist(variable):\n    plt.figure(figsize = (9,3))\n    plt.hist(df_all[variable], bins = 50)\n    plt.xlabel(variable)\n    plt.ylabel(\"Frequency\")\n    plt.title(\"{} distribution with hist\".format(variable))\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:56.193125Z","iopub.execute_input":"2022-07-13T12:29:56.193588Z","iopub.status.idle":"2022-07-13T12:29:56.201709Z","shell.execute_reply.started":"2022-07-13T12:29:56.193539Z","shell.execute_reply":"2022-07-13T12:29:56.200485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numericVar = [\"Fare\", \"Age\",\"PassengerId\"]\nfor n in numericVar:\n    plot_hist(n)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:56.204962Z","iopub.execute_input":"2022-07-13T12:29:56.205302Z","iopub.status.idle":"2022-07-13T12:29:57.078622Z","shell.execute_reply.started":"2022-07-13T12:29:56.205274Z","shell.execute_reply":"2022-07-13T12:29:57.077364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"One can see the types of features","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"\nfeatures_obj=[]\nfeatures_num=[]\nfor features in df_all.columns:\n    if df_all[features].dtype=='object':\n        features_obj.append(features)\n    else:\n        features_num.append(features)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.079859Z","iopub.execute_input":"2022-07-13T12:29:57.080187Z","iopub.status.idle":"2022-07-13T12:29:57.086059Z","shell.execute_reply.started":"2022-07-13T12:29:57.080146Z","shell.execute_reply":"2022-07-13T12:29:57.084900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we have segregated the types of features**","metadata":{}},{"cell_type":"code","source":"print('String Features :', features_obj)\nprint('Numerical Features :',features_num )","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.087479Z","iopub.execute_input":"2022-07-13T12:29:57.087788Z","iopub.status.idle":"2022-07-13T12:29:57.099130Z","shell.execute_reply.started":"2022-07-13T12:29:57.087750Z","shell.execute_reply":"2022-07-13T12:29:57.098191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.100513Z","iopub.execute_input":"2022-07-13T12:29:57.101747Z","iopub.status.idle":"2022-07-13T12:29:57.121442Z","shell.execute_reply.started":"2022-07-13T12:29:57.101708Z","shell.execute_reply":"2022-07-13T12:29:57.120239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.122919Z","iopub.execute_input":"2022-07-13T12:29:57.123731Z","iopub.status.idle":"2022-07-13T12:29:57.138663Z","shell.execute_reply.started":"2022-07-13T12:29:57.123692Z","shell.execute_reply":"2022-07-13T12:29:57.137449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Do some univariate analysis before filling out/Tableau","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1.2 Missing Values**\nIt is convenient to work on concatenated training and test set while dealing with missing values, otherwise filled data may overfit to training or test set samples. \nThe count of missing values in Age, Embarked and Fare are smaller compared to total sample, but roughly 80% of the Cabin is missing. Missing values in **Age, Embarked and Fare** can be filled with *descriptive statistical measures* but that wouldn't work for Cabin.\n\nStudying correlations will help us a lot.\n","metadata":{}},{"cell_type":"code","source":"for feature in df_all.columns:\n    print(feature,' percent missing value:',100*df_all[feature].isnull().sum()/df_all.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.140497Z","iopub.execute_input":"2022-07-13T12:29:57.141176Z","iopub.status.idle":"2022-07-13T12:29:57.155362Z","shell.execute_reply.started":"2022-07-13T12:29:57.141140Z","shell.execute_reply":"2022-07-13T12:29:57.153779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nheatmap=sns.heatmap(df_all.corr(),vmin=-1,vmax=1,annot=True,cmap='BrBG')\nheatmap.set_title('Pearson Correlation Heatmap',fontdict={'fontsize':18},pad=12)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.157147Z","iopub.execute_input":"2022-07-13T12:29:57.158516Z","iopub.status.idle":"2022-07-13T12:29:57.661658Z","shell.execute_reply.started":"2022-07-13T12:29:57.158458Z","shell.execute_reply":"2022-07-13T12:29:57.660397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1.2.1 Age**\nMissing values in Age are filled with","metadata":{}},{"cell_type":"code","source":"df_all.Age.hist(bins=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.663209Z","iopub.execute_input":"2022-07-13T12:29:57.663558Z","iopub.status.idle":"2022-07-13T12:29:57.903797Z","shell.execute_reply.started":"2022-07-13T12:29:57.663526Z","shell.execute_reply":"2022-07-13T12:29:57.902547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You can clearly see that the younger age bracket is mostly present","metadata":{}},{"cell_type":"code","source":"df_all[df_all.Age.isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.906209Z","iopub.execute_input":"2022-07-13T12:29:57.906741Z","iopub.status.idle":"2022-07-13T12:29:57.934033Z","shell.execute_reply.started":"2022-07-13T12:29:57.906691Z","shell.execute_reply":"2022-07-13T12:29:57.932704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.corr()[['Age']].sort_values(by='Age',ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.935680Z","iopub.execute_input":"2022-07-13T12:29:57.936356Z","iopub.status.idle":"2022-07-13T12:29:57.951029Z","shell.execute_reply.started":"2022-07-13T12:29:57.936309Z","shell.execute_reply":"2022-07-13T12:29:57.949652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 12))\nheatmap = sns.heatmap(df_all.corr()[['Age']].sort_values(by='Age',ascending=False), vmin=-1, vmax=1, annot=True, cmap='BrBG')\nheatmap.set_title('Features Correlating with Age', fontdict={'fontsize':18}, pad=16);","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:57.952921Z","iopub.execute_input":"2022-07-13T12:29:57.953712Z","iopub.status.idle":"2022-07-13T12:29:58.257531Z","shell.execute_reply.started":"2022-07-13T12:29:57.953661Z","shell.execute_reply":"2022-07-13T12:29:58.256432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From here we can clearly assume that Pclass and Age are highly related.Therefore we will impute the missing value by median of the corresponding Pclass.\nAlso you must note that the median value will also be dependent on the Sex as the corr. only considers numerical features\n","metadata":{}},{"cell_type":"code","source":"df_all.groupby(['Pclass','Sex'])['Age'].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.259170Z","iopub.execute_input":"2022-07-13T12:29:58.259852Z","iopub.status.idle":"2022-07-13T12:29:58.272035Z","shell.execute_reply.started":"2022-07-13T12:29:58.259792Z","shell.execute_reply":"2022-07-13T12:29:58.270937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all['Age']=df_all.groupby(['Pclass','Sex'])['Age'].apply(lambda x: x.fillna(x.median()))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.273455Z","iopub.execute_input":"2022-07-13T12:29:58.274431Z","iopub.status.idle":"2022-07-13T12:29:58.288463Z","shell.execute_reply.started":"2022-07-13T12:29:58.274392Z","shell.execute_reply":"2022-07-13T12:29:58.287377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have imputed the Age, let's crosscheck. We have Fare,Cabin and Embarked left out.","metadata":{}},{"cell_type":"code","source":"df_all.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.289726Z","iopub.execute_input":"2022-07-13T12:29:58.290240Z","iopub.status.idle":"2022-07-13T12:29:58.301589Z","shell.execute_reply.started":"2022-07-13T12:29:58.290206Z","shell.execute_reply":"2022-07-13T12:29:58.300403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1.2.2 Embarked**","metadata":{}},{"cell_type":"code","source":"df_all[df_all.Embarked.isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.303324Z","iopub.execute_input":"2022-07-13T12:29:58.304294Z","iopub.status.idle":"2022-07-13T12:29:58.325543Z","shell.execute_reply.started":"2022-07-13T12:29:58.304239Z","shell.execute_reply":"2022-07-13T12:29:58.324361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://www.encyclopedia-titanica.org/titanic-survivor/amelia-icard.html\nIf you refer to this article, we can clearly infer that both of these survivors boarded from Southampton    ","metadata":{}},{"cell_type":"code","source":"df_all.Embarked.fillna('S',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.327133Z","iopub.execute_input":"2022-07-13T12:29:58.328570Z","iopub.status.idle":"2022-07-13T12:29:58.335596Z","shell.execute_reply.started":"2022-07-13T12:29:58.328522Z","shell.execute_reply":"2022-07-13T12:29:58.334421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1.2.3 Fare**\n\nWe will use the same technique as before and find out the correlations.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 12))\nheatmap = sns.heatmap(df_all.corr()[['Fare']].sort_values(by='Fare',ascending=False), vmin=-1, vmax=1, annot=True, cmap='BrBG')\nheatmap.set_title('Features Correlating with Fare', fontdict={'fontsize':18}, pad=16);","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.337184Z","iopub.execute_input":"2022-07-13T12:29:58.338190Z","iopub.status.idle":"2022-07-13T12:29:58.635760Z","shell.execute_reply.started":"2022-07-13T12:29:58.338128Z","shell.execute_reply":"2022-07-13T12:29:58.634927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can infer that Fare is highly dependent on the Pclass & Age. Also one should not forget that Embarked and sex are equally important.","metadata":{}},{"cell_type":"code","source":"df_all[df_all.Fare.isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.637211Z","iopub.execute_input":"2022-07-13T12:29:58.638367Z","iopub.status.idle":"2022-07-13T12:29:58.656070Z","shell.execute_reply.started":"2022-07-13T12:29:58.638328Z","shell.execute_reply":"2022-07-13T12:29:58.655023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.groupby(['Pclass','Sex','Age','Embarked'])['Fare'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.662663Z","iopub.execute_input":"2022-07-13T12:29:58.663365Z","iopub.status.idle":"2022-07-13T12:29:58.679338Z","shell.execute_reply.started":"2022-07-13T12:29:58.663324Z","shell.execute_reply":"2022-07-13T12:29:58.678150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We should safely fill this with 6.2375","metadata":{}},{"cell_type":"code","source":"df_all.Fare.fillna(6.2375,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.680571Z","iopub.execute_input":"2022-07-13T12:29:58.681124Z","iopub.status.idle":"2022-07-13T12:29:58.686254Z","shell.execute_reply.started":"2022-07-13T12:29:58.681071Z","shell.execute_reply":"2022-07-13T12:29:58.685298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.Fare.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.687743Z","iopub.execute_input":"2022-07-13T12:29:58.688723Z","iopub.status.idle":"2022-07-13T12:29:58.699701Z","shell.execute_reply.started":"2022-07-13T12:29:58.688686Z","shell.execute_reply":"2022-07-13T12:29:58.698743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1.2.4 Cabin**\n It turns out to be the first letter of the Cabin values are the decks in which the cabins are located. Those decks were mainly separated for one passenger class, but some of them were used by multiple passenger classes. \n \n Here we will be doing some feature engg. to handle the missing values of cabin and ultimately drop Cabin Feature.","metadata":{}},{"cell_type":"code","source":"df_all['Cabin'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.701039Z","iopub.execute_input":"2022-07-13T12:29:58.701929Z","iopub.status.idle":"2022-07-13T12:29:58.714689Z","shell.execute_reply.started":"2022-07-13T12:29:58.701890Z","shell.execute_reply":"2022-07-13T12:29:58.713723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"On the ship there were A-P Decks and we have only A,B,C,D,E,F,G,T survivers.","metadata":{}},{"cell_type":"code","source":"# Creating a Deck Column to extract the info from Cabin Column\n#(M stands for Missing values)\ndf_all['Deck']=df_all['Cabin'].apply(lambda x:x[0] if pd.notnull(x) else 'M')\nprint(df_all.groupby('Deck')['Pclass'].value_counts())\ndf_all.groupby('Deck')['Pclass'].value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:58.716174Z","iopub.execute_input":"2022-07-13T12:29:58.717248Z","iopub.status.idle":"2022-07-13T12:29:59.003998Z","shell.execute_reply.started":"2022-07-13T12:29:58.717206Z","shell.execute_reply":"2022-07-13T12:29:59.002993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n   * 100% of A, B and C decks are 1st class passengers\n   * Deck D has 87% 1st class and 13% 2nd class passengers\n   * Deck E has 83% 1st class, 10% 2nd class and 7% 3rd class passengers\n   * Deck F has 62% 2nd class and 38% 3rd class passengers\n   * 100% of G deck are 3rd class passengers\n   * There is one person on the boat deck in T cabin and he is a 1st class passenger. T cabin passenger has the closest resemblance to A deck passengers so he is grouped with A deck\n   * Passengers labeled as M are the missing values in Cabin feature. I don't think it is possible to find those passengers' real Deck so I decided to use M like a deck.\n   \n   **In Feature Engineering I will try Ordinal Encoding on this feature.*\n\n","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"\n#now we can safely, remove the cabin feature.\ndf_all.drop('Cabin',axis=1,inplace=True)\ndf_all.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.005275Z","iopub.execute_input":"2022-07-13T12:29:59.005801Z","iopub.status.idle":"2022-07-13T12:29:59.022155Z","shell.execute_reply.started":"2022-07-13T12:29:59.005765Z","shell.execute_reply":"2022-07-13T12:29:59.021027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. **FEATURE engg.**","metadata":{}},{"cell_type":"markdown","source":"## **2.1 Feature Interaction & Extraction**","metadata":{}},{"cell_type":"markdown","source":"*2.1.1 Name-Title*","metadata":{"execution":{"iopub.status.busy":"2022-07-13T07:37:43.792431Z","iopub.execute_input":"2022-07-13T07:37:43.792837Z","iopub.status.idle":"2022-07-13T07:37:43.800083Z","shell.execute_reply.started":"2022-07-13T07:37:43.792786Z","shell.execute_reply":"2022-07-13T07:37:43.798582Z"}}},{"cell_type":"code","source":"df_all['Name'].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.023858Z","iopub.execute_input":"2022-07-13T12:29:59.025075Z","iopub.status.idle":"2022-07-13T12:29:59.035678Z","shell.execute_reply.started":"2022-07-13T12:29:59.025027Z","shell.execute_reply":"2022-07-13T12:29:59.034324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name=df_all.Name.to_list()\ntype(name[0])\ndf_all['Title']=[s.split('.')[0].split(',')[-1].strip() for s in name]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.037441Z","iopub.execute_input":"2022-07-13T12:29:59.037879Z","iopub.status.idle":"2022-07-13T12:29:59.051301Z","shell.execute_reply.started":"2022-07-13T12:29:59.037838Z","shell.execute_reply":"2022-07-13T12:29:59.050090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_all['Title'])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.052952Z","iopub.execute_input":"2022-07-13T12:29:59.053701Z","iopub.status.idle":"2022-07-13T12:29:59.065788Z","shell.execute_reply.started":"2022-07-13T12:29:59.053653Z","shell.execute_reply":"2022-07-13T12:29:59.064525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.Title.value_counts().plot(kind='bar')\nplt.xticks(rotation=60)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.067506Z","iopub.execute_input":"2022-07-13T12:29:59.068606Z","iopub.status.idle":"2022-07-13T12:29:59.342669Z","shell.execute_reply.started":"2022-07-13T12:29:59.068561Z","shell.execute_reply":"2022-07-13T12:29:59.341529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encoding this feature will be of great help \nprint(df_all['Title'].map({'Mr':1,'Miss':2,'Mrs':2,'Master':3}).fillna(4).value_counts().plot(kind='bar'))\ndf_all['Title']=df_all['Title'].map({'Mr':1,'Miss':2,'Mrs':2,'Master':3}).fillna(4)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.344134Z","iopub.execute_input":"2022-07-13T12:29:59.344510Z","iopub.status.idle":"2022-07-13T12:29:59.480398Z","shell.execute_reply.started":"2022-07-13T12:29:59.344464Z","shell.execute_reply":"2022-07-13T12:29:59.479021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_all.drop('Name',axis=1,inplace=True)\ndf_all.info()\n#df_all['PassengerId']","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.482469Z","iopub.execute_input":"2022-07-13T12:29:59.483191Z","iopub.status.idle":"2022-07-13T12:29:59.502167Z","shell.execute_reply.started":"2022-07-13T12:29:59.483135Z","shell.execute_reply":"2022-07-13T12:29:59.501067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*****2.2.2 Parch & SibSp = FamilySize*****","metadata":{"execution":{"iopub.status.busy":"2022-07-13T08:20:35.817687Z","iopub.execute_input":"2022-07-13T08:20:35.818057Z","iopub.status.idle":"2022-07-13T08:20:35.824997Z","shell.execute_reply.started":"2022-07-13T08:20:35.818025Z","shell.execute_reply":"2022-07-13T08:20:35.823313Z"}}},{"cell_type":"code","source":"df_all['Fsize']=df_all['SibSp']+df_all['Parch']\ndf_all['Fsize'].value_counts()\n#df_all['Fsize'].plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.503781Z","iopub.execute_input":"2022-07-13T12:29:59.504126Z","iopub.status.idle":"2022-07-13T12:29:59.513379Z","shell.execute_reply.started":"2022-07-13T12:29:59.504096Z","shell.execute_reply":"2022-07-13T12:29:59.512591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g=sns.catplot(x='Fsize',y='Survived',data=df_all,kind='bar')\ng.set_ylabels('Survival')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:29:59.514855Z","iopub.execute_input":"2022-07-13T12:29:59.515223Z","iopub.status.idle":"2022-07-13T12:30:00.090246Z","shell.execute_reply.started":"2022-07-13T12:29:59.515191Z","shell.execute_reply":"2022-07-13T12:30:00.088925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encoding singles,small,medium and large families as {1,2,3,4}\ndf_all['family_size']=df_all.Fsize.map({0:1,1:2,2:2,3:2,4:3,5:3,6:3,7:4,10:4})","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.092003Z","iopub.execute_input":"2022-07-13T12:30:00.092898Z","iopub.status.idle":"2022-07-13T12:30:00.099868Z","shell.execute_reply.started":"2022-07-13T12:30:00.092860Z","shell.execute_reply":"2022-07-13T12:30:00.098963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g=sns.catplot(x='family_size',y='Survived',data=df_all,kind='bar')\ng.set_ylabels('Survival')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.101048Z","iopub.execute_input":"2022-07-13T12:30:00.101910Z","iopub.status.idle":"2022-07-13T12:30:00.506900Z","shell.execute_reply.started":"2022-07-13T12:30:00.101855Z","shell.execute_reply":"2022-07-13T12:30:00.505623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_all.drop(['SibSp','Parch'],axis=1,inplace=True)\ndf_all.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.508361Z","iopub.execute_input":"2022-07-13T12:30:00.509311Z","iopub.status.idle":"2022-07-13T12:30:00.525451Z","shell.execute_reply.started":"2022-07-13T12:30:00.509241Z","shell.execute_reply":"2022-07-13T12:30:00.523928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **2.2 Encoding**","metadata":{}},{"cell_type":"code","source":"df_all['Sex']=df_all['Sex'].astype('category')\ndf_all=pd.get_dummies(df_all,columns=['Sex'])\n#df_all.drop(['Sex_male'],axis=1,inplace=True)\ndf_all.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.527246Z","iopub.execute_input":"2022-07-13T12:30:00.527716Z","iopub.status.idle":"2022-07-13T12:30:00.562437Z","shell.execute_reply.started":"2022-07-13T12:30:00.527680Z","shell.execute_reply":"2022-07-13T12:30:00.561207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all['Embarked']=df_all['Embarked'].astype('category')\ndf_all=pd.get_dummies(df_all,columns=['Embarked'])\ndf_all.head()\n#OR\n#df_temp=pd.get_dummies(df_all['Embarked'],drop_first=True)\n#pd.concat with axis=1","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.564024Z","iopub.execute_input":"2022-07-13T12:30:00.564470Z","iopub.status.idle":"2022-07-13T12:30:00.594670Z","shell.execute_reply.started":"2022-07-13T12:30:00.564435Z","shell.execute_reply":"2022-07-13T12:30:00.593379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all['Deck']=df_all['Deck'].astype('category')\ndf_all=pd.get_dummies(df_all,columns=['Deck'])\ndf_all.head()\n#df_all=pd.get_dummies(df_all['Deck'],drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.596476Z","iopub.execute_input":"2022-07-13T12:30:00.597292Z","iopub.status.idle":"2022-07-13T12:30:00.628062Z","shell.execute_reply.started":"2022-07-13T12:30:00.597239Z","shell.execute_reply":"2022-07-13T12:30:00.626842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.629656Z","iopub.execute_input":"2022-07-13T12:30:00.630144Z","iopub.status.idle":"2022-07-13T12:30:00.657561Z","shell.execute_reply.started":"2022-07-13T12:30:00.630096Z","shell.execute_reply":"2022-07-13T12:30:00.656261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_all.drop(['Name','SibSp','Parch','Embarked_C','Deck_A','Sex_male'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.659234Z","iopub.execute_input":"2022-07-13T12:30:00.659548Z","iopub.status.idle":"2022-07-13T12:30:00.665016Z","shell.execute_reply.started":"2022-07-13T12:30:00.659518Z","shell.execute_reply":"2022-07-13T12:30:00.663649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all[\"Pclass\"] = df_all[\"Pclass\"].astype(\"category\")\ndf_all = pd.get_dummies(df_all, columns= [\"Pclass\"])\ndf_all.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.667003Z","iopub.execute_input":"2022-07-13T12:30:00.668344Z","iopub.status.idle":"2022-07-13T12:30:00.699137Z","shell.execute_reply.started":"2022-07-13T12:30:00.668293Z","shell.execute_reply":"2022-07-13T12:30:00.698095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all[\"Title\"] = df_all[\"Title\"].astype(\"category\")\ndf_all = pd.get_dummies(df_all, columns= [\"Title\"])\ndf_all.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.700226Z","iopub.execute_input":"2022-07-13T12:30:00.700997Z","iopub.status.idle":"2022-07-13T12:30:00.730706Z","shell.execute_reply.started":"2022-07-13T12:30:00.700962Z","shell.execute_reply":"2022-07-13T12:30:00.729574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_all.drop(['Fsize'],inplace=True,axis=1)\n#As ticket helps to locate cabin,embarkment,Fare which is already given lets drop it too.\ndf_all_new=df_all.drop(['Pclass_1','Title_1.0','Ticket'],axis=1)\n\ndf_all_new.info()\ndf_all_new.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.732341Z","iopub.execute_input":"2022-07-13T12:30:00.733587Z","iopub.status.idle":"2022-07-13T12:30:00.754836Z","shell.execute_reply.started":"2022-07-13T12:30:00.733535Z","shell.execute_reply":"2022-07-13T12:30:00.754021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **2.3 PreProcessing**\nhttps://www.kaggle.com/code/prashant111/a-reference-guide-to-feature-engineering-methods/notebook\n\nWe will be scaling and normalizing Age and Fare","metadata":{}},{"cell_type":"code","source":"# plot the histograms to have a quick look at the distributions\n# we can plot Q-Q plots to visualise if the variable is normally distributed\n\ndef diagnostic_plots(df, variable):\n    # function to plot a histogram and a Q-Q plot\n    # side by side, for a certain variable\n    \n    plt.figure(figsize=(15,6))\n    plt.subplot(1, 2, 1)\n    df[variable].hist()\n\n    plt.subplot(1, 2, 2)\n    stats.probplot(df[variable], dist=\"norm\", plot=pylab)\n\n    plt.show()\n    \ndiagnostic_plots(df_all_new, 'Fare')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:00.756028Z","iopub.execute_input":"2022-07-13T12:30:00.757091Z","iopub.status.idle":"2022-07-13T12:30:01.176878Z","shell.execute_reply.started":"2022-07-13T12:30:00.757055Z","shell.execute_reply":"2022-07-13T12:30:01.176039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all_new.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:01.178003Z","iopub.execute_input":"2022-07-13T12:30:01.179043Z","iopub.status.idle":"2022-07-13T12:30:01.189901Z","shell.execute_reply.started":"2022-07-13T12:30:01.178993Z","shell.execute_reply":"2022-07-13T12:30:01.188716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_all['Fare_log']=np.log(df_all.Fare)\n#diagnostic_plots(df_all,'Fare_log')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:01.191320Z","iopub.execute_input":"2022-07-13T12:30:01.192439Z","iopub.status.idle":"2022-07-13T12:30:01.198627Z","shell.execute_reply.started":"2022-07-13T12:30:01.192404Z","shell.execute_reply":"2022-07-13T12:30:01.197481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all_new['fare_sqr'] =df_all_new.Fare**(1/2)\n\ndiagnostic_plots(df_all_new, 'fare_sqr')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:01.199960Z","iopub.execute_input":"2022-07-13T12:30:01.200482Z","iopub.status.idle":"2022-07-13T12:30:01.610975Z","shell.execute_reply.started":"2022-07-13T12:30:01.200448Z","shell.execute_reply":"2022-07-13T12:30:01.609531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all_new['Fare_exp']=df_all_new.Fare**(1/1.2)\ndiagnostic_plots(df_all_new,'Fare_exp')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:01.615009Z","iopub.execute_input":"2022-07-13T12:30:01.615393Z","iopub.status.idle":"2022-07-13T12:30:02.032303Z","shell.execute_reply.started":"2022-07-13T12:30:01.615358Z","shell.execute_reply":"2022-07-13T12:30:02.030138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_all_new['Fare_boxcox'], param = stats.boxcox(df_all_new.Fare) \n\n#print('Optimal λ: ', param)\n\n#diagnostic_plots(data, 'Fare_boxcox')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.034411Z","iopub.execute_input":"2022-07-13T12:30:02.034922Z","iopub.status.idle":"2022-07-13T12:30:02.041080Z","shell.execute_reply.started":"2022-07-13T12:30:02.034869Z","shell.execute_reply":"2022-07-13T12:30:02.039225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all_new['Age_boxcox'], param = stats.boxcox(df_all_new.Age) \n\nprint('Optimal λ: ', param)\n\ndiagnostic_plots(df_all_new, 'Age_boxcox')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.042819Z","iopub.execute_input":"2022-07-13T12:30:02.043919Z","iopub.status.idle":"2022-07-13T12:30:02.507206Z","shell.execute_reply.started":"2022-07-13T12:30:02.043875Z","shell.execute_reply":"2022-07-13T12:30:02.505569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion: We will be using Age_boxcox and fare_sqr**","metadata":{}},{"cell_type":"code","source":"df_all_new.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.508549Z","iopub.execute_input":"2022-07-13T12:30:02.508951Z","iopub.status.idle":"2022-07-13T12:30:02.527642Z","shell.execute_reply.started":"2022-07-13T12:30:02.508914Z","shell.execute_reply":"2022-07-13T12:30:02.526745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all_new.drop(['Fare_exp','Fare','Age'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.529066Z","iopub.execute_input":"2022-07-13T12:30:02.529686Z","iopub.status.idle":"2022-07-13T12:30:02.536517Z","shell.execute_reply.started":"2022-07-13T12:30:02.529650Z","shell.execute_reply":"2022-07-13T12:30:02.535375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_all_new.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.537932Z","iopub.execute_input":"2022-07-13T12:30:02.538402Z","iopub.status.idle":"2022-07-13T12:30:02.559543Z","shell.execute_reply.started":"2022-07-13T12:30:02.538368Z","shell.execute_reply":"2022-07-13T12:30:02.558075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def divide_df(all_data):\n    # Returns divided dfs of training and test set\n    return all_data.loc[:890].drop(['PassengerId'],axis=1), all_data.loc[891:].drop(['Survived','PassengerId'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.561196Z","iopub.execute_input":"2022-07-13T12:30:02.561524Z","iopub.status.idle":"2022-07-13T12:30:02.567721Z","shell.execute_reply.started":"2022-07-13T12:30:02.561495Z","shell.execute_reply":"2022-07-13T12:30:02.566746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train,df_test=divide_df(df_all_new)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.569348Z","iopub.execute_input":"2022-07-13T12:30:02.570528Z","iopub.status.idle":"2022-07-13T12:30:02.584701Z","shell.execute_reply.started":"2022-07-13T12:30:02.570451Z","shell.execute_reply":"2022-07-13T12:30:02.583075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf_train.drop('Name',inplace=True,axis=1)\ndf_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.586461Z","iopub.execute_input":"2022-07-13T12:30:02.587693Z","iopub.status.idle":"2022-07-13T12:30:02.600182Z","shell.execute_reply.started":"2022-07-13T12:30:02.587642Z","shell.execute_reply":"2022-07-13T12:30:02.598589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf_test.drop('Name',inplace=True,axis=1)\ndf_test.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.601885Z","iopub.execute_input":"2022-07-13T12:30:02.603242Z","iopub.status.idle":"2022-07-13T12:30:02.614562Z","shell.execute_reply.started":"2022-07-13T12:30:02.603163Z","shell.execute_reply":"2022-07-13T12:30:02.613435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df_train.drop('Survived',axis=1)\ntarget=df_train.Survived\nX_train,X_test,y_train,y_test=train_test_split(df,target,test_size=0.3,random_state=SEED)\nX_train.shape,X_test.shape,y_train.shape,y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.615957Z","iopub.execute_input":"2022-07-13T12:30:02.616663Z","iopub.status.idle":"2022-07-13T12:30:02.632497Z","shell.execute_reply.started":"2022-07-13T12:30:02.616625Z","shell.execute_reply":"2022-07-13T12:30:02.631201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **2.3 Modelling**","metadata":{}},{"cell_type":"code","source":"model = RandomForestClassifier(criterion='gini', n_estimators=700,\n                             min_samples_split=10,min_samples_leaf=1,\n                             max_features='auto',oob_score=True,\n                             random_state=1,n_jobs=-1)\n#model.fit(X_train,y_train)\n#prediction_rm=model.predict(X_test)\nprint('--------------The Accuracy of the model----------------------------')\n#print('The accuracy of the Random Forest Classifier is',round(accuracy_score(prediction_rm,y_test)*100,2))\n\nresult_rm=cross_val_score(model,df,target,cv=10,scoring='accuracy')\nprint('The cross validated score for Random Forest Classifier is:',round(result_rm.mean()*100,2))\n# for cross_val_pred no need of cross_val fitting\ny_pred = cross_val_predict(model,df,target,cv=10)\nsns.heatmap(confusion_matrix(target,y_pred),annot=True,fmt='3.0f',cmap=\"summer\")\nplt.title('Confusion_matrix', y=1.05, size=15)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:02.634041Z","iopub.execute_input":"2022-07-13T12:30:02.634902Z","iopub.status.idle":"2022-07-13T12:30:32.068740Z","shell.execute_reply.started":"2022-07-13T12:30:02.634859Z","shell.execute_reply":"2022-07-13T12:30:32.067060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(df,target)\npred_test=model.predict(df_test)\ndf_test\n#Comparison between models and tuning needs to be done","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:30:32.070944Z","iopub.execute_input":"2022-07-13T12:30:32.072334Z","iopub.status.idle":"2022-07-13T12:30:34.462712Z","shell.execute_reply.started":"2022-07-13T12:30:32.072178Z","shell.execute_reply":"2022-07-13T12:30:34.461309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsubmission_df['Survived']=pred_test\nsubmission_df\nsubmission_df.to_csv('submissions.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T12:44:07.558567Z","iopub.execute_input":"2022-07-13T12:44:07.559271Z","iopub.status.idle":"2022-07-13T12:44:07.567360Z","shell.execute_reply.started":"2022-07-13T12:44:07.559230Z","shell.execute_reply":"2022-07-13T12:44:07.565888Z"},"trusted":true},"execution_count":null,"outputs":[]}]}