{"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":"# The data has been split into two groups:\n\n1. training set (train.csv)\n\n2. test set (test.csv)\n\nYou should construct your machine learning models using the training set. We give each passengers outcome (often referred to as the ground reality) for the training set. Your model will be based on \"features\" like the class and gender of the passengers. In order to develop new features, feature engineering can also be used.\n\n You should evaluate your models performance on unobserved data using the test set. We do not give each passengers ground truth for the test set. Your responsibility is to foresee these outcomes. Use the model you trained to forecast whether each test set passenger survived the Titanics sinking for each passenger in the test set.\n \n \nVariable Notes\n\npclass: A proxy for socio-economic status (SES)\n1st = Upper\n2nd = Middle\n3rd = Lower\n\n\nage: Age is fractional if less than 1. If the age is estimated, is it in the form of xx.5\n\nsibsp: The dataset defines family relations in this way...\n\nSibling = brother, sister, stepbrother, stepsister\n\nSpouse = husband, wife (mistresses and fiancés were ignored)\n\n\nparch: The dataset defines family relations in this way...\n\nParent = mother, father\n\nChild = daughter, son, stepdaughter, stepson\n\nSome children travelled only with a nanny, therefore parch=0 for them.","metadata":{}},{"cell_type":"markdown","source":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"# data analysis and wrangling\nimport pandas as pd\nimport numpy as np\nimport random as rnd\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# visualization\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\n\n# machine learning\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC, LinearSVC\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn import metrics\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.linear_model import Perceptron\nfrom sklearn.linear_model import SGDClassifier\nfrom sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:07.831736Z","iopub.execute_input":"2022-07-16T07:36:07.832088Z","iopub.status.idle":"2022-07-16T07:36:07.845060Z","shell.execute_reply.started":"2022-07-16T07:36:07.832057Z","shell.execute_reply":"2022-07-16T07:36:07.843042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Train and Test Datasets","metadata":{}},{"cell_type":"code","source":"df1 = pd.read_csv(r\"../input/titanic/train.csv\")\ndf1","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:07.853583Z","iopub.execute_input":"2022-07-16T07:36:07.853962Z","iopub.status.idle":"2022-07-16T07:36:07.896936Z","shell.execute_reply.started":"2022-07-16T07:36:07.853930Z","shell.execute_reply":"2022-07-16T07:36:07.895598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.read_csv(r\"../input/titanic/test.csv\")\ndf2","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:07.899460Z","iopub.execute_input":"2022-07-16T07:36:07.900373Z","iopub.status.idle":"2022-07-16T07:36:07.930770Z","shell.execute_reply.started":"2022-07-16T07:36:07.900326Z","shell.execute_reply":"2022-07-16T07:36:07.929415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:07.932810Z","iopub.execute_input":"2022-07-16T07:36:07.933270Z","iopub.status.idle":"2022-07-16T07:36:07.954474Z","shell.execute_reply.started":"2022-07-16T07:36:07.933226Z","shell.execute_reply":"2022-07-16T07:36:07.953290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:07.956307Z","iopub.execute_input":"2022-07-16T07:36:07.956728Z","iopub.status.idle":"2022-07-16T07:36:07.976285Z","shell.execute_reply.started":"2022-07-16T07:36:07.956686Z","shell.execute_reply":"2022-07-16T07:36:07.975144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:07.979703Z","iopub.execute_input":"2022-07-16T07:36:07.980963Z","iopub.status.idle":"2022-07-16T07:36:07.989915Z","shell.execute_reply.started":"2022-07-16T07:36:07.980916Z","shell.execute_reply":"2022-07-16T07:36:07.988786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:07.991629Z","iopub.execute_input":"2022-07-16T07:36:07.992934Z","iopub.status.idle":"2022-07-16T07:36:08.005873Z","shell.execute_reply.started":"2022-07-16T07:36:07.992888Z","shell.execute_reply":"2022-07-16T07:36:08.004247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.007893Z","iopub.execute_input":"2022-07-16T07:36:08.009226Z","iopub.status.idle":"2022-07-16T07:36:08.017671Z","shell.execute_reply.started":"2022-07-16T07:36:08.009149Z","shell.execute_reply":"2022-07-16T07:36:08.016561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.019734Z","iopub.execute_input":"2022-07-16T07:36:08.020563Z","iopub.status.idle":"2022-07-16T07:36:08.040906Z","shell.execute_reply.started":"2022-07-16T07:36:08.020520Z","shell.execute_reply":"2022-07-16T07:36:08.039775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.042827Z","iopub.execute_input":"2022-07-16T07:36:08.043664Z","iopub.status.idle":"2022-07-16T07:36:08.076986Z","shell.execute_reply.started":"2022-07-16T07:36:08.043620Z","shell.execute_reply":"2022-07-16T07:36:08.075816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.describe(include=['O'])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.084473Z","iopub.execute_input":"2022-07-16T07:36:08.085061Z","iopub.status.idle":"2022-07-16T07:36:08.111169Z","shell.execute_reply.started":"2022-07-16T07:36:08.085016Z","shell.execute_reply":"2022-07-16T07:36:08.109932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# check for duplicate rows and delete","metadata":{}},{"cell_type":"code","source":"df1.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.113891Z","iopub.execute_input":"2022-07-16T07:36:08.114609Z","iopub.status.idle":"2022-07-16T07:36:08.127282Z","shell.execute_reply.started":"2022-07-16T07:36:08.114563Z","shell.execute_reply":"2022-07-16T07:36:08.125791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking of null values","metadata":{}},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.129308Z","iopub.execute_input":"2022-07-16T07:36:08.130016Z","iopub.status.idle":"2022-07-16T07:36:08.141798Z","shell.execute_reply.started":"2022-07-16T07:36:08.129973Z","shell.execute_reply":"2022-07-16T07:36:08.140415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Missing values percentage","metadata":{}},{"cell_type":"code","source":"def missing (df1):\n    missing_number = df1.isnull().sum().sort_values(ascending=False)\n    missing_percent = ((df1.isnull().sum()/df1.isnull().count())*100).sort_values(ascending=False)\n    missing_values = pd.concat([missing_number, missing_percent], axis=1, keys=['Missing_Number', 'Missing_Percent'])\n    return missing_values","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.143777Z","iopub.execute_input":"2022-07-16T07:36:08.144600Z","iopub.status.idle":"2022-07-16T07:36:08.154668Z","shell.execute_reply.started":"2022-07-16T07:36:08.144556Z","shell.execute_reply":"2022-07-16T07:36:08.153329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing (df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.159034Z","iopub.execute_input":"2022-07-16T07:36:08.159980Z","iopub.status.idle":"2022-07-16T07:36:08.186058Z","shell.execute_reply.started":"2022-07-16T07:36:08.159933Z","shell.execute_reply":"2022-07-16T07:36:08.184714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fill null values with mean,median ,mode\nfor i in df1.columns:\n    if df1[i].dtypes == 'object':\n        df1[i].fillna(df1[i].mode()[0], inplace=True)\n    else:\n        df1[i].fillna(df1[i].median(), inplace=True)\nprint(df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.187544Z","iopub.execute_input":"2022-07-16T07:36:08.188524Z","iopub.status.idle":"2022-07-16T07:36:08.212749Z","shell.execute_reply.started":"2022-07-16T07:36:08.188482Z","shell.execute_reply":"2022-07-16T07:36:08.211551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.214436Z","iopub.execute_input":"2022-07-16T07:36:08.215106Z","iopub.status.idle":"2022-07-16T07:36:08.224667Z","shell.execute_reply.started":"2022-07-16T07:36:08.215062Z","shell.execute_reply":"2022-07-16T07:36:08.223413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.drop([\"Name\",\"Cabin\"],axis=1,inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.226431Z","iopub.execute_input":"2022-07-16T07:36:08.227136Z","iopub.status.idle":"2022-07-16T07:36:08.237758Z","shell.execute_reply.started":"2022-07-16T07:36:08.227093Z","shell.execute_reply":"2022-07-16T07:36:08.236508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.241519Z","iopub.execute_input":"2022-07-16T07:36:08.242326Z","iopub.status.idle":"2022-07-16T07:36:08.253149Z","shell.execute_reply.started":"2022-07-16T07:36:08.242157Z","shell.execute_reply":"2022-07-16T07:36:08.252035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(df1.dtypes.map(str))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.254960Z","iopub.execute_input":"2022-07-16T07:36:08.255634Z","iopub.status.idle":"2022-07-16T07:36:08.440345Z","shell.execute_reply.started":"2022-07-16T07:36:08.255594Z","shell.execute_reply":"2022-07-16T07:36:08.439239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.dtypes.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.442016Z","iopub.execute_input":"2022-07-16T07:36:08.442715Z","iopub.status.idle":"2022-07-16T07:36:08.453474Z","shell.execute_reply.started":"2022-07-16T07:36:08.442671Z","shell.execute_reply":"2022-07-16T07:36:08.451742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unique Values","metadata":{}},{"cell_type":"code","source":"for i in df1.columns:\n    print('colum_name',i)\n    print('unique',df1[i].unique())\n    print('\\n')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.455322Z","iopub.execute_input":"2022-07-16T07:36:08.456341Z","iopub.status.idle":"2022-07-16T07:36:08.477462Z","shell.execute_reply.started":"2022-07-16T07:36:08.456292Z","shell.execute_reply":"2022-07-16T07:36:08.476284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# check for duplicate rows and delete","metadata":{}},{"cell_type":"code","source":"df1.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.479292Z","iopub.execute_input":"2022-07-16T07:36:08.480054Z","iopub.status.idle":"2022-07-16T07:36:08.491622Z","shell.execute_reply.started":"2022-07-16T07:36:08.480009Z","shell.execute_reply":"2022-07-16T07:36:08.490285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Relationship with numerical variables","metadata":{}},{"cell_type":"code","source":"df1.plot.scatter(x='Age', y='Fare', ylim=(0,180),xlim=(0,100));","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.499050Z","iopub.execute_input":"2022-07-16T07:36:08.499531Z","iopub.status.idle":"2022-07-16T07:36:08.763668Z","shell.execute_reply.started":"2022-07-16T07:36:08.499493Z","shell.execute_reply":"2022-07-16T07:36:08.762280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.plot.scatter(x='Pclass', y='Fare', ylim=(0,100),xlim=(0,5));","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:08.765639Z","iopub.execute_input":"2022-07-16T07:36:08.766073Z","iopub.status.idle":"2022-07-16T07:36:09.009697Z","shell.execute_reply.started":"2022-07-16T07:36:08.766031Z","shell.execute_reply":"2022-07-16T07:36:09.008404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Relationship with categorical features","metadata":{}},{"cell_type":"code","source":"sns.boxplot(x='Embarked', y=\"Fare\", data=df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:09.011772Z","iopub.execute_input":"2022-07-16T07:36:09.012213Z","iopub.status.idle":"2022-07-16T07:36:09.218438Z","shell.execute_reply.started":"2022-07-16T07:36:09.012150Z","shell.execute_reply":"2022-07-16T07:36:09.217364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(y='Age', x=\"Ticket\", data=df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:09.219803Z","iopub.execute_input":"2022-07-16T07:36:09.220688Z","iopub.status.idle":"2022-07-16T07:36:29.884951Z","shell.execute_reply.started":"2022-07-16T07:36:09.220642Z","shell.execute_reply":"2022-07-16T07:36:29.883337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can Conclude that\ni) LotArea and Enclosed Porch appear to have a linear relationship to SalePrice.\n\nii) Both of the correlations are positive, so if one variable rises, the other rises as well.\n\niii) We can observe that the slope of the linear relationship is especially steep in the instance of \"EnclosedPorch.\"\n\niv) The terms \"HouseStyle\" and \"RoofStyle\" also appear to be connected to \"SalePrice.\"\n\nv) In the case of \"HouseStyle,\" where the box plot illustrates how sales prices rise with the Style of House, the association appears to be greater.\n\nvi) Just four variables were examined, but there are many more that need examination.\n\nvii) The challenge here appears to be in selecting the appropriate characteristics (feature selection), not in defining their intricate interrelationships (feature engineering).","metadata":{}},{"cell_type":"code","source":"df1[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:29.886637Z","iopub.execute_input":"2022-07-16T07:36:29.886977Z","iopub.status.idle":"2022-07-16T07:36:29.902231Z","shell.execute_reply.started":"2022-07-16T07:36:29.886944Z","shell.execute_reply":"2022-07-16T07:36:29.901050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1[[\"Sex\", \"Survived\"]].groupby(['Sex'], as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:29.903544Z","iopub.execute_input":"2022-07-16T07:36:29.903898Z","iopub.status.idle":"2022-07-16T07:36:29.925388Z","shell.execute_reply.started":"2022-07-16T07:36:29.903815Z","shell.execute_reply":"2022-07-16T07:36:29.924339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1[[\"SibSp\", \"Survived\"]].groupby(['SibSp'], as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:29.926839Z","iopub.execute_input":"2022-07-16T07:36:29.927140Z","iopub.status.idle":"2022-07-16T07:36:29.945382Z","shell.execute_reply.started":"2022-07-16T07:36:29.927113Z","shell.execute_reply":"2022-07-16T07:36:29.944229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1[[\"Parch\", \"Survived\"]].groupby(['Parch'], as_index=False).mean().sort_values(by='Survived', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:29.947159Z","iopub.execute_input":"2022-07-16T07:36:29.947609Z","iopub.status.idle":"2022-07-16T07:36:29.963982Z","shell.execute_reply.started":"2022-07-16T07:36:29.947567Z","shell.execute_reply":"2022-07-16T07:36:29.963057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = sns.FacetGrid(df1, col='Survived')\ng.map(plt.hist, 'Age', bins=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:29.965653Z","iopub.execute_input":"2022-07-16T07:36:29.965982Z","iopub.status.idle":"2022-07-16T07:36:30.554439Z","shell.execute_reply.started":"2022-07-16T07:36:29.965952Z","shell.execute_reply":"2022-07-16T07:36:30.553217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid = sns.FacetGrid(train_df, col='Pclass', hue='Survived')\ngrid = sns.FacetGrid(df1, col='Survived', row='Pclass', size=2.2, aspect=1.6)\ngrid.map(plt.hist, 'Age', alpha=.5, bins=20)\ngrid.add_legend();","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:30.555900Z","iopub.execute_input":"2022-07-16T07:36:30.556304Z","iopub.status.idle":"2022-07-16T07:36:32.107371Z","shell.execute_reply.started":"2022-07-16T07:36:30.556186Z","shell.execute_reply":"2022-07-16T07:36:32.106057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid = sns.FacetGrid(train_df, col='Embarked')\ngrid = sns.FacetGrid(df1, 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-16T07:36:32.108972Z","iopub.execute_input":"2022-07-16T07:36:32.109441Z","iopub.status.idle":"2022-07-16T07:36:33.166365Z","shell.execute_reply.started":"2022-07-16T07:36:32.109398Z","shell.execute_reply":"2022-07-16T07:36:33.165229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid = sns.FacetGrid(train_df, col='Embarked', hue='Survived', palette={0: 'k', 1: 'w'})\ngrid = sns.FacetGrid(df1, row='Embarked', col='Survived', size=2.2, aspect=1.6)\ngrid.map(sns.barplot, 'Sex', 'Fare', alpha=.5, ci=None)\ngrid.add_legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:33.167571Z","iopub.execute_input":"2022-07-16T07:36:33.167882Z","iopub.status.idle":"2022-07-16T07:36:34.066519Z","shell.execute_reply.started":"2022-07-16T07:36:33.167855Z","shell.execute_reply":"2022-07-16T07:36:34.065437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid = sns.FacetGrid(train_df, col='Pclass', hue='Gender')\ngrid = sns.FacetGrid(df1, row='Pclass', col='Sex', size=2.2, aspect=1.6)\ngrid.map(plt.hist, 'Age', alpha=.5, bins=20)\ngrid.add_legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:34.068285Z","iopub.execute_input":"2022-07-16T07:36:34.068703Z","iopub.status.idle":"2022-07-16T07:36:35.510639Z","shell.execute_reply.started":"2022-07-16T07:36:34.068662Z","shell.execute_reply":"2022-07-16T07:36:35.509501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check for special characters and in this data set there are no special characters","metadata":{}},{"cell_type":"code","source":"#for loop for unique values.\nfor i in df1.columns:\n    print('\\n',i,'\\n',df1[i].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:35.512013Z","iopub.execute_input":"2022-07-16T07:36:35.512361Z","iopub.status.idle":"2022-07-16T07:36:35.529682Z","shell.execute_reply.started":"2022-07-16T07:36:35.512332Z","shell.execute_reply":"2022-07-16T07:36:35.528443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting numerical data and categorical data in dataset","metadata":{}},{"cell_type":"code","source":"# finding numerical data and categorical data in dataset\nnumerical= df1.drop(['Survived'], axis=1).select_dtypes('number').columns\n\ncategorical = df1.select_dtypes('object').columns\n\nprint(f'Numerical Columns:  {df1[numerical].columns}')\nprint('\\n')\nprint(f'Categorical Columns: {df1[categorical].columns}')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:35.531308Z","iopub.execute_input":"2022-07-16T07:36:35.532108Z","iopub.status.idle":"2022-07-16T07:36:35.545529Z","shell.execute_reply.started":"2022-07-16T07:36:35.532063Z","shell.execute_reply":"2022-07-16T07:36:35.544384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Find out statistical values, Skewness and Kurtosis","metadata":{}},{"cell_type":"code","source":"for col in df1.columns:\n    if df1[col].dtypes != object:\n        q1 = df1[col].quantile(0.25)\n        q2 = df1[col].quantile(0.50)\n        q3 = df1[col].quantile(0.75)\n        IQR = q3 - q1\n        llp = q1-1.5*IQR\n        ulp = q3+1.5*IQR\n        print('column name',col)\n        print('q1',q1)\n        print('q2',q2)\n        print('q3',q3)\n        print('IQR',IQR)\n        print('llp',llp)\n        print('ulp',ulp)\n        print('mean:',df1[col].mean())\n        print('median:',df1[col].median())\n        print('mode',df1[col].mode()[0])\n        print('skewness:',df1[col].skew())\n        print('kurtosis:',df1[col].kurtosis())\n        print('std',df1[col].std())\n        print('max',df1[col].max())\n        print('min',df1[col].min())\n        print('null_value count:',df1[col].isnull().sum())\n        print('\\n')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:35.547320Z","iopub.execute_input":"2022-07-16T07:36:35.547769Z","iopub.status.idle":"2022-07-16T07:36:35.593376Z","shell.execute_reply.started":"2022-07-16T07:36:35.547727Z","shell.execute_reply":"2022-07-16T07:36:35.592160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Auto Visualization","metadata":{}},{"cell_type":"code","source":"pip install autoviz","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:35.594861Z","iopub.execute_input":"2022-07-16T07:36:35.595145Z","iopub.status.idle":"2022-07-16T07:36:45.278880Z","shell.execute_reply.started":"2022-07-16T07:36:35.595119Z","shell.execute_reply":"2022-07-16T07:36:45.277357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from autoviz.AutoViz_Class import AutoViz_Class\nAV = AutoViz_Class()\ndf_av = AV.AutoViz('../input/titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:45.280728Z","iopub.execute_input":"2022-07-16T07:36:45.281053Z","iopub.status.idle":"2022-07-16T07:36:49.936367Z","shell.execute_reply.started":"2022-07-16T07:36:45.281022Z","shell.execute_reply":"2022-07-16T07:36:49.934953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:49.938571Z","iopub.execute_input":"2022-07-16T07:36:49.939269Z","iopub.status.idle":"2022-07-16T07:36:49.945173Z","shell.execute_reply.started":"2022-07-16T07:36:49.939218Z","shell.execute_reply":"2022-07-16T07:36:49.943856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nfor col in df1.columns:\n    if df1[col].dtypes == 'object':\n        df1[col] = le.fit_transform(df1[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:49.946717Z","iopub.execute_input":"2022-07-16T07:36:49.947084Z","iopub.status.idle":"2022-07-16T07:36:49.961156Z","shell.execute_reply.started":"2022-07-16T07:36:49.947053Z","shell.execute_reply":"2022-07-16T07:36:49.959983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying Correlation","metadata":{}},{"cell_type":"code","source":"df1.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:49.962263Z","iopub.execute_input":"2022-07-16T07:36:49.962866Z","iopub.status.idle":"2022-07-16T07:36:49.987803Z","shell.execute_reply.started":"2022-07-16T07:36:49.962833Z","shell.execute_reply":"2022-07-16T07:36:49.987079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40,15))\na=sns.heatmap(df1.corr(),annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:49.988993Z","iopub.execute_input":"2022-07-16T07:36:49.989491Z","iopub.status.idle":"2022-07-16T07:36:50.686069Z","shell.execute_reply.started":"2022-07-16T07:36:49.989462Z","shell.execute_reply":"2022-07-16T07:36:50.685242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Selection","metadata":{}},{"cell_type":"code","source":"X=df1.drop(['Survived'], axis=1)\ny=df1['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.687163Z","iopub.execute_input":"2022-07-16T07:36:50.687701Z","iopub.status.idle":"2022-07-16T07:36:50.694830Z","shell.execute_reply.started":"2022-07-16T07:36:50.687670Z","shell.execute_reply":"2022-07-16T07:36:50.693754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.696348Z","iopub.execute_input":"2022-07-16T07:36:50.696782Z","iopub.status.idle":"2022-07-16T07:36:50.721727Z","shell.execute_reply.started":"2022-07-16T07:36:50.696750Z","shell.execute_reply":"2022-07-16T07:36:50.720977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.722892Z","iopub.execute_input":"2022-07-16T07:36:50.723386Z","iopub.status.idle":"2022-07-16T07:36:50.730646Z","shell.execute_reply.started":"2022-07-16T07:36:50.723354Z","shell.execute_reply":"2022-07-16T07:36:50.729633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting the DataSet into Train and Test","metadata":{}},{"cell_type":"code","source":"#doing Test Train.\nfrom sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=100,stratify=y)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.731950Z","iopub.execute_input":"2022-07-16T07:36:50.732502Z","iopub.status.idle":"2022-07-16T07:36:50.743824Z","shell.execute_reply.started":"2022-07-16T07:36:50.732470Z","shell.execute_reply":"2022-07-16T07:36:50.742671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.745118Z","iopub.execute_input":"2022-07-16T07:36:50.745635Z","iopub.status.idle":"2022-07-16T07:36:50.771511Z","shell.execute_reply.started":"2022-07-16T07:36:50.745602Z","shell.execute_reply":"2022-07-16T07:36:50.770450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.778864Z","iopub.execute_input":"2022-07-16T07:36:50.779265Z","iopub.status.idle":"2022-07-16T07:36:50.799497Z","shell.execute_reply.started":"2022-07-16T07:36:50.779225Z","shell.execute_reply":"2022-07-16T07:36:50.798384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracies of different algorithms applied","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nlog = LogisticRegression()\nlog.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.801065Z","iopub.execute_input":"2022-07-16T07:36:50.801394Z","iopub.status.idle":"2022-07-16T07:36:50.838417Z","shell.execute_reply.started":"2022-07-16T07:36:50.801365Z","shell.execute_reply":"2022-07-16T07:36:50.837270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_lr = log.predict(X_test)\ny_pred_lr","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.840276Z","iopub.execute_input":"2022-07-16T07:36:50.840696Z","iopub.status.idle":"2022-07-16T07:36:50.850142Z","shell.execute_reply.started":"2022-07-16T07:36:50.840654Z","shell.execute_reply":"2022-07-16T07:36:50.848861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import  confusion_matrix,accuracy_score\ncm=confusion_matrix(y_test,y_pred_lr)\nprint(cm)\naccuracy_score(y_test,y_pred_lr)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.851595Z","iopub.execute_input":"2022-07-16T07:36:50.851953Z","iopub.status.idle":"2022-07-16T07:36:50.865055Z","shell.execute_reply.started":"2022-07-16T07:36:50.851921Z","shell.execute_reply":"2022-07-16T07:36:50.864189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Classification Report\nfrom sklearn.metrics import classification_report\nprint(classification_report(y_test, y_pred_lr))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.866362Z","iopub.execute_input":"2022-07-16T07:36:50.867036Z","iopub.status.idle":"2022-07-16T07:36:50.887124Z","shell.execute_reply.started":"2022-07-16T07:36:50.867005Z","shell.execute_reply":"2022-07-16T07:36:50.885954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TEST DATA","metadata":{}},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.888958Z","iopub.execute_input":"2022-07-16T07:36:50.890143Z","iopub.status.idle":"2022-07-16T07:36:50.913509Z","shell.execute_reply.started":"2022-07-16T07:36:50.890090Z","shell.execute_reply":"2022-07-16T07:36:50.912692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing(df2)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.914480Z","iopub.execute_input":"2022-07-16T07:36:50.915364Z","iopub.status.idle":"2022-07-16T07:36:50.931984Z","shell.execute_reply.started":"2022-07-16T07:36:50.915332Z","shell.execute_reply":"2022-07-16T07:36:50.930674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dropping Null Values more than 40%","metadata":{}},{"cell_type":"code","source":"for col in df2.columns:\n    if df2[col].isnull().mean()*100>40:\n        df2.drop(col,axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.933354Z","iopub.execute_input":"2022-07-16T07:36:50.933762Z","iopub.status.idle":"2022-07-16T07:36:50.943795Z","shell.execute_reply.started":"2022-07-16T07:36:50.933730Z","shell.execute_reply":"2022-07-16T07:36:50.942435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.945429Z","iopub.execute_input":"2022-07-16T07:36:50.945737Z","iopub.status.idle":"2022-07-16T07:36:50.971190Z","shell.execute_reply.started":"2022-07-16T07:36:50.945709Z","shell.execute_reply":"2022-07-16T07:36:50.970021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# fill null values with mean,median ,mode","metadata":{}},{"cell_type":"code","source":"#fill null values with mean,median ,mode\nfor i in df2.columns:\n    if df2[i].dtypes == 'object':\n        df2[i].fillna(df2[i].mode()[0], inplace=True)\n    else:\n        df2[i].fillna(df2[i].median(), inplace=True)\nprint(df2)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.972448Z","iopub.execute_input":"2022-07-16T07:36:50.973090Z","iopub.status.idle":"2022-07-16T07:36:50.997970Z","shell.execute_reply.started":"2022-07-16T07:36:50.973045Z","shell.execute_reply":"2022-07-16T07:36:50.996815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"df2.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:50.999410Z","iopub.execute_input":"2022-07-16T07:36:51.000154Z","iopub.status.idle":"2022-07-16T07:36:51.018724Z","shell.execute_reply.started":"2022-07-16T07:36:51.000095Z","shell.execute_reply":"2022-07-16T07:36:51.017360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.020398Z","iopub.execute_input":"2022-07-16T07:36:51.021601Z","iopub.status.idle":"2022-07-16T07:36:51.041174Z","shell.execute_reply.started":"2022-07-16T07:36:51.021567Z","shell.execute_reply":"2022-07-16T07:36:51.039982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.042864Z","iopub.execute_input":"2022-07-16T07:36:51.043691Z","iopub.status.idle":"2022-07-16T07:36:51.055256Z","shell.execute_reply.started":"2022-07-16T07:36:51.043641Z","shell.execute_reply":"2022-07-16T07:36:51.054372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.056336Z","iopub.execute_input":"2022-07-16T07:36:51.057171Z","iopub.status.idle":"2022-07-16T07:36:51.067891Z","shell.execute_reply.started":"2022-07-16T07:36:51.057125Z","shell.execute_reply":"2022-07-16T07:36:51.066558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le=LabelEncoder()\nfor col in df2.columns:\n    if df2[col].dtypes == 'object':\n        df2[col]= le.fit_transform(df2[col])","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.069518Z","iopub.execute_input":"2022-07-16T07:36:51.070666Z","iopub.status.idle":"2022-07-16T07:36:51.083640Z","shell.execute_reply.started":"2022-07-16T07:36:51.070615Z","shell.execute_reply":"2022-07-16T07:36:51.082532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf2.drop([\"Name\"],axis=1,inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.084954Z","iopub.execute_input":"2022-07-16T07:36:51.085861Z","iopub.status.idle":"2022-07-16T07:36:51.092469Z","shell.execute_reply.started":"2022-07-16T07:36:51.085822Z","shell.execute_reply":"2022-07-16T07:36:51.091276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid = log.predict(df2)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.093874Z","iopub.execute_input":"2022-07-16T07:36:51.094831Z","iopub.status.idle":"2022-07-16T07:36:51.106136Z","shell.execute_reply.started":"2022-07-16T07:36:51.094788Z","shell.execute_reply":"2022-07-16T07:36:51.105076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.107924Z","iopub.execute_input":"2022-07-16T07:36:51.109127Z","iopub.status.idle":"2022-07-16T07:36:51.118561Z","shell.execute_reply.started":"2022-07-16T07:36:51.109081Z","shell.execute_reply":"2022-07-16T07:36:51.117713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n        \"PassengerId\": df2[\"PassengerId\"],\n        \"Survived\": valid\n    })\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.119728Z","iopub.execute_input":"2022-07-16T07:36:51.120477Z","iopub.status.idle":"2022-07-16T07:36:51.138382Z","shell.execute_reply.started":"2022-07-16T07:36:51.120433Z","shell.execute_reply":"2022-07-16T07:36:51.137250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission2.csv', index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-16T07:36:51.140145Z","iopub.execute_input":"2022-07-16T07:36:51.140942Z","iopub.status.idle":"2022-07-16T07:36:51.157384Z","shell.execute_reply.started":"2022-07-16T07:36:51.140898Z","shell.execute_reply":"2022-07-16T07:36:51.155896Z"},"trusted":true},"execution_count":null,"outputs":[]}]}