{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"***Introduction***\n\nIn this notebook, I'll go through a typical workflow for solving DS competition.\nThe goal is to correctly predict if someone survived the Titanic shipwreck.\nIn other words, we want to train a ML model to learn the realtionship between passenger's features and their survival outcome and thus make survival predictions of passengers for the data that our model hasn't seen before.\n\nI truly appreciate the work of the following Kaggle users which helped me making this notebook:\n\n[https://www.kaggle.com/code/jasonchong914/titanic-survival-prediction-competition](http://) by [https://www.kaggle.com/jasonchong914]\n\n[https://www.kaggle.com/code/kenjee/titanic-project-example](http://) by [https://www.kaggle.com/kenjee](http://)\n\n[https://www.kaggle.com/code/startupsci/titanic-data-science-solutions](http://) by [https://www.kaggle.com/startupsci](http://)\n\n\nThere are 3 datasets in this competition:\n1. Training set\n2. Test set\n3. Sample submission\n\n","metadata":{}},{"cell_type":"markdown","source":"***Importing libraries***","metadata":{}},{"cell_type":"code","source":"#Data wrangling\nimport pandas as pd\nimport numpy as np\nimport missingno\nfrom collections import Counter\n\n#Data visualization\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\n#Machine learning models\nfrom sklearn.linear_model import LogisticRegression, Perceptron, SGDClassifier\nfrom sklearn.svm import SVC, LinearSVC\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.tree import DecisionTreeClassifier\nfrom catboost import CatBoostClassifier\n\n#Model evaluation\nfrom sklearn.model_selection import cross_val_score\n\n#Hyperparameter tuning\nfrom sklearn.model_selection import GridSearchCV\n\n#Warnings\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.242580Z","iopub.execute_input":"2022-07-18T11:34:35.242932Z","iopub.status.idle":"2022-07-18T11:34:35.257255Z","shell.execute_reply.started":"2022-07-18T11:34:35.242904Z","shell.execute_reply":"2022-07-18T11:34:35.256044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Importing datasets***","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/titanic/train.csv')\ntest=pd.read_csv('/kaggle/input/titanic/test.csv')\nss=pd.read_csv('/kaggle/input/titanic/gender_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.259531Z","iopub.execute_input":"2022-07-18T11:34:35.260257Z","iopub.status.idle":"2022-07-18T11:34:35.288797Z","shell.execute_reply.started":"2022-07-18T11:34:35.260207Z","shell.execute_reply":"2022-07-18T11:34:35.287939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Undesrstanding the shape of the data***","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.290600Z","iopub.execute_input":"2022-07-18T11:34:35.291534Z","iopub.status.idle":"2022-07-18T11:34:35.310719Z","shell.execute_reply.started":"2022-07-18T11:34:35.291489Z","shell.execute_reply":"2022-07-18T11:34:35.309567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.312237Z","iopub.execute_input":"2022-07-18T11:34:35.312997Z","iopub.status.idle":"2022-07-18T11:34:35.332141Z","shell.execute_reply.started":"2022-07-18T11:34:35.312959Z","shell.execute_reply":"2022-07-18T11:34:35.331240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.334313Z","iopub.execute_input":"2022-07-18T11:34:35.335346Z","iopub.status.idle":"2022-07-18T11:34:35.345907Z","shell.execute_reply.started":"2022-07-18T11:34:35.335290Z","shell.execute_reply":"2022-07-18T11:34:35.344945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.347537Z","iopub.execute_input":"2022-07-18T11:34:35.348125Z","iopub.status.idle":"2022-07-18T11:34:35.358122Z","shell.execute_reply.started":"2022-07-18T11:34:35.348080Z","shell.execute_reply":"2022-07-18T11:34:35.357222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.359521Z","iopub.execute_input":"2022-07-18T11:34:35.360060Z","iopub.status.idle":"2022-07-18T11:34:35.369884Z","shell.execute_reply.started":"2022-07-18T11:34:35.360028Z","shell.execute_reply":"2022-07-18T11:34:35.369000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The only difference between the shape of train and test data sets is the Survived column in the train dataset.","metadata":{}},{"cell_type":"code","source":"train.info()\ntest.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.371503Z","iopub.execute_input":"2022-07-18T11:34:35.372157Z","iopub.status.idle":"2022-07-18T11:34:35.398299Z","shell.execute_reply.started":"2022-07-18T11:34:35.372114Z","shell.execute_reply":"2022-07-18T11:34:35.397410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Data description***\n\nSurvived: 0 - Did not surivved ; 1 - Survived\nPclass: 1 - First class ; 2 - Second class ; 3 - Third Class\nSex: Male or Female\nAge: In years\nSibSp: Number of siblings or spouses on Titanic\nParch: Number of parents or children on Titanic\nTicket: Passenger's ticket number\nFare: Passenger's fare\nCabin: Cabin number\nEmbarked: Point of embarkation C = Cherbourg ; Q = Queenstown ; S = Southampton","metadata":{}},{"cell_type":"markdown","source":"***EDA***","metadata":{}},{"cell_type":"code","source":"train.isnull().sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.399816Z","iopub.execute_input":"2022-07-18T11:34:35.400717Z","iopub.status.idle":"2022-07-18T11:34:35.412056Z","shell.execute_reply.started":"2022-07-18T11:34:35.400682Z","shell.execute_reply":"2022-07-18T11:34:35.410802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.495362Z","iopub.execute_input":"2022-07-18T11:34:35.496607Z","iopub.status.idle":"2022-07-18T11:34:35.508697Z","shell.execute_reply.started":"2022-07-18T11:34:35.496453Z","shell.execute_reply":"2022-07-18T11:34:35.507302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cabin, Age and Embarked have missing values in train dataset\nCabin, Age and Fare have missing values in test dataset","metadata":{}},{"cell_type":"code","source":"import missingno\nfrom collections import Counter\nmissingno.matrix(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.511111Z","iopub.execute_input":"2022-07-18T11:34:35.512230Z","iopub.status.idle":"2022-07-18T11:34:35.948713Z","shell.execute_reply.started":"2022-07-18T11:34:35.512164Z","shell.execute_reply":"2022-07-18T11:34:35.947408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.950871Z","iopub.execute_input":"2022-07-18T11:34:35.951953Z","iopub.status.idle":"2022-07-18T11:34:35.992010Z","shell.execute_reply.started":"2022-07-18T11:34:35.951902Z","shell.execute_reply":"2022-07-18T11:34:35.990664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:35.993320Z","iopub.execute_input":"2022-07-18T11:34:35.993649Z","iopub.status.idle":"2022-07-18T11:34:36.028218Z","shell.execute_reply.started":"2022-07-18T11:34:35.993620Z","shell.execute_reply":"2022-07-18T11:34:36.027063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Feature analysis***\n\nKnowing which feature is numerical and which categorical helps us structure analysis more properly. \n\nCategorical - Sex, Pclass and Embarked\n\nNumerical - SibSp, Parch, Age and Fare","metadata":{}},{"cell_type":"code","source":"train['Sex'].value_counts(dropna=False)\n#There were more male passengers than female","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:36.031949Z","iopub.execute_input":"2022-07-18T11:34:36.033152Z","iopub.status.idle":"2022-07-18T11:34:36.042819Z","shell.execute_reply.started":"2022-07-18T11:34:36.033107Z","shell.execute_reply":"2022-07-18T11:34:36.041655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[['Sex','Survived']].groupby('Sex', as_index=False).mean().sort_values(by='Survived', ascending=False)\n#Females had more probability than male to survive","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:36.044406Z","iopub.execute_input":"2022-07-18T11:34:36.045614Z","iopub.status.idle":"2022-07-18T11:34:36.065503Z","shell.execute_reply.started":"2022-07-18T11:34:36.045578Z","shell.execute_reply":"2022-07-18T11:34:36.064139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Survival probability barplot by gender\nsns.barplot(x='Sex', y='Survived', data=train)\nplt.ylabel('Survival Probability')\nplt.title('Survival probability by gender')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:36.067538Z","iopub.execute_input":"2022-07-18T11:34:36.068681Z","iopub.status.idle":"2022-07-18T11:34:36.324400Z","shell.execute_reply.started":"2022-07-18T11:34:36.068633Z","shell.execute_reply":"2022-07-18T11:34:36.323183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Pclass'].value_counts(dropna=False)\n#Most of passengers were in third class","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:36.325811Z","iopub.execute_input":"2022-07-18T11:34:36.326151Z","iopub.status.idle":"2022-07-18T11:34:36.335166Z","shell.execute_reply.started":"2022-07-18T11:34:36.326120Z","shell.execute_reply":"2022-07-18T11:34:36.333978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mean of survival rate by passenger class\ntrain[['Pclass','Survived']].groupby('Pclass', as_index=False).mean().sort_values(by='Survived', ascending=False)\n#The better class the better survival probability","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:36.336689Z","iopub.execute_input":"2022-07-18T11:34:36.337013Z","iopub.status.idle":"2022-07-18T11:34:36.356566Z","shell.execute_reply.started":"2022-07-18T11:34:36.336984Z","shell.execute_reply":"2022-07-18T11:34:36.355093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Survival rate by passenger class barplot\nsns.barplot(x='Pclass', y='Survived', data=train)\nplt.ylabel('Survival probability')\nplt.title('Survival mean by passenger class')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:36.358380Z","iopub.execute_input":"2022-07-18T11:34:36.359223Z","iopub.status.idle":"2022-07-18T11:34:36.656632Z","shell.execute_reply.started":"2022-07-18T11:34:36.359186Z","shell.execute_reply":"2022-07-18T11:34:36.655413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Survival probability by sex and passenger class\nt=sns.factorplot(x='Pclass', y='Survived', hue='Sex', data=train, kind='bar')\nt.despine(left=True)\nplt.ylabel(\"Survival probability\")\nplt.title(\"Survival probability by sex and passenger class\")","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:36.658162Z","iopub.execute_input":"2022-07-18T11:34:36.658651Z","iopub.status.idle":"2022-07-18T11:34:37.212204Z","shell.execute_reply.started":"2022-07-18T11:34:36.658605Z","shell.execute_reply":"2022-07-18T11:34:37.210828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Embarked'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:37.213963Z","iopub.execute_input":"2022-07-18T11:34:37.214511Z","iopub.status.idle":"2022-07-18T11:34:37.226512Z","shell.execute_reply.started":"2022-07-18T11:34:37.214475Z","shell.execute_reply":"2022-07-18T11:34:37.224730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mean of survival by point of embarkation\ntrain[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:37.228338Z","iopub.execute_input":"2022-07-18T11:34:37.228956Z","iopub.status.idle":"2022-07-18T11:34:37.246627Z","shell.execute_reply.started":"2022-07-18T11:34:37.228921Z","shell.execute_reply":"2022-07-18T11:34:37.245277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x='Embarked', y='Survived', data=train)\nplt.ylabel('Survival probability')\nplt.title('Survival probability by point of embarktion')\n#Survival probabilty is the highest for Cherbourg","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:37.253661Z","iopub.execute_input":"2022-07-18T11:34:37.254044Z","iopub.status.idle":"2022-07-18T11:34:37.558396Z","shell.execute_reply.started":"2022-07-18T11:34:37.254012Z","shell.execute_reply":"2022-07-18T11:34:37.556868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's make comparison between embarkation point and class because maybe the majority of Cherbourg's passengers were in first class. (it doesn't seem logical that passengers from any point were priortised during the evacuation)\n","metadata":{}},{"cell_type":"code","source":"sns.factorplot('Pclass', col='Embarked', data=train, kind='count')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:37.559700Z","iopub.execute_input":"2022-07-18T11:34:37.560018Z","iopub.status.idle":"2022-07-18T11:34:38.071846Z","shell.execute_reply.started":"2022-07-18T11:34:37.559990Z","shell.execute_reply":"2022-07-18T11:34:38.070271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most of Cherbourg's passengers were in the first class. On the other hand most of passengers from Southtampton were in third class. This explains that class matters not point of embarktion when it comes to the mean of survival.","metadata":{}},{"cell_type":"code","source":"#Survival rate by all categorical variables\n\ngrid = sns.FacetGrid(train, row = 'Embarked', size = 2.2, aspect = 1.6)\ngrid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette = 'deep')\ngrid.add_legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:38.073456Z","iopub.execute_input":"2022-07-18T11:34:38.073791Z","iopub.status.idle":"2022-07-18T11:34:39.307875Z","shell.execute_reply.started":"2022-07-18T11:34:38.073762Z","shell.execute_reply":"2022-07-18T11:34:39.306474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Outliers detecting and removing***\nTurey's rule\nOutliers are the values more than 1.5 times the interquartiel range from the quartiles - either Q1-1.5IQR or above Q3+1.5IQR.\nWe will use these as part of writing a function to indentify outliers according to Tukey's rule.","metadata":{}},{"cell_type":"code","source":" def detect_outliers(df,n,features):\n        outlier_indices=[]\n        for col in features:\n            Q1=np.percentile(df[col], 25)\n            Q3=np.percentile(df[col], 75)\n            IQR=Q3-Q1\n            outlier_step=1.5*IQR\n            outlier_list_col=df[(df[col]<Q1-outlier_step) | (df[col]>Q3+outlier_step)].index\n            outlier_indices.extend(outlier_list_col)\n        outlier_indices=Counter(outlier_indices)\n        multiple_outliers=list(key for key, value in outlier_indices.items() if value>n)\n        return multiple_outliers\noutliers_to_drop=detect_outliers(train, 2, ['Age', 'SibSp', 'Parch', 'Fare'])\nprint(\"We will drop these {} indices: \".format(len(outliers_to_drop)), outliers_to_drop)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:39.309535Z","iopub.execute_input":"2022-07-18T11:34:39.309954Z","iopub.status.idle":"2022-07-18T11:34:39.327096Z","shell.execute_reply.started":"2022-07-18T11:34:39.309910Z","shell.execute_reply":"2022-07-18T11:34:39.324739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.loc[outliers_to_drop, :]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:39.328381Z","iopub.execute_input":"2022-07-18T11:34:39.328746Z","iopub.status.idle":"2022-07-18T11:34:39.350557Z","shell.execute_reply.started":"2022-07-18T11:34:39.328708Z","shell.execute_reply":"2022-07-18T11:34:39.349187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#drop outliers and reset index\nprint(\"Before: {} rows\".format(len(train)))\ntrain=train.drop(outliers_to_drop, axis=0).reset_index(drop=True)\nprint(\"After: {} rows\".format(len(train)))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:39.353067Z","iopub.execute_input":"2022-07-18T11:34:39.354193Z","iopub.status.idle":"2022-07-18T11:34:39.362131Z","shell.execute_reply.started":"2022-07-18T11:34:39.354140Z","shell.execute_reply":"2022-07-18T11:34:39.361336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Numerical variables correlation with survival\nsns.heatmap(train[['Survived', 'SibSp', 'Parch', 'Age', 'Fare']].corr(), annot=True, fmt='.2f', cmap='coolwarm')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:39.363263Z","iopub.execute_input":"2022-07-18T11:34:39.364307Z","iopub.status.idle":"2022-07-18T11:34:39.671731Z","shell.execute_reply.started":"2022-07-18T11:34:39.364272Z","shell.execute_reply":"2022-07-18T11:34:39.670463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['SibSp'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:39.673043Z","iopub.execute_input":"2022-07-18T11:34:39.673371Z","iopub.status.idle":"2022-07-18T11:34:39.683258Z","shell.execute_reply.started":"2022-07-18T11:34:39.673341Z","shell.execute_reply":"2022-07-18T11:34:39.682030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Survival mean by SibSp\ntrain[['SibSp', 'Survived']].groupby('SibSp', as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:39.685185Z","iopub.execute_input":"2022-07-18T11:34:39.685652Z","iopub.status.idle":"2022-07-18T11:34:39.706010Z","shell.execute_reply.started":"2022-07-18T11:34:39.685607Z","shell.execute_reply":"2022-07-18T11:34:39.705107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x='SibSp', y='Survived', data=train)\nplt.ylabel('Survival probability')\nplt.title(\"Survival mean by SibSp\")","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:39.706995Z","iopub.execute_input":"2022-07-18T11:34:39.707311Z","iopub.status.idle":"2022-07-18T11:34:40.107038Z","shell.execute_reply.started":"2022-07-18T11:34:39.707281Z","shell.execute_reply":"2022-07-18T11:34:40.105877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Parch'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:40.108472Z","iopub.execute_input":"2022-07-18T11:34:40.108806Z","iopub.status.idle":"2022-07-18T11:34:40.117844Z","shell.execute_reply.started":"2022-07-18T11:34:40.108774Z","shell.execute_reply":"2022-07-18T11:34:40.116573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mean of survival by Parch\ntrain[['Parch', 'Survived']].groupby('Parch', as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:40.119451Z","iopub.execute_input":"2022-07-18T11:34:40.119991Z","iopub.status.idle":"2022-07-18T11:34:40.137124Z","shell.execute_reply.started":"2022-07-18T11:34:40.119955Z","shell.execute_reply":"2022-07-18T11:34:40.136229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x='Parch', y='Survived', data=train)\nplt.ylabel('Survival probability')\nplt.title('Survival rate by Parch')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:40.138290Z","iopub.execute_input":"2022-07-18T11:34:40.139240Z","iopub.status.idle":"2022-07-18T11:34:40.536742Z","shell.execute_reply.started":"2022-07-18T11:34:40.139202Z","shell.execute_reply":"2022-07-18T11:34:40.535871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Null values in Age column\ntrain['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:40.537762Z","iopub.execute_input":"2022-07-18T11:34:40.538676Z","iopub.status.idle":"2022-07-18T11:34:40.545970Z","shell.execute_reply.started":"2022-07-18T11:34:40.538638Z","shell.execute_reply":"2022-07-18T11:34:40.544865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Passenger age distribution\nsns.distplot(train['Age'], label='Skewness: %.2f'%(train['Age'].skew()))\nplt.legend(loc='best')\nplt.title('Passenger age distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:40.547842Z","iopub.execute_input":"2022-07-18T11:34:40.548394Z","iopub.status.idle":"2022-07-18T11:34:40.811938Z","shell.execute_reply.started":"2022-07-18T11:34:40.548356Z","shell.execute_reply":"2022-07-18T11:34:40.811176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Age distribution by survival\n\nt = sns.FacetGrid(train, col = 'Survived')\nt.map(sns.distplot, 'Age')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:40.812939Z","iopub.execute_input":"2022-07-18T11:34:40.813959Z","iopub.status.idle":"2022-07-18T11:34:41.265061Z","shell.execute_reply.started":"2022-07-18T11:34:40.813924Z","shell.execute_reply":"2022-07-18T11:34:41.263870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.kdeplot(train['Age'][train['Survived']==0], label='Did not survive')\nsns.kdeplot(train['Age'][train['Survived']==1], label='Survived')\nplt.xlabel('Age')\nplt.title('Age distribution by Survival outcome')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.268144Z","iopub.execute_input":"2022-07-18T11:34:41.268514Z","iopub.status.idle":"2022-07-18T11:34:41.483764Z","shell.execute_reply.started":"2022-07-18T11:34:41.268481Z","shell.execute_reply":"2022-07-18T11:34:41.482534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Fare'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.485500Z","iopub.execute_input":"2022-07-18T11:34:41.485808Z","iopub.status.idle":"2022-07-18T11:34:41.493055Z","shell.execute_reply.started":"2022-07-18T11:34:41.485779Z","shell.execute_reply":"2022-07-18T11:34:41.491931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Passenger fare distribution\nsns.distplot(train['Fare'], label='Skewness: %.2f'%(train['Fare'].skew()))\nplt.legend(loc='best')\nplt.ylabel('Passenger fare distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.494767Z","iopub.execute_input":"2022-07-18T11:34:41.495424Z","iopub.status.idle":"2022-07-18T11:34:41.825725Z","shell.execute_reply.started":"2022-07-18T11:34:41.495377Z","shell.execute_reply":"2022-07-18T11:34:41.824858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Data preprocessing***\n\nDropping and filling missing values","metadata":{}},{"cell_type":"code","source":"#Drop ticket and cabin features from training and test set for simplicity\ntrain=train.drop(['Ticket', 'Cabin'], axis=1)\ntest=test.drop(['Ticket', 'Cabin'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.827073Z","iopub.execute_input":"2022-07-18T11:34:41.827425Z","iopub.status.idle":"2022-07-18T11:34:41.834599Z","shell.execute_reply.started":"2022-07-18T11:34:41.827392Z","shell.execute_reply":"2022-07-18T11:34:41.833303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.836177Z","iopub.execute_input":"2022-07-18T11:34:41.836536Z","iopub.status.idle":"2022-07-18T11:34:41.855166Z","shell.execute_reply.started":"2022-07-18T11:34:41.836505Z","shell.execute_reply":"2022-07-18T11:34:41.853987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compute the most frequent value of embarked in training set\nmode=train['Embarked'].dropna().mode()[0]\nmode","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.856875Z","iopub.execute_input":"2022-07-18T11:34:41.857568Z","iopub.status.idle":"2022-07-18T11:34:41.869940Z","shell.execute_reply.started":"2022-07-18T11:34:41.857531Z","shell.execute_reply":"2022-07-18T11:34:41.868991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fill missing value in embarked with mode\ntrain['Embarked'].fillna(mode,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.871610Z","iopub.execute_input":"2022-07-18T11:34:41.872346Z","iopub.status.idle":"2022-07-18T11:34:41.878879Z","shell.execute_reply.started":"2022-07-18T11:34:41.872285Z","shell.execute_reply":"2022-07-18T11:34:41.877735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.880621Z","iopub.execute_input":"2022-07-18T11:34:41.881768Z","iopub.status.idle":"2022-07-18T11:34:41.897763Z","shell.execute_reply.started":"2022-07-18T11:34:41.881721Z","shell.execute_reply":"2022-07-18T11:34:41.896506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"median=test['Fare'].dropna().median()\nmedian","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.899529Z","iopub.execute_input":"2022-07-18T11:34:41.900300Z","iopub.status.idle":"2022-07-18T11:34:41.909085Z","shell.execute_reply.started":"2022-07-18T11:34:41.900252Z","shell.execute_reply":"2022-07-18T11:34:41.908216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Fare'].fillna(median, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.910235Z","iopub.execute_input":"2022-07-18T11:34:41.910984Z","iopub.status.idle":"2022-07-18T11:34:41.921531Z","shell.execute_reply.started":"2022-07-18T11:34:41.910952Z","shell.execute_reply":"2022-07-18T11:34:41.920497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine=pd.concat([train,test], axis=0).reset_index(drop=True)\ncombine.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.922779Z","iopub.execute_input":"2022-07-18T11:34:41.923247Z","iopub.status.idle":"2022-07-18T11:34:41.951191Z","shell.execute_reply.started":"2022-07-18T11:34:41.923218Z","shell.execute_reply":"2022-07-18T11:34:41.949929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Missing values in combined dataset\ncombine.isnull().sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.963097Z","iopub.execute_input":"2022-07-18T11:34:41.963496Z","iopub.status.idle":"2022-07-18T11:34:41.974792Z","shell.execute_reply.started":"2022-07-18T11:34:41.963465Z","shell.execute_reply":"2022-07-18T11:34:41.973303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting sex into numerical values ; 0=male 1=female\ncombine['Sex']=combine['Sex'].map({'male':0,'female':1})","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.976535Z","iopub.execute_input":"2022-07-18T11:34:41.977398Z","iopub.status.idle":"2022-07-18T11:34:41.984893Z","shell.execute_reply.started":"2022-07-18T11:34:41.977360Z","shell.execute_reply":"2022-07-18T11:34:41.983554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.factorplot(y='Age', x='Sex', hue='Pclass', kind='box', data=combine)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:41.986076Z","iopub.execute_input":"2022-07-18T11:34:41.987074Z","iopub.status.idle":"2022-07-18T11:34:42.401299Z","shell.execute_reply.started":"2022-07-18T11:34:41.987034Z","shell.execute_reply":"2022-07-18T11:34:42.400128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.factorplot(y='Age', x='Parch', kind='box', data=combine)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:42.403281Z","iopub.execute_input":"2022-07-18T11:34:42.403779Z","iopub.status.idle":"2022-07-18T11:34:42.760412Z","shell.execute_reply.started":"2022-07-18T11:34:42.403734Z","shell.execute_reply":"2022-07-18T11:34:42.759149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.factorplot(y='Age', x='SibSp', kind='box', data=combine)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:42.761854Z","iopub.execute_input":"2022-07-18T11:34:42.762183Z","iopub.status.idle":"2022-07-18T11:34:43.109018Z","shell.execute_reply.started":"2022-07-18T11:34:42.762153Z","shell.execute_reply":"2022-07-18T11:34:43.107719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(combine.drop(['Survived', 'Name', 'PassengerId', 'Fare'], axis=1).corr(),annot=True, cmap='coolwarm')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:43.110481Z","iopub.execute_input":"2022-07-18T11:34:43.111470Z","iopub.status.idle":"2022-07-18T11:34:43.421590Z","shell.execute_reply.started":"2022-07-18T11:34:43.111419Z","shell.execute_reply":"2022-07-18T11:34:43.420386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Age is not correlated with sex but slightly negatively correlated to SibSp, Parch and Pclass","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:43.422927Z","iopub.execute_input":"2022-07-18T11:34:43.423235Z","iopub.status.idle":"2022-07-18T11:34:43.428985Z","shell.execute_reply.started":"2022-07-18T11:34:43.423207Z","shell.execute_reply":"2022-07-18T11:34:43.427739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"age_nan=list(combine[combine['Age'].isnull()].index)\nlen(age_nan)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:43.430662Z","iopub.execute_input":"2022-07-18T11:34:43.431055Z","iopub.status.idle":"2022-07-18T11:34:43.443831Z","shell.execute_reply.started":"2022-07-18T11:34:43.431020Z","shell.execute_reply":"2022-07-18T11:34:43.442606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loop through list and impute missing ages\nfor index in age_nan:\n    median_age=combine['Age'].median()\n    predict_age=combine['Age'][(combine['SibSp']==combine.iloc[index]['SibSp'])\n        &(combine['Parch']==combine.iloc[index]['Parch'])\n        &(combine['Pclass']==combine.iloc[index]['Pclass'])].median()\n    \n    if np.isnan(predict_age):\n        combine['Age'].iloc[index]=median_age\n    else:\n        combine['Age'].iloc[index]=predict_age","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:43.445918Z","iopub.execute_input":"2022-07-18T11:34:43.446370Z","iopub.status.idle":"2022-07-18T11:34:43.990320Z","shell.execute_reply.started":"2022-07-18T11:34:43.446307Z","shell.execute_reply":"2022-07-18T11:34:43.989112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine['Age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:43.991860Z","iopub.execute_input":"2022-07-18T11:34:43.992208Z","iopub.status.idle":"2022-07-18T11:34:44.000403Z","shell.execute_reply.started":"2022-07-18T11:34:43.992176Z","shell.execute_reply":"2022-07-18T11:34:43.999194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Transformation","metadata":{}},{"cell_type":"code","source":"#Passenger fare distribution\nsns.distplot(combine['Fare'],label='Skewness: %.2f'%(combine['Fare'].skew()))\nplt.legend(loc='best')\nplt.title('Passenger Fare distribution')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.001987Z","iopub.execute_input":"2022-07-18T11:34:44.002703Z","iopub.status.idle":"2022-07-18T11:34:44.345257Z","shell.execute_reply.started":"2022-07-18T11:34:44.002664Z","shell.execute_reply":"2022-07-18T11:34:44.343786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Apply log transformation to fare column to reduce skewness\ncombine['Fare']=combine['Fare'].map(lambda x: np.log(x) if x>0 else 0)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.346916Z","iopub.execute_input":"2022-07-18T11:34:44.347401Z","iopub.status.idle":"2022-07-18T11:34:44.357603Z","shell.execute_reply.started":"2022-07-18T11:34:44.347352Z","shell.execute_reply":"2022-07-18T11:34:44.356545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(combine['Fare'], label='Skewness: %.2f'%(combine['Fare'].skew()))\nplt.legend(loc='best')\nplt.title(\"Passenger fare distribution after log transformation\")","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.358972Z","iopub.execute_input":"2022-07-18T11:34:44.359717Z","iopub.status.idle":"2022-07-18T11:34:44.661976Z","shell.execute_reply.started":"2022-07-18T11:34:44.359679Z","shell.execute_reply":"2022-07-18T11:34:44.660680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Feature Engineering**","metadata":{}},{"cell_type":"code","source":"combine['Title']=[name.split(',')[1].split('.')[0].strip() for name in combine['Name']]\ncombine[['Name','Title']].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.663387Z","iopub.execute_input":"2022-07-18T11:34:44.663710Z","iopub.status.idle":"2022-07-18T11:34:44.682303Z","shell.execute_reply.started":"2022-07-18T11:34:44.663681Z","shell.execute_reply":"2022-07-18T11:34:44.681002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"combine['Title'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.684719Z","iopub.execute_input":"2022-07-18T11:34:44.685622Z","iopub.status.idle":"2022-07-18T11:34:44.698465Z","shell.execute_reply.started":"2022-07-18T11:34:44.685570Z","shell.execute_reply":"2022-07-18T11:34:44.696063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" combine['Title'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.701630Z","iopub.execute_input":"2022-07-18T11:34:44.702129Z","iopub.status.idle":"2022-07-18T11:34:44.711111Z","shell.execute_reply.started":"2022-07-18T11:34:44.702077Z","shell.execute_reply":"2022-07-18T11:34:44.709834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine['Title']=combine['Title'].replace(['Dr', 'Rev','Col', 'Major', 'Lady', 'Jonkheer', 'Don','Capt','the Countess', 'Sir', 'Dona'],'Rare')\ncombine['Title']=combine['Title'].replace(['Mlle','Ms'],'Miss')\ncombine['Title']=combine['Title'].replace('Mme', 'Mrs')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.713199Z","iopub.execute_input":"2022-07-18T11:34:44.714213Z","iopub.status.idle":"2022-07-18T11:34:44.729239Z","shell.execute_reply.started":"2022-07-18T11:34:44.714165Z","shell.execute_reply":"2022-07-18T11:34:44.727925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(combine['Title'])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:44.731119Z","iopub.execute_input":"2022-07-18T11:34:44.732162Z","iopub.status.idle":"2022-07-18T11:34:45.302973Z","shell.execute_reply.started":"2022-07-18T11:34:44.732113Z","shell.execute_reply":"2022-07-18T11:34:45.301721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine[['Title', 'Survived']].groupby(['Title'], as_index = False).mean().sort_values(by = 'Survived', ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.304723Z","iopub.execute_input":"2022-07-18T11:34:45.306674Z","iopub.status.idle":"2022-07-18T11:34:45.325016Z","shell.execute_reply.started":"2022-07-18T11:34:45.306621Z","shell.execute_reply":"2022-07-18T11:34:45.323716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.factorplot(x = 'Title', y = 'Survived', data = combine, kind = 'bar')\nplt.ylabel('Survival Probability')\nplt.title('Mean of survival by Title')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.326699Z","iopub.execute_input":"2022-07-18T11:34:45.328636Z","iopub.status.idle":"2022-07-18T11:34:45.705025Z","shell.execute_reply.started":"2022-07-18T11:34:45.328591Z","shell.execute_reply":"2022-07-18T11:34:45.703771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dropping name column - unnecessary one\ncombine = combine.drop('Name', axis = 1)\ncombine.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.706441Z","iopub.execute_input":"2022-07-18T11:34:45.706765Z","iopub.status.idle":"2022-07-18T11:34:45.725026Z","shell.execute_reply.started":"2022-07-18T11:34:45.706736Z","shell.execute_reply":"2022-07-18T11:34:45.723840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Family size calculation\ncombine['FamilySize'] = combine['SibSp'] + combine['Parch'] + 1\ncombine[['SibSp', 'Parch', 'FamilySize']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.727546Z","iopub.execute_input":"2022-07-18T11:34:45.728285Z","iopub.status.idle":"2022-07-18T11:34:45.745356Z","shell.execute_reply.started":"2022-07-18T11:34:45.728237Z","shell.execute_reply":"2022-07-18T11:34:45.744199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Mean of survival by family size\n\ncombine[['FamilySize', 'Survived']].groupby('FamilySize', as_index = False).mean().sort_values(by = 'Survived', ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.747375Z","iopub.execute_input":"2022-07-18T11:34:45.748214Z","iopub.status.idle":"2022-07-18T11:34:45.765654Z","shell.execute_reply.started":"2022-07-18T11:34:45.748168Z","shell.execute_reply":"2022-07-18T11:34:45.764159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating IsAlone feature\n\ncombine['IsAlone'] = 0\ncombine.loc[combine['FamilySize'] == 1, 'IsAlone'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.767484Z","iopub.execute_input":"2022-07-18T11:34:45.768225Z","iopub.status.idle":"2022-07-18T11:34:45.776720Z","shell.execute_reply.started":"2022-07-18T11:34:45.768177Z","shell.execute_reply":"2022-07-18T11:34:45.775586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine[['IsAlone', 'Survived']].groupby('IsAlone', as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.778185Z","iopub.execute_input":"2022-07-18T11:34:45.778968Z","iopub.status.idle":"2022-07-18T11:34:45.797444Z","shell.execute_reply.started":"2022-07-18T11:34:45.778931Z","shell.execute_reply":"2022-07-18T11:34:45.796208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# Dropoing SibSp, Parch and FamilySize features from combine dataframe\n\ncombine = combine.drop(['SibSp', 'Parch', 'FamilySize'], axis = 1)\ncombine.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.799501Z","iopub.execute_input":"2022-07-18T11:34:45.800379Z","iopub.status.idle":"2022-07-18T11:34:45.819575Z","shell.execute_reply.started":"2022-07-18T11:34:45.800307Z","shell.execute_reply":"2022-07-18T11:34:45.818420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# In order to create Age Class feature we need to trasform Age into ordinal variable.\n#We will separate Age into 5 age groups and assign a number to each age groups.\n\ncombine['AgeGroup'] = pd.cut(combine['Age'], 5)\ncombine[['AgeGroup', 'Survived']].groupby('AgeGroup', as_index=False).mean().sort_values(by = 'AgeGroup')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.821577Z","iopub.execute_input":"2022-07-18T11:34:45.822458Z","iopub.status.idle":"2022-07-18T11:34:45.846395Z","shell.execute_reply.started":"2022-07-18T11:34:45.822408Z","shell.execute_reply":"2022-07-18T11:34:45.845511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assign ordinals to each age group \n\ncombine.loc[combine['Age'] <= 16.136, 'Age'] = 0\ncombine.loc[(combine['Age'] > 16.136) & (combine['Age'] <= 32.102), 'Age'] = 1\ncombine.loc[(combine['Age'] > 32.102) & (combine['Age'] <= 48.068), 'Age'] = 2\ncombine.loc[(combine['Age'] > 48.068) & (combine['Age'] <= 64.034), 'Age'] = 3\ncombine.loc[combine['Age'] > 64.034 , 'Age'] = 4","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.848161Z","iopub.execute_input":"2022-07-18T11:34:45.848921Z","iopub.status.idle":"2022-07-18T11:34:45.863183Z","shell.execute_reply.started":"2022-07-18T11:34:45.848852Z","shell.execute_reply":"2022-07-18T11:34:45.862376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping age band feature\n\ncombine = combine.drop('AgeGroup', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.864901Z","iopub.execute_input":"2022-07-18T11:34:45.865687Z","iopub.status.idle":"2022-07-18T11:34:45.872051Z","shell.execute_reply.started":"2022-07-18T11:34:45.865643Z","shell.execute_reply":"2022-07-18T11:34:45.870805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine[['Age', 'Pclass']].dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.874093Z","iopub.execute_input":"2022-07-18T11:34:45.874975Z","iopub.status.idle":"2022-07-18T11:34:45.888076Z","shell.execute_reply.started":"2022-07-18T11:34:45.874929Z","shell.execute_reply":"2022-07-18T11:34:45.886774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine['Age']=combine['Age'].astype('int')\ncombine['Age'].dtype","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.889256Z","iopub.execute_input":"2022-07-18T11:34:45.889879Z","iopub.status.idle":"2022-07-18T11:34:45.901076Z","shell.execute_reply.started":"2022-07-18T11:34:45.889846Z","shell.execute_reply":"2022-07-18T11:34:45.900196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create Age*Pclass variable\ncombine['AgePclass']=combine['Age']*combine['Pclass']\ncombine[['Age','Pclass','AgePclass']].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.902409Z","iopub.execute_input":"2022-07-18T11:34:45.903294Z","iopub.status.idle":"2022-07-18T11:34:45.917196Z","shell.execute_reply.started":"2022-07-18T11:34:45.903257Z","shell.execute_reply":"2022-07-18T11:34:45.916309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**FEATURE ENCODING**\n\nAs ML models require all the inputs and outputs variables to be numeric we need to encode all categorical data before we can fit the date to our models.\n\nWe already have encoded sex column where 0=female and 1=male. We need to do the same process for Title and Embarked. In addition, similar to the age column, we'll need to transform fare into an ordinal variable rather than conrinuous variable.","metadata":{}},{"cell_type":"code","source":"combine=pd.get_dummies(combine,columns=['Title'])\ncombine=pd.get_dummies(combine, columns=['Embarked'], prefix='Em')\ncombine.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.918691Z","iopub.execute_input":"2022-07-18T11:34:45.919541Z","iopub.status.idle":"2022-07-18T11:34:45.944888Z","shell.execute_reply.started":"2022-07-18T11:34:45.919507Z","shell.execute_reply":"2022-07-18T11:34:45.944028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine['FareGroup']=pd.cut(combine['Fare'],4)\ncombine[['FareGroup','Survived']].groupby(['FareGroup'], as_index=False).mean().sort_values(by='FareGroup')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.945914Z","iopub.execute_input":"2022-07-18T11:34:45.946834Z","iopub.status.idle":"2022-07-18T11:34:45.968131Z","shell.execute_reply.started":"2022-07-18T11:34:45.946799Z","shell.execute_reply":"2022-07-18T11:34:45.967410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The higher fare the better chances for surviving","metadata":{}},{"cell_type":"code","source":"#Assign ordinal to each fare band\ncombine.loc[combine['Fare']<=1.56, 'Fare']=0\ncombine.loc[(combine['Fare']>1.56) & (combine['Fare']<=3.119), 'Fare']=1\ncombine.loc[(combine['Fare']>3.110) & (combine['Fare']<=4.678),'Fare']=2\ncombine.loc[combine['Fare']>4.678, 'Fare']=3","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.969203Z","iopub.execute_input":"2022-07-18T11:34:45.970030Z","iopub.status.idle":"2022-07-18T11:34:45.980835Z","shell.execute_reply.started":"2022-07-18T11:34:45.969997Z","shell.execute_reply":"2022-07-18T11:34:45.979696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine['Fare']=combine['Fare'].astype('int')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.982142Z","iopub.execute_input":"2022-07-18T11:34:45.983317Z","iopub.status.idle":"2022-07-18T11:34:45.992427Z","shell.execute_reply.started":"2022-07-18T11:34:45.983270Z","shell.execute_reply":"2022-07-18T11:34:45.991289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine=combine.drop('FareGroup', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:45.994024Z","iopub.execute_input":"2022-07-18T11:34:45.994491Z","iopub.status.idle":"2022-07-18T11:34:46.005148Z","shell.execute_reply.started":"2022-07-18T11:34:45.994447Z","shell.execute_reply":"2022-07-18T11:34:46.004390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.006672Z","iopub.execute_input":"2022-07-18T11:34:46.007149Z","iopub.status.idle":"2022-07-18T11:34:46.029443Z","shell.execute_reply.started":"2022-07-18T11:34:46.007101Z","shell.execute_reply":"2022-07-18T11:34:46.027999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=combine[:len(train)]\ntest=combine[len(train):]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.031496Z","iopub.execute_input":"2022-07-18T11:34:46.032532Z","iopub.status.idle":"2022-07-18T11:34:46.039939Z","shell.execute_reply.started":"2022-07-18T11:34:46.032492Z","shell.execute_reply":"2022-07-18T11:34:46.038728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combine.info()\ntrain.info()\ntest.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.041372Z","iopub.execute_input":"2022-07-18T11:34:46.041815Z","iopub.status.idle":"2022-07-18T11:34:46.087189Z","shell.execute_reply.started":"2022-07-18T11:34:46.041770Z","shell.execute_reply":"2022-07-18T11:34:46.085645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=train.drop('PassengerId', axis=1)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.088896Z","iopub.execute_input":"2022-07-18T11:34:46.090051Z","iopub.status.idle":"2022-07-18T11:34:46.115629Z","shell.execute_reply.started":"2022-07-18T11:34:46.089992Z","shell.execute_reply":"2022-07-18T11:34:46.114032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting survived back to integer in train dataset\ntrain['Survived']=train['Survived'].astype('int')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.117649Z","iopub.execute_input":"2022-07-18T11:34:46.118540Z","iopub.status.idle":"2022-07-18T11:34:46.139933Z","shell.execute_reply.started":"2022-07-18T11:34:46.118487Z","shell.execute_reply":"2022-07-18T11:34:46.138361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.142203Z","iopub.execute_input":"2022-07-18T11:34:46.142911Z","iopub.status.idle":"2022-07-18T11:34:46.165260Z","shell.execute_reply.started":"2022-07-18T11:34:46.142858Z","shell.execute_reply":"2022-07-18T11:34:46.163553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dropping survived column from test datasset\ntest=test.drop('Survived', axis=1)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.167125Z","iopub.execute_input":"2022-07-18T11:34:46.167846Z","iopub.status.idle":"2022-07-18T11:34:46.187247Z","shell.execute_reply.started":"2022-07-18T11:34:46.167801Z","shell.execute_reply":"2022-07-18T11:34:46.186102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ML MODELLING**\nIn order to train our data and make predictions we'll need to use classification models for the Titanic dataset.\nHere are the classifiers for this task:\n1. Logistic regression\n2. SVM\n3. K-nearest neighbours\n4.Gaussian Naive Bayes\n5. Perceptron\n6. Linear SVC\n7. Stochastic gradient descent\n8. Decision tree\n9. Random forest\n10. CatBoost\n\nNow, I'll fit the training set to the models named above and evaluate their accuracy for making predictions.\nAfter determing which is the best model, I'll do hyperparameter tuning in order to boost the performance of the best model.\n\n\n**Splitting training data**\n\nFirst, we need to split the training data into independent variables, represented by X and the dependent variable represented by Y.\n\nY_train is the survived column in the training set\nX_train are the other columns in the trainng set without Survived columnn. \nOur models will learn to classify survival, Y_train based on all X_train and make predictions on X_test.","metadata":{}},{"cell_type":"code","source":"X_train=train.drop('Survived', axis=1)\nY_train=train['Survived']\nX_test=test.drop(\"PassengerId\", axis=1).copy()\n\nprint(\"X_train shape: \", X_train.shape)\nprint(\"Y_train shape: \", Y_train.shape)\nprint(\"X_test shape: \", X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.189010Z","iopub.execute_input":"2022-07-18T11:34:46.189922Z","iopub.status.idle":"2022-07-18T11:34:46.201807Z","shell.execute_reply.started":"2022-07-18T11:34:46.189873Z","shell.execute_reply":"2022-07-18T11:34:46.200812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Fit data to model and make predictions**\n\nStep 1: Instantiate the model\n\nStep 2: Fitting the training data to the training set\n\nStep 3: Predict the test set","metadata":{}},{"cell_type":"code","source":"# Logistic regression\nlogreg = LogisticRegression()\nlogreg.fit(X_train, Y_train)\nY_pred = logreg.predict(X_test)\nacc_log = round(logreg.score(X_train, Y_train)* 100,2)\nacc_log","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.203289Z","iopub.execute_input":"2022-07-18T11:34:46.203956Z","iopub.status.idle":"2022-07-18T11:34:46.268858Z","shell.execute_reply.started":"2022-07-18T11:34:46.203921Z","shell.execute_reply":"2022-07-18T11:34:46.267564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#SVM\nsvc=SVC()\nsvc.fit(X_train, Y_train)\nY_pred = svc.predict(X_test)\nacc_svc = round(svc.score(X_train, Y_train)*100,2)\nacc_svc","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.270857Z","iopub.execute_input":"2022-07-18T11:34:46.271686Z","iopub.status.idle":"2022-07-18T11:34:46.404615Z","shell.execute_reply.started":"2022-07-18T11:34:46.271635Z","shell.execute_reply":"2022-07-18T11:34:46.403425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#K-nearest Neighbours\nknn=KNeighborsClassifier(n_neighbors=5)\nknn.fit(X_train, Y_train)\nY_pred = knn.predict(X_test)\nacc_knn=round(knn.score(X_train, Y_train)*100,2)\nacc_knn","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.407087Z","iopub.execute_input":"2022-07-18T11:34:46.407447Z","iopub.status.idle":"2022-07-18T11:34:46.479287Z","shell.execute_reply.started":"2022-07-18T11:34:46.407414Z","shell.execute_reply":"2022-07-18T11:34:46.478417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Gaussian Naive Bayes\ngaussian=GaussianNB()\ngaussian.fit(X_train, Y_train)\nY_pred = gaussian.predict(X_test)\nacc_gaussian= round(gaussian.score(X_train, Y_train)*100,2)\nacc_gaussian","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.480441Z","iopub.execute_input":"2022-07-18T11:34:46.481535Z","iopub.status.idle":"2022-07-18T11:34:46.496895Z","shell.execute_reply.started":"2022-07-18T11:34:46.481485Z","shell.execute_reply":"2022-07-18T11:34:46.495681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Perceptron\nperceptron = Perceptron()\nperceptron.fit(X_train, Y_train)\nY_pred = perceptron.predict(X_test)\nacc_perceptron = round(perceptron.score(X_train, Y_train) * 100, 2)\nacc_perceptron","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.498443Z","iopub.execute_input":"2022-07-18T11:34:46.499490Z","iopub.status.idle":"2022-07-18T11:34:46.523317Z","shell.execute_reply.started":"2022-07-18T11:34:46.499442Z","shell.execute_reply":"2022-07-18T11:34:46.522169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Linear SVC\nlinear_svc=LinearSVC()\nlinear_svc.fit(X_train, Y_train)\nY_pred=linear_svc.predict(X_test)\nacc_linear_svc=round(linear_svc.score(X_train, Y_train)*100,2)\nacc_linear_svc","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.524960Z","iopub.execute_input":"2022-07-18T11:34:46.525666Z","iopub.status.idle":"2022-07-18T11:34:46.620125Z","shell.execute_reply.started":"2022-07-18T11:34:46.525613Z","shell.execute_reply":"2022-07-18T11:34:46.618868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Stochastic gradient descent\nsgd = SGDClassifier()\nsgd.fit(X_train, Y_train)\nY_pred = sgd.predict(X_test)\nacc_sgd = round(sgd.score(X_train, Y_train) * 100, 2)\nacc_sgd","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.621834Z","iopub.execute_input":"2022-07-18T11:34:46.622568Z","iopub.status.idle":"2022-07-18T11:34:46.663015Z","shell.execute_reply.started":"2022-07-18T11:34:46.622517Z","shell.execute_reply":"2022-07-18T11:34:46.661526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Decision tree\ndecision_tree=DecisionTreeClassifier()\ndecision_tree.fit(X_train, Y_train)\nY_pred = decision_tree.predict(X_test)\nacc_decision_tree=round(decision_tree.score(X_train, Y_train)*100,2)\nacc_decision_tree","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.670348Z","iopub.execute_input":"2022-07-18T11:34:46.671431Z","iopub.status.idle":"2022-07-18T11:34:46.706431Z","shell.execute_reply.started":"2022-07-18T11:34:46.671369Z","shell.execute_reply":"2022-07-18T11:34:46.705160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Random Forest\nrandom_forest=RandomForestClassifier(n_estimators=100)\nrandom_forest.fit(X_train, Y_train)\nY_pred=random_forest.predict(X_test)\nacc_random_forest=round(random_forest.score(X_train, Y_train)*100,2)\nacc_random_forest","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:46.713385Z","iopub.execute_input":"2022-07-18T11:34:46.718526Z","iopub.status.idle":"2022-07-18T11:34:47.013450Z","shell.execute_reply.started":"2022-07-18T11:34:46.718457Z","shell.execute_reply":"2022-07-18T11:34:47.012306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catboost=CatBoostClassifier()\ncatboost.fit(X_train, Y_train)\nY_pred=catboost.predict(X_test)\nacc_catboost=round(catboost.score(X_train, Y_train)*100,2)\nacc_catboost","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:47.015147Z","iopub.execute_input":"2022-07-18T11:34:47.015853Z","iopub.status.idle":"2022-07-18T11:34:47.638662Z","shell.execute_reply.started":"2022-07-18T11:34:47.015810Z","shell.execute_reply":"2022-07-18T11:34:47.637362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model evaluation + hyperparameter tuning**\n\nAfter training our models, the next step is to assess the performance of these models and select one which has the highest prediction accurecy","metadata":{}},{"cell_type":"markdown","source":"**Training accuracy**\n\nTraining accuracy shows how well the model has learned from the training set.","metadata":{}},{"cell_type":"code","source":"models = pd.DataFrame({'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression', \n                                 'Random Forest', 'Naive Bayes', 'Perceptron', 'Stochastic Gradient Decent', \n                                 'Linear SVC', 'Decision Tree', 'CatBoost'],\n                       'Score': [acc_svc, acc_knn, acc_log, acc_random_forest, acc_gaussian, acc_perceptron,\n                                 acc_sgd, acc_linear_svc, acc_decision_tree, acc_catboost]})\n\nmodels.sort_values(by = 'Score', ascending = False, ignore_index = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:47.640129Z","iopub.execute_input":"2022-07-18T11:34:47.640524Z","iopub.status.idle":"2022-07-18T11:34:47.656384Z","shell.execute_reply.started":"2022-07-18T11:34:47.640492Z","shell.execute_reply":"2022-07-18T11:34:47.654722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**K-Fold Cross Validation**\n","metadata":{}},{"cell_type":"code","source":"# Creating a list which contains classifiers \n\nclassifiers = []\nclassifiers.append(LogisticRegression())\nclassifiers.append(SVC())\nclassifiers.append(KNeighborsClassifier(n_neighbors = 5))\nclassifiers.append(GaussianNB())\nclassifiers.append(Perceptron())\nclassifiers.append(LinearSVC())\nclassifiers.append(SGDClassifier())\nclassifiers.append(DecisionTreeClassifier())\nclassifiers.append(RandomForestClassifier())\nclassifiers.append(CatBoostClassifier())\n\nlen(classifiers)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:47.658285Z","iopub.execute_input":"2022-07-18T11:34:47.658795Z","iopub.status.idle":"2022-07-18T11:34:47.673824Z","shell.execute_reply.started":"2022-07-18T11:34:47.658748Z","shell.execute_reply":"2022-07-18T11:34:47.672552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating a list which contains cross validation results for each classifier\n\ncv_results = []\nfor classifier in classifiers:\n    cv_results.append(cross_val_score(classifier, X_train, Y_train, scoring = 'accuracy', cv = 10))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:47.675732Z","iopub.execute_input":"2022-07-18T11:34:47.676222Z","iopub.status.idle":"2022-07-18T11:34:57.628748Z","shell.execute_reply.started":"2022-07-18T11:34:47.676180Z","shell.execute_reply":"2022-07-18T11:34:57.627769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_mean=[]\ncv_std=[]\nfor cv_result in cv_results:\n    cv_mean.append(cv_result.mean())\n    cv_std.append(cv_result.std())","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:57.630773Z","iopub.execute_input":"2022-07-18T11:34:57.631657Z","iopub.status.idle":"2022-07-18T11:34:57.639483Z","shell.execute_reply.started":"2022-07-18T11:34:57.631611Z","shell.execute_reply":"2022-07-18T11:34:57.638091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_res=pd.DataFrame({'Cross Validation Mean': cv_mean, 'Cross Validation Std':cv_std,\n                    'Algorithm':['Logistic Regression','SVM', 'KNN', 'Gausian Naive Bayes', 'Perceptron', 'Linear SVC', 'Stochastic Gradient Descent', 'Decision Tree', 'Random Forest', 'CatBoost']})\ncv_res.sort_values(by='Cross Validation Mean', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:57.641104Z","iopub.execute_input":"2022-07-18T11:34:57.641471Z","iopub.status.idle":"2022-07-18T11:34:57.659593Z","shell.execute_reply.started":"2022-07-18T11:34:57.641438Z","shell.execute_reply":"2022-07-18T11:34:57.658397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot('Cross Validation Mean', 'Algorithm', data=cv_res, order=cv_res.sort_values(by='Cross Validation Mean', ascending=False)['Algorithm'],palette='Set2', **{'xerr':cv_std})\nplt.title(\"Cross Validation Scores\")","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:57.661471Z","iopub.execute_input":"2022-07-18T11:34:57.661903Z","iopub.status.idle":"2022-07-18T11:34:57.855890Z","shell.execute_reply.started":"2022-07-18T11:34:57.661861Z","shell.execute_reply":"2022-07-18T11:34:57.854700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"SVM has the highest cross validation mean and therefore I'll proceed with this model","metadata":{}},{"cell_type":"code","source":"param_grid={'C':[0.1, 1,10,100,100],\n           'gamma':[1,0.1,0.01,0.001,0.0001],\n            'kernel':['rbf']}\ngrid=GridSearchCV(SVC(), param_grid, refit=True, verbose=3)\ngrid.fit(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:34:57.857442Z","iopub.execute_input":"2022-07-18T11:34:57.857887Z","iopub.status.idle":"2022-07-18T11:35:01.826481Z","shell.execute_reply.started":"2022-07-18T11:34:57.857840Z","shell.execute_reply":"2022-07-18T11:35:01.825046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Best parameters: \", grid.best_params_)\nprint(\"Best estimator: \", grid.best_estimator_)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:01.828431Z","iopub.execute_input":"2022-07-18T11:35:01.828903Z","iopub.status.idle":"2022-07-18T11:35:01.836082Z","shell.execute_reply.started":"2022-07-18T11:35:01.828857Z","shell.execute_reply":"2022-07-18T11:35:01.834850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training accuracy\nsvc=SVC(C=100, gamma=0.01, kernel='rbf')\nsvc.fit(X_train, Y_train)\nY_pred=svc.predict(X_test)\nacc_svc=round(svc.score(X_train, Y_train)*100,2)\nacc_svc","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:01.837891Z","iopub.execute_input":"2022-07-18T11:35:01.838399Z","iopub.status.idle":"2022-07-18T11:35:01.931775Z","shell.execute_reply.started":"2022-07-18T11:35:01.838352Z","shell.execute_reply":"2022-07-18T11:35:01.930646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cross Validation Mean Score\ncross_val_score(svc, X_train, Y_train, scoring='accuracy', cv=10).mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:01.933306Z","iopub.execute_input":"2022-07-18T11:35:01.934589Z","iopub.status.idle":"2022-07-18T11:35:02.347523Z","shell.execute_reply.started":"2022-07-18T11:35:01.934539Z","shell.execute_reply":"2022-07-18T11:35:02.346417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:02.348908Z","iopub.execute_input":"2022-07-18T11:35:02.349303Z","iopub.status.idle":"2022-07-18T11:35:02.359260Z","shell.execute_reply.started":"2022-07-18T11:35:02.349268Z","shell.execute_reply":"2022-07-18T11:35:02.357657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(Y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:02.360985Z","iopub.execute_input":"2022-07-18T11:35:02.361497Z","iopub.status.idle":"2022-07-18T11:35:02.369821Z","shell.execute_reply.started":"2022-07-18T11:35:02.361443Z","shell.execute_reply":"2022-07-18T11:35:02.368921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Data submission***","metadata":{}},{"cell_type":"code","source":"ss.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:02.371274Z","iopub.execute_input":"2022-07-18T11:35:02.372361Z","iopub.status.idle":"2022-07-18T11:35:02.384664Z","shell.execute_reply.started":"2022-07-18T11:35:02.372280Z","shell.execute_reply":"2022-07-18T11:35:02.383573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:02.386642Z","iopub.execute_input":"2022-07-18T11:35:02.387089Z","iopub.status.idle":"2022-07-18T11:35:02.394975Z","shell.execute_reply.started":"2022-07-18T11:35:02.387052Z","shell.execute_reply":"2022-07-18T11:35:02.393635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_data=pd.DataFrame({'PassengerId': test['PassengerId'],\n                        'Survived':Y_pred})\nsubmit_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:02.397441Z","iopub.execute_input":"2022-07-18T11:35:02.398695Z","iopub.status.idle":"2022-07-18T11:35:02.415034Z","shell.execute_reply.started":"2022-07-18T11:35:02.398642Z","shell.execute_reply":"2022-07-18T11:35:02.414182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:02.416317Z","iopub.execute_input":"2022-07-18T11:35:02.417407Z","iopub.status.idle":"2022-07-18T11:35:02.423474Z","shell.execute_reply.started":"2022-07-18T11:35:02.417304Z","shell.execute_reply":"2022-07-18T11:35:02.422477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_data.to_csv(\"subimission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:06:29.194511Z","iopub.execute_input":"2022-07-18T12:06:29.195047Z","iopub.status.idle":"2022-07-18T12:06:29.207875Z","shell.execute_reply.started":"2022-07-18T12:06:29.194991Z","shell.execute_reply":"2022-07-18T12:06:29.206476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}