{"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()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.237874Z","iopub.execute_input":"2022-07-15T15:28:31.238327Z","iopub.status.idle":"2022-07-15T15:28:31.820355Z","shell.execute_reply.started":"2022-07-15T15:28:31.238207Z","shell.execute_reply":"2022-07-15T15:28:31.818930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.822901Z","iopub.execute_input":"2022-07-15T15:28:31.823617Z","iopub.status.idle":"2022-07-15T15:28:31.827949Z","shell.execute_reply.started":"2022-07-15T15:28:31.823578Z","shell.execute_reply":"2022-07-15T15:28:31.827181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.829416Z","iopub.execute_input":"2022-07-15T15:28:31.829740Z","iopub.status.idle":"2022-07-15T15:28:31.847510Z","shell.execute_reply.started":"2022-07-15T15:28:31.829709Z","shell.execute_reply":"2022-07-15T15:28:31.846294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.851247Z","iopub.execute_input":"2022-07-15T15:28:31.851991Z","iopub.status.idle":"2022-07-15T15:28:31.857814Z","shell.execute_reply.started":"2022-07-15T15:28:31.851945Z","shell.execute_reply":"2022-07-15T15:28:31.856926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.859571Z","iopub.execute_input":"2022-07-15T15:28:31.860308Z","iopub.status.idle":"2022-07-15T15:28:31.886486Z","shell.execute_reply.started":"2022-07-15T15:28:31.860262Z","shell.execute_reply":"2022-07-15T15:28:31.884420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.888519Z","iopub.execute_input":"2022-07-15T15:28:31.889391Z","iopub.status.idle":"2022-07-15T15:28:31.924661Z","shell.execute_reply.started":"2022-07-15T15:28:31.889328Z","shell.execute_reply":"2022-07-15T15:28:31.923437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.925732Z","iopub.execute_input":"2022-07-15T15:28:31.926390Z","iopub.status.idle":"2022-07-15T15:28:31.940527Z","shell.execute_reply.started":"2022-07-15T15:28:31.926357Z","shell.execute_reply":"2022-07-15T15:28:31.939281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['PassengerId'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.941914Z","iopub.execute_input":"2022-07-15T15:28:31.942265Z","iopub.status.idle":"2022-07-15T15:28:31.960130Z","shell.execute_reply.started":"2022-07-15T15:28:31.942224Z","shell.execute_reply":"2022-07-15T15:28:31.958517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets drop the 'PassengerId' column as it just a unique number for each instance & it won't help us in predictions\ndf=df.drop('PassengerId',axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.962005Z","iopub.execute_input":"2022-07-15T15:28:31.963131Z","iopub.status.idle":"2022-07-15T15:28:31.972817Z","shell.execute_reply.started":"2022-07-15T15:28:31.963070Z","shell.execute_reply":"2022-07-15T15:28:31.971090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets separate Numerical Features and Catagorical features to gets some insights:","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.977253Z","iopub.execute_input":"2022-07-15T15:28:31.978376Z","iopub.status.idle":"2022-07-15T15:28:31.986585Z","shell.execute_reply.started":"2022-07-15T15:28:31.978321Z","shell.execute_reply":"2022-07-15T15:28:31.985622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat=df.select_dtypes(include='object') #Categorical Dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:31.988246Z","iopub.execute_input":"2022-07-15T15:28:31.988988Z","iopub.status.idle":"2022-07-15T15:28:32.002775Z","shell.execute_reply.started":"2022-07-15T15:28:31.988942Z","shell.execute_reply":"2022-07-15T15:28:32.000552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.head() ","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.004320Z","iopub.execute_input":"2022-07-15T15:28:32.005349Z","iopub.status.idle":"2022-07-15T15:28:32.027304Z","shell.execute_reply.started":"2022-07-15T15:28:32.005314Z","shell.execute_reply":"2022-07-15T15:28:32.026032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num=df.select_dtypes(exclude='object') #Numerical Dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.028852Z","iopub.execute_input":"2022-07-15T15:28:32.029580Z","iopub.status.idle":"2022-07-15T15:28:32.039140Z","shell.execute_reply.started":"2022-07-15T15:28:32.029528Z","shell.execute_reply":"2022-07-15T15:28:32.037239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.041332Z","iopub.execute_input":"2022-07-15T15:28:32.042300Z","iopub.status.idle":"2022-07-15T15:28:32.061014Z","shell.execute_reply.started":"2022-07-15T15:28:32.042250Z","shell.execute_reply":"2022-07-15T15:28:32.059730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets check insights of features:","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.062489Z","iopub.execute_input":"2022-07-15T15:28:32.064303Z","iopub.status.idle":"2022-07-15T15:28:32.073636Z","shell.execute_reply.started":"2022-07-15T15:28:32.064183Z","shell.execute_reply":"2022-07-15T15:28:32.072514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class feature_details:\n    \n    \n    def categorical_feature_details(self,categorical_df):\n        \n        print(\"\\n==================================================================\")\n        print(\"                   CATEGORICAL FEATURE DETAILS                     \")\n        print(\"==================================================================\")\n        \n        cols=categorical_df.columns\n    \n        for col in cols:\n            print(\"\\n\\n________________\")\n            print(\"\\nFeature: {}\".format(col))\n            print(\"________________\")\n\n\n\n            #nulls\n            \n            print('\\n\\nNULLS:-\\n')\n            nulls=df[col].isnull().sum()\n            pernulls=np.round((nulls/(len(df[col])))*100,2)\n            print(\"    {} % Null values \\n    (i.e Out of {} instaces, there are {} Number of null values)\".format(pernulls,len(df[col]),nulls))\n\n\n\n            #uniques\n\n            print('\\nUNIQUES:-\\n')\n            nuniqs=df[col].nunique()\n            print(\"    Total {} unique values\".format(nuniqs))\n\n            uniqs=df[col].unique()\n            print(\"\\n    Unique values are:\")\n            for i in range(len(uniqs)):\n                print('    ',i+1,'-',uniqs[i])\n                \n                \n                \n                \n    def categorywise_outliers_details(self,categorical_df,target_column):\n        \n        df=pd.concat([categorical_df,target_column],axis=1)\n        targetcol=target_column.name\n        cols=df.columns\n        \n        \n        print(\"\\n=====================================================================\")\n        print(\"\\n                   CATEGORYWISE OUTLIERS DETAILS:                    \")\n        print(\"               (with respect to target variable: {} )               \\n\".format(targetcol))\n\n        print(\"=====================================================================\")\n        \n        \n        for i in range(len(cols)):\n\n            col=cols[i]\n\n            if col==targetcol:\n                continue\n            else:\n                print(\"\\n\\n________________\")\n                print(\"\\nCOLUMN: {} \".format(col.upper()))\n                print(\"________________\")\n                cats=df[col].unique()\n            \n            for i in range(len(cats)):\n                cat=cats[i]\n\n                print(\"\\n\\n      {}. Category: {} \\n\".format(i+1,cat))\n\n\n\n                q75,q25=np.percentile(df[df[col]==cat][targetcol],[75,25])\n                iqr=np.round(q75-q25,3)\n                upperlim=np.round(q75+(1.5*iqr),3)\n                lowerlim=np.round(q25-(1.5*iqr),3)\n\n\n\n                upper_indexes=df[(df[col]==cat) & (df[targetcol]>upperlim)].index\n                lower_indexes=df[(df[col]==cat) & (df[targetcol]<lowerlim)].index\n\n                mini=df[df[col]==cat][targetcol].min()\n                maxi=df[df[col]==cat][targetcol].max()\n                avg=np.round(df[df[col]==cat][targetcol].mean(),2)\n                print(\"                Minimum {} for {} is: {}\".format(targetcol,cat,mini))\n                print(\"                Maximum {} for {} is: {}\".format(targetcol,cat,maxi))\n                print(\"                Average {} for {} is: {}\\n\".format(targetcol,cat,avg))\n                \n                print(\"                Upper Limit is {}\".format(upperlim))\n                print(\"                Lower Limit is {}\".format(lowerlim))\n                \n                \n                \n                if (len(upper_indexes)>0) or (len(lower_indexes)>0):\n\n                    if len(upper_indexes)>0:\n                        print(\"\\n                  >>> HIGHER OUTLIERS: {} \".format(len(upper_indexes)))\n                        print(\"                \\nOutliers are at index nos: \\n\",list(upper_indexes))\n                        \n\n                    if len(lower_indexes)>0:\n                        print(\"                  >>> LOWER OUTLIERS: {} \\n\".format(len(lower_indexes)))\n                        print(\"                \\nOutliers are at index nos: \\n\",list(lower_indexes))\n                        \n\n                else:\n                    print(\"                  NO OUTLIERS\")\n                    \n                    \n                \n            \n                \n\n                \n                \n    def numerical_feature_details(self,numerical_df):\n        print(\"\\n==================================================================\")\n        print(\"                   NUMERICAL FEATURE DETAILS                     \")\n        print(\"==================================================================\")\n        \n        cols=numerical_df.columns\n\n        for col in cols:\n            print(\"\\n\\n________________\")\n            print(\"\\nFeature: {}\".format(col))\n            print(\"________________\\n\")\n\n\n            \n            mins=df[col].min()\n            maxs=df[col].max()\n            avgs=np.round(df[col].mean(),2)\n            \n            print(\"Minimum {} is {}\".format(col,mins))\n            print(\"Maximum {} is {}\".format(col,maxs))\n            print(\"Average {} is {}\".format(col,avgs))\n            \n            \n            \n            \n            #nulls\n\n            print('\\n\\nNULLS:-\\n')\n            nulls=df[col].isnull().sum()\n            pernulls=np.round((nulls/(len(df[col])))*100,2)\n            print(\"    {} % Null values \\n    (i.e Out of {} instaces, there are {} Number of null values)\".format(pernulls,len(df[col]),nulls))\n\n\n\n            #uniques\n\n            print('\\nUNIQUES:-\\n')\n            nuniqs=df[col].nunique()\n            print(\"    Total {} unique values\".format(nuniqs))\n\n\n\n            #outliers\n            \n            print('\\nOUTLIERS:-\\n')\n\n\n            q75,q25=np.percentile(df[col],[75,25])\n            iqr=np.round(q75-q25,3)\n            upperlim=np.round(q75+(1.5*iqr),3)\n            lowerlim=np.round(q25-(1.5*iqr),3)\n            \n            print(\"    25th Percentile is: \",q25)\n            print(\"    75th Percentile is: \",q75)\n\n            print(\"    Inter Quartile range is: \",iqr)\n\n            print(\"    Upper limit is: \", upperlim)\n            print(\"    Lower limit is: \", lowerlim)\n                        \n            print('\\n')    \n            print(\"    >>> HIGHER OUTLIERS: {} \".format(len(df[df[col]>upperlim])))\n            print(\"    >>> LOWER OUTLIERS: {} \".format(len(df[df[col]<lowerlim])))\n            print('\\n\\n')\n\n\n            \n            \n\n       ","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.075316Z","iopub.execute_input":"2022-07-15T15:28:32.076037Z","iopub.status.idle":"2022-07-15T15:28:32.113809Z","shell.execute_reply.started":"2022-07-15T15:28:32.075995Z","shell.execute_reply":"2022-07-15T15:28:32.112557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_details=feature_details()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.115192Z","iopub.execute_input":"2022-07-15T15:28:32.115752Z","iopub.status.idle":"2022-07-15T15:28:32.130823Z","shell.execute_reply.started":"2022-07-15T15:28:32.115720Z","shell.execute_reply":"2022-07-15T15:28:32.129398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Categorical Features details\nfeature_details.categorical_feature_details(df_cat)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:28:32.132234Z","iopub.execute_input":"2022-07-15T15:28:32.133258Z","iopub.status.idle":"2022-07-15T15:28:32.202116Z","shell.execute_reply.started":"2022-07-15T15:28:32.133155Z","shell.execute_reply":"2022-07-15T15:28:32.200071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"77.1 % values in 'Cabin' feature are null\n    (i.e There are 687 Number of null values Out of 891 instaces)\n    \n0.22% values in 'Embarked' feature are null\n    (i.e There are 2 Number of null values Out of 891 instaces)","metadata":{}},{"cell_type":"code","source":"#lets Check for nulls in 'cabin' feature:\ndf['Cabin'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.203774Z","iopub.execute_input":"2022-07-15T15:28:32.204797Z","iopub.status.idle":"2022-07-15T15:28:32.215168Z","shell.execute_reply.started":"2022-07-15T15:28:32.204763Z","shell.execute_reply":"2022-07-15T15:28:32.214002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#filling null values with string 'Nan'\ndf['Cabin']=df['Cabin'].fillna('Nan')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.217035Z","iopub.execute_input":"2022-07-15T15:28:32.217466Z","iopub.status.idle":"2022-07-15T15:28:32.228448Z","shell.execute_reply.started":"2022-07-15T15:28:32.217429Z","shell.execute_reply":"2022-07-15T15:28:32.227287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting feature Dtype to object\ndf['Cabin']=df['Cabin'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.229423Z","iopub.execute_input":"2022-07-15T15:28:32.229742Z","iopub.status.idle":"2022-07-15T15:28:32.241805Z","shell.execute_reply.started":"2022-07-15T15:28:32.229713Z","shell.execute_reply":"2022-07-15T15:28:32.240119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Bringing out first initial of Cabin value as i suppose it represent Deck\n\ndecks=[]\nfor i in range(len(df['Cabin'])):\n    if df['Cabin'][i] == 'Nan':\n        continue\n    else:\n        decks.append(df['Cabin'][i][0])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.243634Z","iopub.execute_input":"2022-07-15T15:28:32.244165Z","iopub.status.idle":"2022-07-15T15:28:32.267335Z","shell.execute_reply.started":"2022-07-15T15:28:32.244110Z","shell.execute_reply":"2022-07-15T15:28:32.265550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting values in cabin feature into initials (i.e Deck) to check survival rate by Decks \ndf['Cabin']=df['Cabin'].apply(lambda x:x[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.268690Z","iopub.execute_input":"2022-07-15T15:28:32.269480Z","iopub.status.idle":"2022-07-15T15:28:32.285428Z","shell.execute_reply.started":"2022-07-15T15:28:32.269439Z","shell.execute_reply":"2022-07-15T15:28:32.284188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dataframe without Nulls\ndf[df['Cabin']!='N']","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.289017Z","iopub.execute_input":"2022-07-15T15:28:32.290284Z","iopub.status.idle":"2022-07-15T15:28:32.318452Z","shell.execute_reply.started":"2022-07-15T15:28:32.290193Z","shell.execute_reply":"2022-07-15T15:28:32.317234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.Figure(figsize=(10,6),dpi=75)\nax=sns.countplot(data=df[df['Cabin']!='N'],x='Cabin',hue='Survived')\nfor i in ax.containers:\n    ax.bar_label(i,)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.320065Z","iopub.execute_input":"2022-07-15T15:28:32.320681Z","iopub.status.idle":"2022-07-15T15:28:32.700516Z","shell.execute_reply.started":"2022-07-15T15:28:32.320643Z","shell.execute_reply":"2022-07-15T15:28:32.699488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Observation:\n\n    - We dont have much information on 'Cabin' feature, still if we look at the above graph and the Cross Section \n      of Titanic. with whatever information we have it is strange to see that the majority of people who couldn't survive\n      are from upperdecks i.e 'B'&'C'.\n      \n    - There is nothing more we can get with such small data from 'Cabin' feature, So it is better to drop this feature.","metadata":{}},{"cell_type":"code","source":"df_cat=df_cat.drop('Cabin',axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.702634Z","iopub.execute_input":"2022-07-15T15:28:32.702956Z","iopub.status.idle":"2022-07-15T15:28:32.708985Z","shell.execute_reply.started":"2022-07-15T15:28:32.702927Z","shell.execute_reply":"2022-07-15T15:28:32.707619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.710357Z","iopub.execute_input":"2022-07-15T15:28:32.711460Z","iopub.status.idle":"2022-07-15T15:28:32.727892Z","shell.execute_reply.started":"2022-07-15T15:28:32.711411Z","shell.execute_reply":"2022-07-15T15:28:32.726946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#There were some null values in 'Embarked' features also, Lets check them:\n\ndf_cat[df_cat['Embarked'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.737549Z","iopub.execute_input":"2022-07-15T15:28:32.738409Z","iopub.status.idle":"2022-07-15T15:28:32.750907Z","shell.execute_reply.started":"2022-07-15T15:28:32.738374Z","shell.execute_reply":"2022-07-15T15:28:32.749736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Embarked'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.752336Z","iopub.execute_input":"2022-07-15T15:28:32.753054Z","iopub.status.idle":"2022-07-15T15:28:32.763819Z","shell.execute_reply.started":"2022-07-15T15:28:32.753017Z","shell.execute_reply":"2022-07-15T15:28:32.762987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets fill the Null values with 'mode' i.e 'S'","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.765087Z","iopub.execute_input":"2022-07-15T15:28:32.766072Z","iopub.status.idle":"2022-07-15T15:28:32.770935Z","shell.execute_reply.started":"2022-07-15T15:28:32.766036Z","shell.execute_reply":"2022-07-15T15:28:32.770054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat['Embarked']=df_cat['Embarked'].fillna(df_cat['Embarked'].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.772194Z","iopub.execute_input":"2022-07-15T15:28:32.773246Z","iopub.status.idle":"2022-07-15T15:28:32.782901Z","shell.execute_reply.started":"2022-07-15T15:28:32.773175Z","shell.execute_reply":"2022-07-15T15:28:32.782087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat['Embarked'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.784131Z","iopub.execute_input":"2022-07-15T15:28:32.785195Z","iopub.status.idle":"2022-07-15T15:28:32.799143Z","shell.execute_reply.started":"2022-07-15T15:28:32.785151Z","shell.execute_reply":"2022-07-15T15:28:32.797816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.isnull().sum()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:28:32.800663Z","iopub.execute_input":"2022-07-15T15:28:32.801738Z","iopub.status.idle":"2022-07-15T15:28:32.816971Z","shell.execute_reply.started":"2022-07-15T15:28:32.801693Z","shell.execute_reply":"2022-07-15T15:28:32.815740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets do this same for main df also,\ndf['Embarked']=df['Embarked'].fillna(df['Embarked'].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.818284Z","iopub.execute_input":"2022-07-15T15:28:32.819410Z","iopub.status.idle":"2022-07-15T15:28:32.829988Z","shell.execute_reply.started":"2022-07-15T15:28:32.819376Z","shell.execute_reply":"2022-07-15T15:28:32.828854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Numerical Features details\nfeature_details.numerical_feature_details(df_num)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.831415Z","iopub.execute_input":"2022-07-15T15:28:32.832287Z","iopub.status.idle":"2022-07-15T15:28:32.855845Z","shell.execute_reply.started":"2022-07-15T15:28:32.832244Z","shell.execute_reply":"2022-07-15T15:28:32.854683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Feature 'Age' has (19.87%) null values, lets check them.","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.857245Z","iopub.execute_input":"2022-07-15T15:28:32.857597Z","iopub.status.idle":"2022-07-15T15:28:32.862634Z","shell.execute_reply.started":"2022-07-15T15:28:32.857566Z","shell.execute_reply":"2022-07-15T15:28:32.861323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Counts of instances with Siblings or Spouse\nplt.Figure(figsize=(10,6),dpi=75)\n\nax=sns.countplot(data=df,x='SibSp',hue='Sex')\nfor i in ax.containers:\n    ax.bar_label(i,)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:32.864390Z","iopub.execute_input":"2022-07-15T15:28:32.865090Z","iopub.status.idle":"2022-07-15T15:28:33.175921Z","shell.execute_reply.started":"2022-07-15T15:28:32.865042Z","shell.execute_reply":"2022-07-15T15:28:33.175155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Counts of instances with Parents or Childrens\n\nplt.Figure(figsize=(10,6),dpi=75)\n\nax=sns.countplot(data=df,x='Parch',hue='Sex')\nfor i in ax.containers:\n    ax.bar_label(i,)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:33.177413Z","iopub.execute_input":"2022-07-15T15:28:33.177711Z","iopub.status.idle":"2022-07-15T15:28:33.493025Z","shell.execute_reply.started":"2022-07-15T15:28:33.177684Z","shell.execute_reply":"2022-07-15T15:28:33.491745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Average fare\n\nplt.Figure(figsize=(10,6),dpi=75)\n\nax=sns.barplot(data=df,x='Sex',y='Fare',ci=None)\nax.bar_label(ax.containers[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:33.494383Z","iopub.execute_input":"2022-07-15T15:28:33.495234Z","iopub.status.idle":"2022-07-15T15:28:33.680177Z","shell.execute_reply.started":"2022-07-15T15:28:33.495179Z","shell.execute_reply":"2022-07-15T15:28:33.678991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Average fare with Siblings or Spouse\n\nplt.Figure(figsize=(10,6),dpi=75)\nax=sns.barplot(data=df,x='SibSp',hue='Sex',y='Fare',ci=None)\nfor i in ax.containers:\n    ax.bar_label(i,)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:28:33.681789Z","iopub.execute_input":"2022-07-15T15:28:33.682241Z","iopub.status.idle":"2022-07-15T15:28:34.019158Z","shell.execute_reply.started":"2022-07-15T15:28:33.682195Z","shell.execute_reply":"2022-07-15T15:28:34.018291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Average fare with Parents or Children\n\nplt.Figure(figsize=(10,10),dpi=75)\nax=sns.barplot(data=df,x='Parch',hue='Sex',y='Fare',ci=None)\nfor i in ax.containers:\n    ax.bar_label(i,)\n    \nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:34.020666Z","iopub.execute_input":"2022-07-15T15:28:34.021277Z","iopub.status.idle":"2022-07-15T15:28:34.434140Z","shell.execute_reply.started":"2022-07-15T15:28:34.021229Z","shell.execute_reply":"2022-07-15T15:28:34.433145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.Figure(figsize=(10,6),dpi=75)\nsns.heatmap(df.corr(),annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:34.435945Z","iopub.execute_input":"2022-07-15T15:28:34.436341Z","iopub.status.idle":"2022-07-15T15:28:34.867686Z","shell.execute_reply.started":"2022-07-15T15:28:34.436306Z","shell.execute_reply":"2022-07-15T15:28:34.866397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets fill missing values of age by categoriwise mean","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:34.869564Z","iopub.execute_input":"2022-07-15T15:28:34.870231Z","iopub.status.idle":"2022-07-15T15:28:34.875179Z","shell.execute_reply.started":"2022-07-15T15:28:34.870176Z","shell.execute_reply":"2022-07-15T15:28:34.873540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby(['Survived', 'Pclass','Sex','SibSp','Parch','Embarked']).mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:34.877167Z","iopub.execute_input":"2022-07-15T15:28:34.877534Z","iopub.status.idle":"2022-07-15T15:28:34.910783Z","shell.execute_reply.started":"2022-07-15T15:28:34.877503Z","shell.execute_reply":"2022-07-15T15:28:34.909937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'] = df.groupby(['Survived', 'Pclass','Sex','SibSp','Parch','Embarked'])['Age'].transform(lambda x: x.fillna(x.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:34.911850Z","iopub.execute_input":"2022-07-15T15:28:34.912734Z","iopub.status.idle":"2022-07-15T15:28:34.968877Z","shell.execute_reply.started":"2022-07-15T15:28:34.912698Z","shell.execute_reply":"2022-07-15T15:28:34.967969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:34.970042Z","iopub.execute_input":"2022-07-15T15:28:34.971057Z","iopub.status.idle":"2022-07-15T15:28:34.978104Z","shell.execute_reply.started":"2022-07-15T15:28:34.971019Z","shell.execute_reply":"2022-07-15T15:28:34.976985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'] = df.groupby(['Survived', 'Pclass','Sex','SibSp','Parch'])['Age'].transform(lambda x: x.fillna(x.mean()))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:28:34.979481Z","iopub.execute_input":"2022-07-15T15:28:34.979820Z","iopub.status.idle":"2022-07-15T15:28:35.028696Z","shell.execute_reply.started":"2022-07-15T15:28:34.979791Z","shell.execute_reply":"2022-07-15T15:28:35.027564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.030428Z","iopub.execute_input":"2022-07-15T15:28:35.030771Z","iopub.status.idle":"2022-07-15T15:28:35.038951Z","shell.execute_reply.started":"2022-07-15T15:28:35.030740Z","shell.execute_reply":"2022-07-15T15:28:35.038064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'] = df.groupby(['Survived', 'Pclass','Sex','SibSp'])['Age'].transform(lambda x: x.fillna(x.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.040343Z","iopub.execute_input":"2022-07-15T15:28:35.040857Z","iopub.status.idle":"2022-07-15T15:28:35.072957Z","shell.execute_reply.started":"2022-07-15T15:28:35.040826Z","shell.execute_reply":"2022-07-15T15:28:35.071501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.077361Z","iopub.execute_input":"2022-07-15T15:28:35.077926Z","iopub.status.idle":"2022-07-15T15:28:35.087070Z","shell.execute_reply.started":"2022-07-15T15:28:35.077877Z","shell.execute_reply":"2022-07-15T15:28:35.085780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'] = df.groupby(['Survived', 'Pclass','Sex'])['Age'].transform(lambda x: x.fillna(x.mean()))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.088708Z","iopub.execute_input":"2022-07-15T15:28:35.089982Z","iopub.status.idle":"2022-07-15T15:28:35.105878Z","shell.execute_reply.started":"2022-07-15T15:28:35.089932Z","shell.execute_reply":"2022-07-15T15:28:35.104760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.107712Z","iopub.execute_input":"2022-07-15T15:28:35.108608Z","iopub.status.idle":"2022-07-15T15:28:35.119293Z","shell.execute_reply.started":"2022-07-15T15:28:35.108550Z","shell.execute_reply":"2022-07-15T15:28:35.118018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets redefine Numerical dataframe as we have missing values with respect to some categorical features\n\ndf_num=df.select_dtypes(exclude='object') #Numerical Dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.121056Z","iopub.execute_input":"2022-07-15T15:28:35.121705Z","iopub.status.idle":"2022-07-15T15:28:35.134454Z","shell.execute_reply.started":"2022-07-15T15:28:35.121658Z","shell.execute_reply":"2022-07-15T15:28:35.133331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.136011Z","iopub.execute_input":"2022-07-15T15:28:35.136558Z","iopub.status.idle":"2022-07-15T15:28:35.146692Z","shell.execute_reply.started":"2022-07-15T15:28:35.136520Z","shell.execute_reply":"2022-07-15T15:28:35.145678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets recheck Numerical Features details\nfeature_details.numerical_feature_details(df_num)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.147986Z","iopub.execute_input":"2022-07-15T15:28:35.148744Z","iopub.status.idle":"2022-07-15T15:28:35.176569Z","shell.execute_reply.started":"2022-07-15T15:28:35.148707Z","shell.execute_reply":"2022-07-15T15:28:35.175153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking for Parch's outliers\n\nsns.displot(df['Parch'],kde=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.178578Z","iopub.execute_input":"2022-07-15T15:28:35.179021Z","iopub.status.idle":"2022-07-15T15:28:35.623408Z","shell.execute_reply.started":"2022-07-15T15:28:35.178986Z","shell.execute_reply":"2022-07-15T15:28:35.621716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=df,x='Parch')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.625357Z","iopub.execute_input":"2022-07-15T15:28:35.625968Z","iopub.status.idle":"2022-07-15T15:28:35.846249Z","shell.execute_reply.started":"2022-07-15T15:28:35.625924Z","shell.execute_reply":"2022-07-15T15:28:35.845311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Parch'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.847971Z","iopub.execute_input":"2022-07-15T15:28:35.848668Z","iopub.status.idle":"2022-07-15T15:28:35.857696Z","shell.execute_reply.started":"2022-07-15T15:28:35.848625Z","shell.execute_reply":"2022-07-15T15:28:35.856807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"118+80+5+5+4+1","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:28:35.859142Z","iopub.execute_input":"2022-07-15T15:28:35.859770Z","iopub.status.idle":"2022-07-15T15:28:35.867031Z","shell.execute_reply.started":"2022-07-15T15:28:35.859737Z","shell.execute_reply":"2022-07-15T15:28:35.866275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# % instances with 0 Parents or Childs\nnp.round((df['Parch'].value_counts()[0]/len(df))*100,2)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.868369Z","iopub.execute_input":"2022-07-15T15:28:35.868886Z","iopub.status.idle":"2022-07-15T15:28:35.882192Z","shell.execute_reply.started":"2022-07-15T15:28:35.868856Z","shell.execute_reply":"2022-07-15T15:28:35.881426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###### Outliers details:\n\n##### Age-\n    - There are 13 instances above 63 years which is OK.\n    \n##### SibSp-\n    - There are 46 instances with more than 3 Siblings or Spouses which is OK.\n    \n##### Parch-\n    There are 213 instances with more than 1 Parents or Childrens as a outliers, which is OK. \n    \n##### Fare-\n    - There are 116 instances with fare more than 65 dollars which is OK.\n    \n    \n#### Conclustion - No significant problem with outliers in numerical data","metadata":{}},{"cell_type":"code","source":"df_cat.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.883981Z","iopub.execute_input":"2022-07-15T15:28:35.884329Z","iopub.status.idle":"2022-07-15T15:28:35.899337Z","shell.execute_reply.started":"2022-07-15T15:28:35.884298Z","shell.execute_reply":"2022-07-15T15:28:35.898118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets remove the 'Name' and 'Ticket' columns as they are not going to contibute in prediction\n\ndf_cat=df_cat.drop(['Name','Ticket'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.900911Z","iopub.execute_input":"2022-07-15T15:28:35.902055Z","iopub.status.idle":"2022-07-15T15:28:35.911099Z","shell.execute_reply.started":"2022-07-15T15:28:35.901993Z","shell.execute_reply":"2022-07-15T15:28:35.910240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.913186Z","iopub.execute_input":"2022-07-15T15:28:35.914226Z","iopub.status.idle":"2022-07-15T15:28:35.931038Z","shell.execute_reply.started":"2022-07-15T15:28:35.914124Z","shell.execute_reply":"2022-07-15T15:28:35.930260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat.isnull().sum()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:28:35.932044Z","iopub.execute_input":"2022-07-15T15:28:35.932908Z","iopub.status.idle":"2022-07-15T15:28:35.947689Z","shell.execute_reply.started":"2022-07-15T15:28:35.932876Z","shell.execute_reply":"2022-07-15T15:28:35.946540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.949324Z","iopub.execute_input":"2022-07-15T15:28:35.950025Z","iopub.status.idle":"2022-07-15T15:28:35.965526Z","shell.execute_reply.started":"2022-07-15T15:28:35.949991Z","shell.execute_reply":"2022-07-15T15:28:35.964107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets remove 'Survived' column as it is Target variable","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.966859Z","iopub.execute_input":"2022-07-15T15:28:35.967670Z","iopub.status.idle":"2022-07-15T15:28:35.976454Z","shell.execute_reply.started":"2022-07-15T15:28:35.967632Z","shell.execute_reply":"2022-07-15T15:28:35.975449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num=df_num.drop('Survived',axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.977851Z","iopub.execute_input":"2022-07-15T15:28:35.978241Z","iopub.status.idle":"2022-07-15T15:28:35.989396Z","shell.execute_reply.started":"2022-07-15T15:28:35.978183Z","shell.execute_reply":"2022-07-15T15:28:35.988161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:35.991329Z","iopub.execute_input":"2022-07-15T15:28:35.991674Z","iopub.status.idle":"2022-07-15T15:28:36.011756Z","shell.execute_reply.started":"2022-07-15T15:28:35.991642Z","shell.execute_reply":"2022-07-15T15:28:36.010766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_num.isnull().sum()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:28:36.012998Z","iopub.execute_input":"2022-07-15T15:28:36.014020Z","iopub.status.idle":"2022-07-15T15:28:36.029709Z","shell.execute_reply.started":"2022-07-15T15:28:36.013977Z","shell.execute_reply":"2022-07-15T15:28:36.028615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.031288Z","iopub.execute_input":"2022-07-15T15:28:36.032235Z","iopub.status.idle":"2022-07-15T15:28:36.054056Z","shell.execute_reply.started":"2022-07-15T15:28:36.032180Z","shell.execute_reply":"2022-07-15T15:28:36.053187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Onehot Encoding","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.055463Z","iopub.execute_input":"2022-07-15T15:28:36.056518Z","iopub.status.idle":"2022-07-15T15:28:36.061131Z","shell.execute_reply.started":"2022-07-15T15:28:36.056483Z","shell.execute_reply":"2022-07-15T15:28:36.060313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Categorical Dataset with dummies\ndf_cat_dum=pd.get_dummies(df_cat,drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.062344Z","iopub.execute_input":"2022-07-15T15:28:36.063175Z","iopub.status.idle":"2022-07-15T15:28:36.078745Z","shell.execute_reply.started":"2022-07-15T15:28:36.063142Z","shell.execute_reply":"2022-07-15T15:28:36.077813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat_dum.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.079966Z","iopub.execute_input":"2022-07-15T15:28:36.081407Z","iopub.status.idle":"2022-07-15T15:28:36.098625Z","shell.execute_reply.started":"2022-07-15T15:28:36.081312Z","shell.execute_reply":"2022-07-15T15:28:36.097728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets join this categorical dataframe with dummies with numerical data frame \n#to create final dataframe for our Machine Learning Model ","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.100091Z","iopub.execute_input":"2022-07-15T15:28:36.100461Z","iopub.status.idle":"2022-07-15T15:28:36.104973Z","shell.execute_reply.started":"2022-07-15T15:28:36.100430Z","shell.execute_reply":"2022-07-15T15:28:36.104146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_final=pd.concat([df_num,df_cat_dum],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.106046Z","iopub.execute_input":"2022-07-15T15:28:36.106864Z","iopub.status.idle":"2022-07-15T15:28:36.119114Z","shell.execute_reply.started":"2022-07-15T15:28:36.106833Z","shell.execute_reply":"2022-07-15T15:28:36.118082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_final.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.120422Z","iopub.execute_input":"2022-07-15T15:28:36.120886Z","iopub.status.idle":"2022-07-15T15:28:36.137745Z","shell.execute_reply.started":"2022-07-15T15:28:36.120857Z","shell.execute_reply":"2022-07-15T15:28:36.136646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(df),len(df_cat_dum),len(df_cat),len(df_num),len(df_final))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.139401Z","iopub.execute_input":"2022-07-15T15:28:36.139971Z","iopub.status.idle":"2022-07-15T15:28:36.149250Z","shell.execute_reply.started":"2022-07-15T15:28:36.139937Z","shell.execute_reply":"2022-07-15T15:28:36.148280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We are pretty much done with EDA and Feature Engineering, Now lets create Machine Learning Model.\n\n## Logistic Regression Model","metadata":{}},{"cell_type":"code","source":"#Separating Dependent and Independent variables\n\nX=df_final #Independent Variables\ny=df['Survived'] #Dependent Variable\n","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.150328Z","iopub.execute_input":"2022-07-15T15:28:36.151434Z","iopub.status.idle":"2022-07-15T15:28:36.160791Z","shell.execute_reply.started":"2022-07-15T15:28:36.151392Z","shell.execute_reply":"2022-07-15T15:28:36.159540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets Split training ,evaluation and testing data\n\nfrom sklearn.model_selection import train_test_split\n\n# 80% of data is training data, setting aside other 20%\nX_train, X_OTHER, y_train, y_OTHER = train_test_split(X, y, test_size=0.20, random_state=101)\n\n# Remaining 20% is split into evaluation and test sets\n# Each is 10% of the original data size\nX_eval, X_test, y_eval, y_test = train_test_split(X_OTHER, y_OTHER, test_size=0.5, random_state=101)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.162112Z","iopub.execute_input":"2022-07-15T15:28:36.162633Z","iopub.status.idle":"2022-07-15T15:28:36.229719Z","shell.execute_reply.started":"2022-07-15T15:28:36.162601Z","shell.execute_reply":"2022-07-15T15:28:36.228764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets scale data","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.231885Z","iopub.execute_input":"2022-07-15T15:28:36.232577Z","iopub.status.idle":"2022-07-15T15:28:36.236652Z","shell.execute_reply.started":"2022-07-15T15:28:36.232526Z","shell.execute_reply":"2022-07-15T15:28:36.235679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.237926Z","iopub.execute_input":"2022-07-15T15:28:36.238528Z","iopub.status.idle":"2022-07-15T15:28:36.250045Z","shell.execute_reply.started":"2022-07-15T15:28:36.238495Z","shell.execute_reply":"2022-07-15T15:28:36.248673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.264869Z","iopub.execute_input":"2022-07-15T15:28:36.265568Z","iopub.status.idle":"2022-07-15T15:28:36.269857Z","shell.execute_reply.started":"2022-07-15T15:28:36.265529Z","shell.execute_reply":"2022-07-15T15:28:36.268917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=scaler.fit_transform(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.271832Z","iopub.execute_input":"2022-07-15T15:28:36.273393Z","iopub.status.idle":"2022-07-15T15:28:36.286414Z","shell.execute_reply.started":"2022-07-15T15:28:36.273329Z","shell.execute_reply":"2022-07-15T15:28:36.285474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_eval=scaler.transform(X_eval)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.288496Z","iopub.execute_input":"2022-07-15T15:28:36.289717Z","iopub.status.idle":"2022-07-15T15:28:36.298070Z","shell.execute_reply.started":"2022-07-15T15:28:36.289671Z","shell.execute_reply":"2022-07-15T15:28:36.296951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.299429Z","iopub.execute_input":"2022-07-15T15:28:36.299869Z","iopub.status.idle":"2022-07-15T15:28:36.309762Z","shell.execute_reply.started":"2022-07-15T15:28:36.299834Z","shell.execute_reply":"2022-07-15T15:28:36.308671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets create base Model\n\nfrom sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.311385Z","iopub.execute_input":"2022-07-15T15:28:36.311893Z","iopub.status.idle":"2022-07-15T15:28:36.342665Z","shell.execute_reply.started":"2022-07-15T15:28:36.311854Z","shell.execute_reply":"2022-07-15T15:28:36.341742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_model=LogisticRegression(penalty='elasticnet',solver='saga',multi_class='ovr',max_iter=10000)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.343753Z","iopub.execute_input":"2022-07-15T15:28:36.344915Z","iopub.status.idle":"2022-07-15T15:28:36.351066Z","shell.execute_reply.started":"2022-07-15T15:28:36.344865Z","shell.execute_reply":"2022-07-15T15:28:36.350245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets find best parameters with GradSearch Cross Validation\nfrom sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.352758Z","iopub.execute_input":"2022-07-15T15:28:36.353105Z","iopub.status.idle":"2022-07-15T15:28:36.363594Z","shell.execute_reply.started":"2022-07-15T15:28:36.353076Z","shell.execute_reply":"2022-07-15T15:28:36.362371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid={'l1_ratio':np.linspace(0,1,20),'C':np.logspace(0,10,20)}","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.366508Z","iopub.execute_input":"2022-07-15T15:28:36.367364Z","iopub.status.idle":"2022-07-15T15:28:36.376947Z","shell.execute_reply.started":"2022-07-15T15:28:36.367319Z","shell.execute_reply":"2022-07-15T15:28:36.375718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model=GridSearchCV(estimator=log_model,param_grid=param_grid,cv=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.379003Z","iopub.execute_input":"2022-07-15T15:28:36.379573Z","iopub.status.idle":"2022-07-15T15:28:36.389386Z","shell.execute_reply.started":"2022-07-15T15:28:36.379526Z","shell.execute_reply":"2022-07-15T15:28:36.387641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:28:36.391353Z","iopub.execute_input":"2022-07-15T15:28:36.391820Z","iopub.status.idle":"2022-07-15T15:29:14.438579Z","shell.execute_reply.started":"2022-07-15T15:28:36.391775Z","shell.execute_reply":"2022-07-15T15:29:14.437386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.440623Z","iopub.execute_input":"2022-07-15T15:29:14.440965Z","iopub.status.idle":"2022-07-15T15:29:14.449956Z","shell.execute_reply.started":"2022-07-15T15:29:14.440937Z","shell.execute_reply":"2022-07-15T15:29:14.448531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.451702Z","iopub.execute_input":"2022-07-15T15:29:14.452118Z","iopub.status.idle":"2022-07-15T15:29:14.465987Z","shell.execute_reply.started":"2022-07-15T15:29:14.452085Z","shell.execute_reply":"2022-07-15T15:29:14.464037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model.predict_proba(X_eval)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-15T15:29:14.467733Z","iopub.execute_input":"2022-07-15T15:29:14.468657Z","iopub.status.idle":"2022-07-15T15:29:14.481322Z","shell.execute_reply.started":"2022-07-15T15:29:14.468602Z","shell.execute_reply":"2022-07-15T15:29:14.480226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=grid_model.predict(X_eval)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.482462Z","iopub.execute_input":"2022-07-15T15:29:14.482869Z","iopub.status.idle":"2022-07-15T15:29:14.494539Z","shell.execute_reply.started":"2022-07-15T15:29:14.482836Z","shell.execute_reply":"2022-07-15T15:29:14.493377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Performance Metrics\n\nfrom sklearn.metrics import confusion_matrix,accuracy_score,recall_score,precision_score,classification_report","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.495926Z","iopub.execute_input":"2022-07-15T15:29:14.496341Z","iopub.status.idle":"2022-07-15T15:29:14.507146Z","shell.execute_reply.started":"2022-07-15T15:29:14.496299Z","shell.execute_reply":"2022-07-15T15:29:14.506114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_eval,y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.508249Z","iopub.execute_input":"2022-07-15T15:29:14.508601Z","iopub.status.idle":"2022-07-15T15:29:14.524616Z","shell.execute_reply.started":"2022-07-15T15:29:14.508568Z","shell.execute_reply":"2022-07-15T15:29:14.523274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"precision_score(y_eval,y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.526060Z","iopub.execute_input":"2022-07-15T15:29:14.526443Z","iopub.status.idle":"2022-07-15T15:29:14.540120Z","shell.execute_reply.started":"2022-07-15T15:29:14.526411Z","shell.execute_reply":"2022-07-15T15:29:14.538922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"recall_score(y_eval,y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.541926Z","iopub.execute_input":"2022-07-15T15:29:14.542354Z","iopub.status.idle":"2022-07-15T15:29:14.557496Z","shell.execute_reply.started":"2022-07-15T15:29:14.542320Z","shell.execute_reply":"2022-07-15T15:29:14.556528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_eval,y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.559159Z","iopub.execute_input":"2022-07-15T15:29:14.560356Z","iopub.status.idle":"2022-07-15T15:29:14.572873Z","shell.execute_reply.started":"2022-07-15T15:29:14.560321Z","shell.execute_reply":"2022-07-15T15:29:14.571683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_eval,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.574456Z","iopub.execute_input":"2022-07-15T15:29:14.575060Z","iopub.status.idle":"2022-07-15T15:29:14.586664Z","shell.execute_reply.started":"2022-07-15T15:29:14.575025Z","shell.execute_reply":"2022-07-15T15:29:14.585438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import plot_confusion_matrix,plot_precision_recall_curve,plot_roc_curve","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.588441Z","iopub.execute_input":"2022-07-15T15:29:14.589116Z","iopub.status.idle":"2022-07-15T15:29:14.596485Z","shell.execute_reply.started":"2022-07-15T15:29:14.589079Z","shell.execute_reply":"2022-07-15T15:29:14.595195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(grid_model,X_eval,y_eval)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.597956Z","iopub.execute_input":"2022-07-15T15:29:14.598645Z","iopub.status.idle":"2022-07-15T15:29:14.800162Z","shell.execute_reply.started":"2022-07-15T15:29:14.598532Z","shell.execute_reply":"2022-07-15T15:29:14.799338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_precision_recall_curve(grid_model,X_eval,y_eval)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:14.801749Z","iopub.execute_input":"2022-07-15T15:29:14.802669Z","iopub.status.idle":"2022-07-15T15:29:15.074546Z","shell.execute_reply.started":"2022-07-15T15:29:14.802605Z","shell.execute_reply":"2022-07-15T15:29:15.073375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc_curve(grid_model,X_eval,y_eval)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.075736Z","iopub.execute_input":"2022-07-15T15:29:15.076838Z","iopub.status.idle":"2022-07-15T15:29:15.328407Z","shell.execute_reply.started":"2022-07-15T15:29:15.076801Z","shell.execute_reply":"2022-07-15T15:29:15.327465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_final=grid_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.330121Z","iopub.execute_input":"2022-07-15T15:29:15.330535Z","iopub.status.idle":"2022-07-15T15:29:15.335851Z","shell.execute_reply.started":"2022-07-15T15:29:15.330501Z","shell.execute_reply":"2022-07-15T15:29:15.334726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_model.predict_proba(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.337364Z","iopub.execute_input":"2022-07-15T15:29:15.338301Z","iopub.status.idle":"2022-07-15T15:29:15.353138Z","shell.execute_reply.started":"2022-07-15T15:29:15.338257Z","shell.execute_reply":"2022-07-15T15:29:15.351893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred_final)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.354469Z","iopub.execute_input":"2022-07-15T15:29:15.355910Z","iopub.status.idle":"2022-07-15T15:29:15.368812Z","shell.execute_reply.started":"2022-07-15T15:29:15.355852Z","shell.execute_reply":"2022-07-15T15:29:15.367586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"precision_score(y_test,y_pred_final)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.371124Z","iopub.execute_input":"2022-07-15T15:29:15.372053Z","iopub.status.idle":"2022-07-15T15:29:15.382897Z","shell.execute_reply.started":"2022-07-15T15:29:15.372002Z","shell.execute_reply":"2022-07-15T15:29:15.381941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"recall_score(y_test,y_pred_final)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.384939Z","iopub.execute_input":"2022-07-15T15:29:15.385698Z","iopub.status.idle":"2022-07-15T15:29:15.395662Z","shell.execute_reply.started":"2022-07-15T15:29:15.385660Z","shell.execute_reply":"2022-07-15T15:29:15.393557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,y_pred_final))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.397402Z","iopub.execute_input":"2022-07-15T15:29:15.397912Z","iopub.status.idle":"2022-07-15T15:29:15.410240Z","shell.execute_reply.started":"2022-07-15T15:29:15.397865Z","shell.execute_reply":"2022-07-15T15:29:15.408920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_test,y_pred_final)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.411604Z","iopub.execute_input":"2022-07-15T15:29:15.413136Z","iopub.status.idle":"2022-07-15T15:29:15.426011Z","shell.execute_reply.started":"2022-07-15T15:29:15.413094Z","shell.execute_reply":"2022-07-15T15:29:15.424614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(grid_model,X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.427882Z","iopub.execute_input":"2022-07-15T15:29:15.428813Z","iopub.status.idle":"2022-07-15T15:29:15.679411Z","shell.execute_reply.started":"2022-07-15T15:29:15.428763Z","shell.execute_reply":"2022-07-15T15:29:15.677891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_precision_recall_curve(grid_model,X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.681597Z","iopub.execute_input":"2022-07-15T15:29:15.683681Z","iopub.status.idle":"2022-07-15T15:29:15.969299Z","shell.execute_reply.started":"2022-07-15T15:29:15.683609Z","shell.execute_reply":"2022-07-15T15:29:15.967520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc_curve(grid_model,X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T15:29:15.971255Z","iopub.execute_input":"2022-07-15T15:29:15.971676Z","iopub.status.idle":"2022-07-15T15:29:16.229296Z","shell.execute_reply.started":"2022-07-15T15:29:15.971643Z","shell.execute_reply":"2022-07-15T15:29:16.227723Z"},"trusted":true},"execution_count":null,"outputs":[]}]}