{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n%matplotlib inline","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-24T12:46:00.881183Z","iopub.execute_input":"2022-07-24T12:46:00.881628Z","iopub.status.idle":"2022-07-24T12:46:01.370184Z","shell.execute_reply.started":"2022-07-24T12:46:00.881538Z","shell.execute_reply":"2022-07-24T12:46:01.369055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#loading the dataset\ndf=pd.read_csv('../input/titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.371925Z","iopub.execute_input":"2022-07-24T12:46:01.372334Z","iopub.status.idle":"2022-07-24T12:46:01.383087Z","shell.execute_reply.started":"2022-07-24T12:46:01.372303Z","shell.execute_reply":"2022-07-24T12:46:01.382061Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reading the dataset\ndf","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-24T12:46:01.384407Z","iopub.execute_input":"2022-07-24T12:46:01.385162Z","iopub.status.idle":"2022-07-24T12:46:01.413623Z","shell.execute_reply.started":"2022-07-24T12:46:01.385133Z","shell.execute_reply":"2022-07-24T12:46:01.412406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Description of the Dataset","metadata":{}},{"cell_type":"raw","source":"survival - Survival (0 = No; 1 = Yes)\nclass - Passenger Class (1 = 1st; 2 = 2nd; 3 = 3rd)\nname - Name\nsex - Sex\nage - Age\nsibsp - Number of Siblings/Spouses Aboard\nparch - Number of Parents/Children Aboard\nticket - Ticket Number\nfare - Passenger Fare\ncabin - Cabin\nembarked - Port of Embarkation (C = Cherbourg; Q = Queenstown; S = Southampton)\nboat - Lifeboat (if survived)\nbody - Body number (if did not survive and body was recovered)","metadata":{}},{"cell_type":"code","source":"#reading the first five rows of the dataset\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.416590Z","iopub.execute_input":"2022-07-24T12:46:01.417038Z","iopub.status.idle":"2022-07-24T12:46:01.433522Z","shell.execute_reply.started":"2022-07-24T12:46:01.416998Z","shell.execute_reply":"2022-07-24T12:46:01.432292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reading the last five rows of the dataset\ndf.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.434840Z","iopub.execute_input":"2022-07-24T12:46:01.435893Z","iopub.status.idle":"2022-07-24T12:46:01.455304Z","shell.execute_reply.started":"2022-07-24T12:46:01.435845Z","shell.execute_reply":"2022-07-24T12:46:01.454224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#summary of the dataset\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.456742Z","iopub.execute_input":"2022-07-24T12:46:01.457444Z","iopub.status.idle":"2022-07-24T12:46:01.471301Z","shell.execute_reply.started":"2022-07-24T12:46:01.457414Z","shell.execute_reply":"2022-07-24T12:46:01.470336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#statistical summary of the dataset\ndf.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.472748Z","iopub.execute_input":"2022-07-24T12:46:01.473226Z","iopub.status.idle":"2022-07-24T12:46:01.503736Z","shell.execute_reply.started":"2022-07-24T12:46:01.473197Z","shell.execute_reply":"2022-07-24T12:46:01.502851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Statisticl summary of Categorical values\ndf.describe(include='O')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.504712Z","iopub.execute_input":"2022-07-24T12:46:01.505504Z","iopub.status.idle":"2022-07-24T12:46:01.525294Z","shell.execute_reply.started":"2022-07-24T12:46:01.505474Z","shell.execute_reply":"2022-07-24T12:46:01.524211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#rows and columns present in this data set\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.526545Z","iopub.execute_input":"2022-07-24T12:46:01.527014Z","iopub.status.idle":"2022-07-24T12:46:01.534697Z","shell.execute_reply.started":"2022-07-24T12:46:01.526981Z","shell.execute_reply":"2022-07-24T12:46:01.533706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"there are 891 rows and 12 columns\n","metadata":{}},{"cell_type":"code","source":"#lloking for null values in the dataset\ndf.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.538381Z","iopub.execute_input":"2022-07-24T12:46:01.539053Z","iopub.status.idle":"2022-07-24T12:46:01.550628Z","shell.execute_reply.started":"2022-07-24T12:46:01.539010Z","shell.execute_reply":"2022-07-24T12:46:01.549701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are null values present in this dataset","metadata":{}},{"cell_type":"code","source":"train_data = df.copy()\ntrain_data[\"Age\"].fillna(df[\"Age\"].median(skipna=True), inplace=True)\ntrain_data[\"Embarked\"].fillna(df['Embarked'].value_counts().idxmax(), inplace=True)\ntrain_data.drop('Cabin', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.552096Z","iopub.execute_input":"2022-07-24T12:46:01.552915Z","iopub.status.idle":"2022-07-24T12:46:01.563706Z","shell.execute_reply.started":"2022-07-24T12:46:01.552870Z","shell.execute_reply":"2022-07-24T12:46:01.562707Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nax = df[\"Age\"].hist(bins=15, density=True, stacked=True, color='teal', alpha=0.6)\ndf[\"Age\"].plot(kind='density', color='teal')\nax = train_data[\"Age\"].hist(bins=15, density=True, stacked=True, color='orange', alpha=0.5)\ntrain_data[\"Age\"].plot(kind='density', color='orange')\nax.legend(['Raw Age', 'Adjusted Age'])\nax.set(xlabel='Age')\nplt.xlim(-10,85)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.564892Z","iopub.execute_input":"2022-07-24T12:46:01.565742Z","iopub.status.idle":"2022-07-24T12:46:01.896873Z","shell.execute_reply.started":"2022-07-24T12:46:01.565713Z","shell.execute_reply":"2022-07-24T12:46:01.895698Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Handling the Null Values","metadata":{}},{"cell_type":"code","source":"#plotting a graph to look at the Missing Values \nsns.barplot(data=df.isnull(),color='teal')\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:01.898482Z","iopub.execute_input":"2022-07-24T12:46:01.899129Z","iopub.status.idle":"2022-07-24T12:46:02.514780Z","shell.execute_reply.started":"2022-07-24T12:46:01.899094Z","shell.execute_reply":"2022-07-24T12:46:02.513733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#lloking for null values in the dataset\ndf.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:02.516282Z","iopub.execute_input":"2022-07-24T12:46:02.516597Z","iopub.status.idle":"2022-07-24T12:46:02.526282Z","shell.execute_reply.started":"2022-07-24T12:46:02.516569Z","shell.execute_reply":"2022-07-24T12:46:02.525002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('The percentage of Missing value in the Age column is %.2f%%'%(df['Age'].isnull().sum()/df.shape[0]*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:02.527755Z","iopub.execute_input":"2022-07-24T12:46:02.528079Z","iopub.status.idle":"2022-07-24T12:46:02.538818Z","shell.execute_reply.started":"2022-07-24T12:46:02.528051Z","shell.execute_reply":"2022-07-24T12:46:02.537879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plotting a histogram graph to che check the distribution in the age \nsns.set(style='white')\nsns.set(style='whitegrid',color_codes=True)\na=df['Age'].hist(bins=15,stacked=True,density=True,color='Teal',alpha=0.6)\ndf['Age'].plot(kind='density')\nplt.show()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-24T12:46:02.540131Z","iopub.execute_input":"2022-07-24T12:46:02.540499Z","iopub.status.idle":"2022-07-24T12:46:02.833218Z","shell.execute_reply.started":"2022-07-24T12:46:02.540467Z","shell.execute_reply":"2022-07-24T12:46:02.832023Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(df['Age'],kde=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:02.834683Z","iopub.execute_input":"2022-07-24T12:46:02.835838Z","iopub.status.idle":"2022-07-24T12:46:03.208147Z","shell.execute_reply.started":"2022-07-24T12:46:02.835790Z","shell.execute_reply":"2022-07-24T12:46:03.206882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This is a right skewed plot, where we can fill in the missing values with mean or median","metadata":{}},{"cell_type":"markdown","source":"#### Now looking at the mean and median values of the age record fot this dataset\n","metadata":{}},{"cell_type":"code","source":"print('The Mean of the Age column is %.2f%%' % (df['Age'].mean(skipna=True)))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.209526Z","iopub.execute_input":"2022-07-24T12:46:03.210093Z","iopub.status.idle":"2022-07-24T12:46:03.215913Z","shell.execute_reply.started":"2022-07-24T12:46:03.210058Z","shell.execute_reply":"2022-07-24T12:46:03.215109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('The Median of the Age column is %.2f%%' % (df['Age'].median(skipna=True)))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.216886Z","iopub.execute_input":"2022-07-24T12:46:03.217557Z","iopub.status.idle":"2022-07-24T12:46:03.228152Z","shell.execute_reply.started":"2022-07-24T12:46:03.217526Z","shell.execute_reply":"2022-07-24T12:46:03.227387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=df.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.229593Z","iopub.execute_input":"2022-07-24T12:46:03.229919Z","iopub.status.idle":"2022-07-24T12:46:03.238546Z","shell.execute_reply.started":"2022-07-24T12:46:03.229892Z","shell.execute_reply":"2022-07-24T12:46:03.237344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.240153Z","iopub.execute_input":"2022-07-24T12:46:03.241112Z","iopub.status.idle":"2022-07-24T12:46:03.266117Z","shell.execute_reply.started":"2022-07-24T12:46:03.241066Z","shell.execute_reply":"2022-07-24T12:46:03.265002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['Age'].fillna(df1['Age'].median(skipna=True),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.267475Z","iopub.execute_input":"2022-07-24T12:46:03.267803Z","iopub.status.idle":"2022-07-24T12:46:03.274636Z","shell.execute_reply.started":"2022-07-24T12:46:03.267774Z","shell.execute_reply":"2022-07-24T12:46:03.273513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.276413Z","iopub.execute_input":"2022-07-24T12:46:03.277040Z","iopub.status.idle":"2022-07-24T12:46:03.291006Z","shell.execute_reply.started":"2022-07-24T12:46:03.277006Z","shell.execute_reply":"2022-07-24T12:46:03.290191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cabin","metadata":{}},{"cell_type":"code","source":"print('The percentage of Missing value in the Cabin column is %.2f%%'%(df['Cabin'].isnull().sum()/df.shape[0]*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.296937Z","iopub.execute_input":"2022-07-24T12:46:03.297286Z","iopub.status.idle":"2022-07-24T12:46:03.304932Z","shell.execute_reply.started":"2022-07-24T12:46:03.297255Z","shell.execute_reply":"2022-07-24T12:46:03.304026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Since the Cabin Feature is missing 77% of the records , it better to remove the Cabin Feature from the datset,Because fill with some other values may cause to a different output","metadata":{}},{"cell_type":"code","source":"df1.drop('Cabin',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.306136Z","iopub.execute_input":"2022-07-24T12:46:03.307128Z","iopub.status.idle":"2022-07-24T12:46:03.319801Z","shell.execute_reply.started":"2022-07-24T12:46:03.307093Z","shell.execute_reply":"2022-07-24T12:46:03.318747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.321015Z","iopub.execute_input":"2022-07-24T12:46:03.321519Z","iopub.status.idle":"2022-07-24T12:46:03.337267Z","shell.execute_reply.started":"2022-07-24T12:46:03.321490Z","shell.execute_reply":"2022-07-24T12:46:03.336123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Embarked","metadata":{}},{"cell_type":"code","source":"df1.loc[:,['Embarked']]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.338491Z","iopub.execute_input":"2022-07-24T12:46:03.339407Z","iopub.status.idle":"2022-07-24T12:46:03.351398Z","shell.execute_reply.started":"2022-07-24T12:46:03.339373Z","shell.execute_reply":"2022-07-24T12:46:03.350065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('The Percentage of missing Record in the Embarked column is %.2f%%' % (df1['Embarked'].isnull().sum()/df.shape[0]*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.352463Z","iopub.execute_input":"2022-07-24T12:46:03.353168Z","iopub.status.idle":"2022-07-24T12:46:03.359212Z","shell.execute_reply.started":"2022-07-24T12:46:03.353133Z","shell.execute_reply":"2022-07-24T12:46:03.358120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As only 0.22% of records are missing from the Embarked we can fill the values with the mode value/Most frequent value","metadata":{}},{"cell_type":"code","source":"df1['Embarked'].mode()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.360772Z","iopub.execute_input":"2022-07-24T12:46:03.361852Z","iopub.status.idle":"2022-07-24T12:46:03.373708Z","shell.execute_reply.started":"2022-07-24T12:46:03.361809Z","shell.execute_reply":"2022-07-24T12:46:03.372389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(kind='count',x='Embarked',data=df1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.375398Z","iopub.execute_input":"2022-07-24T12:46:03.376178Z","iopub.status.idle":"2022-07-24T12:46:03.580499Z","shell.execute_reply.started":"2022-07-24T12:46:03.376128Z","shell.execute_reply":"2022-07-24T12:46:03.579596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"Embarked - Port of Embarkation (C = Cherbourg; Q = Queenstown; S = Southampton)\nEmbarked meaning is where the traveler mounted from\nFrom the graph we came to know that most of the people have departed from S which is Southampton\nWe will fill the missing values of the Embarked feature with S","metadata":{}},{"cell_type":"code","source":"df1.loc[df1['Embarked'].isnull()==True,'Embarked']='S'","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.582155Z","iopub.execute_input":"2022-07-24T12:46:03.582505Z","iopub.status.idle":"2022-07-24T12:46:03.589319Z","shell.execute_reply.started":"2022-07-24T12:46:03.582475Z","shell.execute_reply":"2022-07-24T12:46:03.588128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.590931Z","iopub.execute_input":"2022-07-24T12:46:03.591478Z","iopub.status.idle":"2022-07-24T12:46:03.604520Z","shell.execute_reply.started":"2022-07-24T12:46:03.591433Z","shell.execute_reply":"2022-07-24T12:46:03.603708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"    All the Null values are handled","metadata":{}},{"cell_type":"code","source":"#Checking for duplicate values\ndf1.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.605962Z","iopub.execute_input":"2022-07-24T12:46:03.606515Z","iopub.status.idle":"2022-07-24T12:46:03.616098Z","shell.execute_reply.started":"2022-07-24T12:46:03.606485Z","shell.execute_reply":"2022-07-24T12:46:03.614684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"    There are no Duplicate Values in this Dataset","metadata":{}},{"cell_type":"code","source":"#dropping the PassengerId and Name column from the dataset\ndf1.drop(['Name','PassengerId'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.617087Z","iopub.execute_input":"2022-07-24T12:46:03.617847Z","iopub.status.idle":"2022-07-24T12:46:03.623572Z","shell.execute_reply.started":"2022-07-24T12:46:03.617811Z","shell.execute_reply":"2022-07-24T12:46:03.622794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropping the Ticket column from the dataset\ndf1.drop(['Ticket'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.624689Z","iopub.execute_input":"2022-07-24T12:46:03.625912Z","iopub.status.idle":"2022-07-24T12:46:03.635382Z","shell.execute_reply.started":"2022-07-24T12:46:03.625865Z","shell.execute_reply":"2022-07-24T12:46:03.634531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.636546Z","iopub.execute_input":"2022-07-24T12:46:03.637450Z","iopub.status.idle":"2022-07-24T12:46:03.660218Z","shell.execute_reply.started":"2022-07-24T12:46:03.637417Z","shell.execute_reply":"2022-07-24T12:46:03.659098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"   # Exploratory Data Analysis","metadata":{}},{"cell_type":"markdown","source":"# Uni-Variate Analysis","metadata":{}},{"cell_type":"markdown","source":"#### Target variable","metadata":{}},{"cell_type":"markdown","source":"## Survived","metadata":{}},{"cell_type":"code","source":"#plotting a count plot\nsns.countplot('Survived',data=df1,palette=\"husl\")\nplt.title('Survived',fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.661709Z","iopub.execute_input":"2022-07-24T12:46:03.662608Z","iopub.status.idle":"2022-07-24T12:46:03.775688Z","shell.execute_reply.started":"2022-07-24T12:46:03.662562Z","shell.execute_reply":"2022-07-24T12:46:03.774271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unique features for Survived \ndf1['Survived'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.777454Z","iopub.execute_input":"2022-07-24T12:46:03.778781Z","iopub.status.idle":"2022-07-24T12:46:03.788993Z","shell.execute_reply.started":"2022-07-24T12:46:03.778727Z","shell.execute_reply":"2022-07-24T12:46:03.787800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"1. The data is imbalanced\n\n2. Survival - Survival (0 = No; 1 = Yes)\n\n3. Most of the people in the titanic accident have not Survived","metadata":{}},{"cell_type":"markdown","source":"## Gender","metadata":{}},{"cell_type":"code","source":"#plotting a donut chart for the gender column\n#the graph is plotted usingmatplotlib library \nplt.figure(figsize=(6,6))\nplt.pie(df['Sex'].value_counts(),labels=['Male','Female'],data=df1,autopct='%.2f%%',\n        colors=('chartreuse','cadetblue'),\n       shadow=True,center=(0,1),wedgeprops = {'linewidth': 3},labeldistance =1.2)\ncentre_circle = plt.Circle((0, 1), 0.50, fc='white')\nfig = plt.gcf()\n# Adding Circle in Pie chart\nfig.gca().add_artist(centre_circle)\nplt.title('Gender',fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.790412Z","iopub.execute_input":"2022-07-24T12:46:03.791740Z","iopub.status.idle":"2022-07-24T12:46:03.899263Z","shell.execute_reply.started":"2022-07-24T12:46:03.791690Z","shell.execute_reply":"2022-07-24T12:46:03.897954Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['Sex'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.900879Z","iopub.execute_input":"2022-07-24T12:46:03.901850Z","iopub.status.idle":"2022-07-24T12:46:03.912135Z","shell.execute_reply.started":"2022-07-24T12:46:03.901805Z","shell.execute_reply":"2022-07-24T12:46:03.910656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"1. The Female gender has Survival rate more than the male gender as we know from the Titanic Story that only the Ladies and Childrens were first helped for rescue","metadata":{}},{"cell_type":"markdown","source":"## Gender Analysis","metadata":{}},{"cell_type":"code","source":"df1.loc[:,['Sex','Age']]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.913921Z","iopub.execute_input":"2022-07-24T12:46:03.915120Z","iopub.status.idle":"2022-07-24T12:46:03.936268Z","shell.execute_reply.started":"2022-07-24T12:46:03.915071Z","shell.execute_reply":"2022-07-24T12:46:03.935397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(df1[(df1['Sex']=='male')])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.937748Z","iopub.execute_input":"2022-07-24T12:46:03.938216Z","iopub.status.idle":"2022-07-24T12:46:03.961611Z","shell.execute_reply.started":"2022-07-24T12:46:03.938168Z","shell.execute_reply":"2022-07-24T12:46:03.960506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#male children among 577 male\n(df1.loc[(df1['Sex']=='male')&(df1['Age']<18)])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.963496Z","iopub.execute_input":"2022-07-24T12:46:03.964274Z","iopub.status.idle":"2022-07-24T12:46:03.993569Z","shell.execute_reply.started":"2022-07-24T12:46:03.964230Z","shell.execute_reply":"2022-07-24T12:46:03.992402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# male childrens in among the male\nlen(df1[(df1['Sex']=='male')&(df1['Age']<18)])","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:03.995468Z","iopub.execute_input":"2022-07-24T12:46:03.996282Z","iopub.status.idle":"2022-07-24T12:46:04.006010Z","shell.execute_reply.started":"2022-07-24T12:46:03.996237Z","shell.execute_reply":"2022-07-24T12:46:04.004732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"1.From the analysis we get to know that there are only 58 male children Survived among the 577 Male","metadata":{}},{"cell_type":"markdown","source":"# P-Class","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=df1,x='Pclass',palette='pastel')\nplt.title('Passenger Class',fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:04.008069Z","iopub.execute_input":"2022-07-24T12:46:04.008941Z","iopub.status.idle":"2022-07-24T12:46:04.131896Z","shell.execute_reply.started":"2022-07-24T12:46:04.008898Z","shell.execute_reply":"2022-07-24T12:46:04.130732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"1.Most of the People from Passenger class 3 has Survived the most","metadata":{}},{"cell_type":"markdown","source":"# Age","metadata":{}},{"cell_type":"code","source":"#plotting a histogram graph to che check the distribution in the age \nsns.set(style='white')\nsns.set(style='whitegrid',color_codes=True)\na=df1['Age'].hist(bins=15,stacked=True,density=True,color='firebrick',alpha=0.6)\ndf1['Age'].plot(kind='density')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:04.133350Z","iopub.execute_input":"2022-07-24T12:46:04.134449Z","iopub.status.idle":"2022-07-24T12:46:04.356884Z","shell.execute_reply.started":"2022-07-24T12:46:04.134401Z","shell.execute_reply":"2022-07-24T12:46:04.355717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parch","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(7,7))\nsns.histplot(x=df1['Parch'],data='df1',color='mediumvioletred')\nplt.margins(x=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:04.358206Z","iopub.execute_input":"2022-07-24T12:46:04.358976Z","iopub.status.idle":"2022-07-24T12:46:04.564303Z","shell.execute_reply.started":"2022-07-24T12:46:04.358932Z","shell.execute_reply":"2022-07-24T12:46:04.563059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Parch'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:04.565910Z","iopub.execute_input":"2022-07-24T12:46:04.566919Z","iopub.status.idle":"2022-07-24T12:46:04.576911Z","shell.execute_reply.started":"2022-07-24T12:46:04.566883Z","shell.execute_reply":"2022-07-24T12:46:04.575486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"1.The 0th Class has more value which means Either the Person has to be single \n    other wise Must be a Error in the data collection (missing data)\n2. there is only one person with a family member of 6 people","metadata":{}},{"cell_type":"markdown","source":"# Fare","metadata":{}},{"cell_type":"code","source":"df1['Fare'].hist(color='peru',bins=40,figsize=(8,4))\nplt.title('Fare',fontsize='18')\nplt.margins(x=0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:04.579351Z","iopub.execute_input":"2022-07-24T12:46:04.579896Z","iopub.status.idle":"2022-07-24T12:46:04.805407Z","shell.execute_reply.started":"2022-07-24T12:46:04.579862Z","shell.execute_reply":"2022-07-24T12:46:04.804091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(17,8))\nplt.plot(df[\"Fare\"],color='blueviolet')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:04.806955Z","iopub.execute_input":"2022-07-24T12:46:04.807392Z","iopub.status.idle":"2022-07-24T12:46:05.062584Z","shell.execute_reply.started":"2022-07-24T12:46:04.807351Z","shell.execute_reply":"2022-07-24T12:46:05.061762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Fare'].value_counts().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:05.063731Z","iopub.execute_input":"2022-07-24T12:46:05.064405Z","iopub.status.idle":"2022-07-24T12:46:05.073866Z","shell.execute_reply.started":"2022-07-24T12:46:05.064374Z","shell.execute_reply":"2022-07-24T12:46:05.072835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"1. There is a lot of variation in the Fare price it is because of Different Passenger Classes and different Places of Embarked ","metadata":{}},{"cell_type":"markdown","source":"# Bi-Variate Analysis ","metadata":{}},{"cell_type":"code","source":"sns.barplot(x='Sex',y='Age',hue='Pclass',data=df1,color=\"darkorange\")\nplt.title('Passenger Class with Gender and Age')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:05.075247Z","iopub.execute_input":"2022-07-24T12:46:05.075555Z","iopub.status.idle":"2022-07-24T12:46:05.430992Z","shell.execute_reply.started":"2022-07-24T12:46:05.075526Z","shell.execute_reply":"2022-07-24T12:46:05.429694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Passenger Classs with Survival","metadata":{}},{"cell_type":"code","source":"sns.barplot(x='Pclass',y='Survived',data=df1,color=\"darkturquoise\")\nplt.title('Passenger Classs with Survival')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:05.432558Z","iopub.execute_input":"2022-07-24T12:46:05.432933Z","iopub.status.idle":"2022-07-24T12:46:05.824829Z","shell.execute_reply.started":"2022-07-24T12:46:05.432903Z","shell.execute_reply":"2022-07-24T12:46:05.823633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The people from the 1st class have survived more and then the people from the 2nd class has survived more than the people from the 3rd class.","metadata":{}},{"cell_type":"markdown","source":"Class - Passenger Class (1 = 1st; 2 = 2nd; 3 = 3rd)","metadata":{}},{"cell_type":"markdown","source":"## Siblings ","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(17,7))\nsns.swarmplot(x=df['SibSp'],y=df['PassengerId'],color='seagreen')\nplt.title('Siblings',fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:05.826189Z","iopub.execute_input":"2022-07-24T12:46:05.826572Z","iopub.status.idle":"2022-07-24T12:46:06.310965Z","shell.execute_reply.started":"2022-07-24T12:46:05.826539Z","shell.execute_reply":"2022-07-24T12:46:06.309729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"1. There are lot of people from 0th class and very less number in 5th .\n\n2. It means that Most of the people are here without Sibling.","metadata":{}},{"cell_type":"markdown","source":"# Passsenger Survived with Gender","metadata":{}},{"cell_type":"code","source":"sns.factorplot('Pclass','Survived',hue='Sex',data=df1,palette='Set2')\nplt.title('Passsenger Survived with Gender',fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:06.312992Z","iopub.execute_input":"2022-07-24T12:46:06.313869Z","iopub.status.idle":"2022-07-24T12:46:06.850690Z","shell.execute_reply.started":"2022-07-24T12:46:06.313820Z","shell.execute_reply":"2022-07-24T12:46:06.849465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Age for Surviving Population and Deceased Population","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nsns.kdeplot(df1[\"Age\"][df1.Survived == 1], color=\"dodgerblue\", shade=True)\nsns.kdeplot(df1[\"Age\"][df1.Survived == 0], color=\"mediumorchid\", shade=True)\nplt.legend(['Survived', 'Died'])\nplt.title('Density Plot of Age for Surviving Population and Deceased Population')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:06.852075Z","iopub.execute_input":"2022-07-24T12:46:06.852927Z","iopub.status.idle":"2022-07-24T12:46:07.143251Z","shell.execute_reply.started":"2022-07-24T12:46:06.852892Z","shell.execute_reply":"2022-07-24T12:46:07.142041Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Converting Categorical to Numerical","metadata":{}},{"cell_type":"code","source":"#importing the Label Encoder Sub module from the sklearn, Preprocessing \nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.144867Z","iopub.execute_input":"2022-07-24T12:46:07.145328Z","iopub.status.idle":"2022-07-24T12:46:07.190296Z","shell.execute_reply.started":"2022-07-24T12:46:07.145274Z","shell.execute_reply":"2022-07-24T12:46:07.189100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lc=LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.191581Z","iopub.execute_input":"2022-07-24T12:46:07.191899Z","iopub.status.idle":"2022-07-24T12:46:07.197153Z","shell.execute_reply.started":"2022-07-24T12:46:07.191871Z","shell.execute_reply":"2022-07-24T12:46:07.196044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#applying labelEncoder to the Gender and Embarked\ndf1.Sex=lc.fit_transform(df1.Sex)\ndf1.Embarked=lc.fit_transform(df1.Embarked)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.198801Z","iopub.execute_input":"2022-07-24T12:46:07.199232Z","iopub.status.idle":"2022-07-24T12:46:07.208500Z","shell.execute_reply.started":"2022-07-24T12:46:07.199189Z","shell.execute_reply":"2022-07-24T12:46:07.207710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"The converted labels for Embarked\nc=0\nq=1\ns=2","metadata":{}},{"cell_type":"markdown","source":"The converted labels for Gender\nfemale=0\nmale=1","metadata":{}},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.210293Z","iopub.execute_input":"2022-07-24T12:46:07.211098Z","iopub.status.idle":"2022-07-24T12:46:07.232666Z","shell.execute_reply.started":"2022-07-24T12:46:07.211055Z","shell.execute_reply":"2022-07-24T12:46:07.231169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.234519Z","iopub.execute_input":"2022-07-24T12:46:07.235324Z","iopub.status.idle":"2022-07-24T12:46:07.244041Z","shell.execute_reply.started":"2022-07-24T12:46:07.235278Z","shell.execute_reply":"2022-07-24T12:46:07.243117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#heatmap sjhowing the correlation\nplt.figure(figsize=(15,8))\nsns.heatmap(df1.corr(),cmap='YlGnBu',annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.245251Z","iopub.execute_input":"2022-07-24T12:46:07.247961Z","iopub.status.idle":"2022-07-24T12:46:07.762951Z","shell.execute_reply.started":"2022-07-24T12:46:07.247916Z","shell.execute_reply":"2022-07-24T12:46:07.761796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropping the SibSp and Parch column to avoid Multicollinearity\ndf1.drop(['SibSp','Parch'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.764834Z","iopub.execute_input":"2022-07-24T12:46:07.765505Z","iopub.status.idle":"2022-07-24T12:46:07.771793Z","shell.execute_reply.started":"2022-07-24T12:46:07.765461Z","shell.execute_reply":"2022-07-24T12:46:07.770991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.783758Z","iopub.execute_input":"2022-07-24T12:46:07.784491Z","iopub.status.idle":"2022-07-24T12:46:07.800852Z","shell.execute_reply.started":"2022-07-24T12:46:07.784448Z","shell.execute_reply":"2022-07-24T12:46:07.799720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nsns.heatmap(df1.corr(),cmap='BuPu',annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:07.802415Z","iopub.execute_input":"2022-07-24T12:46:07.802878Z","iopub.status.idle":"2022-07-24T12:46:08.190301Z","shell.execute_reply.started":"2022-07-24T12:46:07.802833Z","shell.execute_reply":"2022-07-24T12:46:08.189166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.192056Z","iopub.execute_input":"2022-07-24T12:46:08.192508Z","iopub.status.idle":"2022-07-24T12:46:08.210459Z","shell.execute_reply.started":"2022-07-24T12:46:08.192464Z","shell.execute_reply":"2022-07-24T12:46:08.209221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Test Split","metadata":{}},{"cell_type":"code","source":"x=df1.drop(['Survived'],axis=1)\ny=df1.Survived","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.211762Z","iopub.execute_input":"2022-07-24T12:46:08.212124Z","iopub.status.idle":"2022-07-24T12:46:08.220155Z","shell.execute_reply.started":"2022-07-24T12:46:08.212081Z","shell.execute_reply":"2022-07-24T12:46:08.219406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.221443Z","iopub.execute_input":"2022-07-24T12:46:08.222022Z","iopub.status.idle":"2022-07-24T12:46:08.242058Z","shell.execute_reply.started":"2022-07-24T12:46:08.221992Z","shell.execute_reply":"2022-07-24T12:46:08.241194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.243113Z","iopub.execute_input":"2022-07-24T12:46:08.244164Z","iopub.status.idle":"2022-07-24T12:46:08.251275Z","shell.execute_reply.started":"2022-07-24T12:46:08.244132Z","shell.execute_reply":"2022-07-24T12:46:08.250456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre-Processing","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.252471Z","iopub.execute_input":"2022-07-24T12:46:08.253199Z","iopub.status.idle":"2022-07-24T12:46:08.259243Z","shell.execute_reply.started":"2022-07-24T12:46:08.253166Z","shell.execute_reply":"2022-07-24T12:46:08.258404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc=StandardScaler()\ns_x=sc.fit_transform(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.260282Z","iopub.execute_input":"2022-07-24T12:46:08.261026Z","iopub.status.idle":"2022-07-24T12:46:08.273357Z","shell.execute_reply.started":"2022-07-24T12:46:08.260997Z","shell.execute_reply":"2022-07-24T12:46:08.271989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(s_x,y,random_state=0,test_size=0.25)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.274946Z","iopub.execute_input":"2022-07-24T12:46:08.276285Z","iopub.status.idle":"2022-07-24T12:46:08.297363Z","shell.execute_reply.started":"2022-07-24T12:46:08.276247Z","shell.execute_reply":"2022-07-24T12:46:08.296490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.298567Z","iopub.execute_input":"2022-07-24T12:46:08.299465Z","iopub.status.idle":"2022-07-24T12:46:08.305841Z","shell.execute_reply.started":"2022-07-24T12:46:08.299432Z","shell.execute_reply":"2022-07-24T12:46:08.304907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.307149Z","iopub.execute_input":"2022-07-24T12:46:08.308140Z","iopub.status.idle":"2022-07-24T12:46:08.317374Z","shell.execute_reply.started":"2022-07-24T12:46:08.308097Z","shell.execute_reply":"2022-07-24T12:46:08.316300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.319440Z","iopub.execute_input":"2022-07-24T12:46:08.321427Z","iopub.status.idle":"2022-07-24T12:46:08.329158Z","shell.execute_reply.started":"2022-07-24T12:46:08.321392Z","shell.execute_reply":"2022-07-24T12:46:08.328087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.330667Z","iopub.execute_input":"2022-07-24T12:46:08.331777Z","iopub.status.idle":"2022-07-24T12:46:08.341022Z","shell.execute_reply.started":"2022-07-24T12:46:08.331730Z","shell.execute_reply":"2022-07-24T12:46:08.340001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"markdown","source":"## Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.342485Z","iopub.execute_input":"2022-07-24T12:46:08.343076Z","iopub.status.idle":"2022-07-24T12:46:08.368595Z","shell.execute_reply.started":"2022-07-24T12:46:08.343045Z","shell.execute_reply":"2022-07-24T12:46:08.367692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg=LogisticRegression()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.370000Z","iopub.execute_input":"2022-07-24T12:46:08.370579Z","iopub.status.idle":"2022-07-24T12:46:08.374552Z","shell.execute_reply.started":"2022-07-24T12:46:08.370549Z","shell.execute_reply":"2022-07-24T12:46:08.373716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.376167Z","iopub.execute_input":"2022-07-24T12:46:08.376809Z","iopub.status.idle":"2022-07-24T12:46:08.393125Z","shell.execute_reply.started":"2022-07-24T12:46:08.376774Z","shell.execute_reply":"2022-07-24T12:46:08.391941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_pred=logreg.predict(x_train)\nx_test_pred=logreg.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.394660Z","iopub.execute_input":"2022-07-24T12:46:08.395233Z","iopub.status.idle":"2022-07-24T12:46:08.400343Z","shell.execute_reply.started":"2022-07-24T12:46:08.395198Z","shell.execute_reply":"2022-07-24T12:46:08.399436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg.score(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.401630Z","iopub.execute_input":"2022-07-24T12:46:08.402607Z","iopub.status.idle":"2022-07-24T12:46:08.411090Z","shell.execute_reply.started":"2022-07-24T12:46:08.402567Z","shell.execute_reply":"2022-07-24T12:46:08.409847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training score for Logistic Regression is %.1f%%' % (logreg.score(x_train,y_train)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.412368Z","iopub.execute_input":"2022-07-24T12:46:08.412702Z","iopub.status.idle":"2022-07-24T12:46:08.419760Z","shell.execute_reply.started":"2022-07-24T12:46:08.412671Z","shell.execute_reply":"2022-07-24T12:46:08.418719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg.score(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.421157Z","iopub.execute_input":"2022-07-24T12:46:08.421459Z","iopub.status.idle":"2022-07-24T12:46:08.430131Z","shell.execute_reply.started":"2022-07-24T12:46:08.421432Z","shell.execute_reply":"2022-07-24T12:46:08.429130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Testing score for Logistic Regression is %.2f%%' % (logreg.score(x_test,y_test)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.431507Z","iopub.execute_input":"2022-07-24T12:46:08.431875Z","iopub.status.idle":"2022-07-24T12:46:08.440286Z","shell.execute_reply.started":"2022-07-24T12:46:08.431843Z","shell.execute_reply":"2022-07-24T12:46:08.439217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Result\n### This is Good Model","metadata":{}},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"pd.crosstab(y_test,x_test_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.441418Z","iopub.execute_input":"2022-07-24T12:46:08.441834Z","iopub.status.idle":"2022-07-24T12:46:08.466280Z","shell.execute_reply.started":"2022-07-24T12:46:08.441799Z","shell.execute_reply":"2022-07-24T12:46:08.465242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.467521Z","iopub.execute_input":"2022-07-24T12:46:08.468313Z","iopub.status.idle":"2022-07-24T12:46:08.473049Z","shell.execute_reply.started":"2022-07-24T12:46:08.468275Z","shell.execute_reply":"2022-07-24T12:46:08.471994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"report=classification_report(y_test,x_test_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.474225Z","iopub.execute_input":"2022-07-24T12:46:08.474975Z","iopub.status.idle":"2022-07-24T12:46:08.489010Z","shell.execute_reply.started":"2022-07-24T12:46:08.474941Z","shell.execute_reply":"2022-07-24T12:46:08.487720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(report)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.490973Z","iopub.execute_input":"2022-07-24T12:46:08.491688Z","iopub.status.idle":"2022-07-24T12:46:08.498093Z","shell.execute_reply.started":"2022-07-24T12:46:08.491615Z","shell.execute_reply":"2022-07-24T12:46:08.496865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This is a Good Accuracy 80 % without Balancing the dataset","metadata":{}},{"cell_type":"markdown","source":"# Balancing the Dataset","metadata":{}},{"cell_type":"markdown","source":"# SMOTE","metadata":{}},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.499886Z","iopub.execute_input":"2022-07-24T12:46:08.501016Z","iopub.status.idle":"2022-07-24T12:46:08.561429Z","shell.execute_reply.started":"2022-07-24T12:46:08.500979Z","shell.execute_reply":"2022-07-24T12:46:08.560518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"smote=SMOTE()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.562510Z","iopub.execute_input":"2022-07-24T12:46:08.563369Z","iopub.status.idle":"2022-07-24T12:46:08.568230Z","shell.execute_reply.started":"2022-07-24T12:46:08.563333Z","shell.execute_reply":"2022-07-24T12:46:08.566915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_smote, y_smote = smote.fit_resample(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.569667Z","iopub.execute_input":"2022-07-24T12:46:08.570127Z","iopub.status.idle":"2022-07-24T12:46:08.586246Z","shell.execute_reply.started":"2022-07-24T12:46:08.570084Z","shell.execute_reply":"2022-07-24T12:46:08.585373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\nprint(\"Actual Classes\",Counter(y))\nprint(\"SMOTE Classes\",Counter(y_smote))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.587908Z","iopub.execute_input":"2022-07-24T12:46:08.589238Z","iopub.status.idle":"2022-07-24T12:46:08.594581Z","shell.execute_reply.started":"2022-07-24T12:46:08.589207Z","shell.execute_reply":"2022-07-24T12:46:08.593769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nxs_train,xs_test,ys_train,ys_test=train_test_split(X_smote,y_smote,random_state=0,test_size=0.25)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.595859Z","iopub.execute_input":"2022-07-24T12:46:08.596334Z","iopub.status.idle":"2022-07-24T12:46:08.606870Z","shell.execute_reply.started":"2022-07-24T12:46:08.596297Z","shell.execute_reply":"2022-07-24T12:46:08.605842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.608066Z","iopub.execute_input":"2022-07-24T12:46:08.608883Z","iopub.status.idle":"2022-07-24T12:46:08.614516Z","shell.execute_reply.started":"2022-07-24T12:46:08.608852Z","shell.execute_reply":"2022-07-24T12:46:08.613582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg=LogisticRegression()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.615842Z","iopub.execute_input":"2022-07-24T12:46:08.616896Z","iopub.status.idle":"2022-07-24T12:46:08.625354Z","shell.execute_reply.started":"2022-07-24T12:46:08.616853Z","shell.execute_reply":"2022-07-24T12:46:08.624080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg.fit(xs_train,ys_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.626353Z","iopub.execute_input":"2022-07-24T12:46:08.626847Z","iopub.status.idle":"2022-07-24T12:46:08.654472Z","shell.execute_reply.started":"2022-07-24T12:46:08.626816Z","shell.execute_reply":"2022-07-24T12:46:08.653300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xs_train_pred=logreg.predict(xs_train)\nxs_test_pred=logreg.predict(xs_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.655884Z","iopub.execute_input":"2022-07-24T12:46:08.656321Z","iopub.status.idle":"2022-07-24T12:46:08.664526Z","shell.execute_reply.started":"2022-07-24T12:46:08.656288Z","shell.execute_reply":"2022-07-24T12:46:08.663777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg.score(xs_train,ys_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.666343Z","iopub.execute_input":"2022-07-24T12:46:08.666810Z","iopub.status.idle":"2022-07-24T12:46:08.677407Z","shell.execute_reply.started":"2022-07-24T12:46:08.666768Z","shell.execute_reply":"2022-07-24T12:46:08.676186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training score AFTER Balancing for Logistic Regression is %.1f%%' % (logreg.score(xs_train,ys_train)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.680443Z","iopub.execute_input":"2022-07-24T12:46:08.681090Z","iopub.status.idle":"2022-07-24T12:46:08.690916Z","shell.execute_reply.started":"2022-07-24T12:46:08.681057Z","shell.execute_reply":"2022-07-24T12:46:08.689547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg.score(xs_test,ys_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.693682Z","iopub.execute_input":"2022-07-24T12:46:08.694297Z","iopub.status.idle":"2022-07-24T12:46:08.704022Z","shell.execute_reply.started":"2022-07-24T12:46:08.694256Z","shell.execute_reply":"2022-07-24T12:46:08.703002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Testing score AFTER Balancing for Logistic Regression is %.2f%%' % (logreg.score(xs_test,ys_test)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.705380Z","iopub.execute_input":"2022-07-24T12:46:08.706206Z","iopub.status.idle":"2022-07-24T12:46:08.715747Z","shell.execute_reply.started":"2022-07-24T12:46:08.706160Z","shell.execute_reply":"2022-07-24T12:46:08.714551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Result\n### This is Good Model","metadata":{}},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"pd.crosstab(ys_test,xs_test_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.717755Z","iopub.execute_input":"2022-07-24T12:46:08.718496Z","iopub.status.idle":"2022-07-24T12:46:08.744555Z","shell.execute_reply.started":"2022-07-24T12:46:08.718449Z","shell.execute_reply":"2022-07-24T12:46:08.743374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.746101Z","iopub.execute_input":"2022-07-24T12:46:08.746837Z","iopub.status.idle":"2022-07-24T12:46:08.752195Z","shell.execute_reply.started":"2022-07-24T12:46:08.746793Z","shell.execute_reply":"2022-07-24T12:46:08.750918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"report=classification_report(ys_test,xs_test_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.753529Z","iopub.execute_input":"2022-07-24T12:46:08.753938Z","iopub.status.idle":"2022-07-24T12:46:08.770449Z","shell.execute_reply.started":"2022-07-24T12:46:08.753911Z","shell.execute_reply":"2022-07-24T12:46:08.768664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(report)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.772032Z","iopub.execute_input":"2022-07-24T12:46:08.772632Z","iopub.status.idle":"2022-07-24T12:46:08.777968Z","shell.execute_reply.started":"2022-07-24T12:46:08.772589Z","shell.execute_reply":"2022-07-24T12:46:08.777066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This Gives better reult after balancing the data compared to thee data before balancing","metadata":{}},{"cell_type":"markdown","source":"# Decision Tree Classifier","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.779135Z","iopub.execute_input":"2022-07-24T12:46:08.780075Z","iopub.status.idle":"2022-07-24T12:46:08.789160Z","shell.execute_reply.started":"2022-07-24T12:46:08.780016Z","shell.execute_reply":"2022-07-24T12:46:08.788276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DTC=DecisionTreeClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.790850Z","iopub.execute_input":"2022-07-24T12:46:08.792121Z","iopub.status.idle":"2022-07-24T12:46:08.802009Z","shell.execute_reply.started":"2022-07-24T12:46:08.792068Z","shell.execute_reply":"2022-07-24T12:46:08.800834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DTC.fit(xs_train,ys_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.803489Z","iopub.execute_input":"2022-07-24T12:46:08.804176Z","iopub.status.idle":"2022-07-24T12:46:08.825450Z","shell.execute_reply.started":"2022-07-24T12:46:08.804141Z","shell.execute_reply":"2022-07-24T12:46:08.817931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DTC.fit(xs_train,ys_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.827255Z","iopub.execute_input":"2022-07-24T12:46:08.828417Z","iopub.status.idle":"2022-07-24T12:46:08.842130Z","shell.execute_reply.started":"2022-07-24T12:46:08.828370Z","shell.execute_reply":"2022-07-24T12:46:08.840958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DTCpred=DTC.predict(xs_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.843800Z","iopub.execute_input":"2022-07-24T12:46:08.844479Z","iopub.status.idle":"2022-07-24T12:46:08.852000Z","shell.execute_reply.started":"2022-07-24T12:46:08.844440Z","shell.execute_reply":"2022-07-24T12:46:08.851182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DTCpred","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.854146Z","iopub.execute_input":"2022-07-24T12:46:08.854513Z","iopub.status.idle":"2022-07-24T12:46:08.865506Z","shell.execute_reply.started":"2022-07-24T12:46:08.854482Z","shell.execute_reply":"2022-07-24T12:46:08.864400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Decision Tree Regressor Training Score %.2f%% '%(DTC.score(xs_train,ys_train)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.867227Z","iopub.execute_input":"2022-07-24T12:46:08.868054Z","iopub.status.idle":"2022-07-24T12:46:08.879775Z","shell.execute_reply.started":"2022-07-24T12:46:08.868012Z","shell.execute_reply":"2022-07-24T12:46:08.878580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Decision Tree Regressor Testing Score %.2f%% '%(DTC.score(xs_test,ys_test)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.881791Z","iopub.execute_input":"2022-07-24T12:46:08.882525Z","iopub.status.idle":"2022-07-24T12:46:08.894484Z","shell.execute_reply.started":"2022-07-24T12:46:08.882483Z","shell.execute_reply":"2022-07-24T12:46:08.893157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# XGB Classifier","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.896308Z","iopub.execute_input":"2022-07-24T12:46:08.897056Z","iopub.status.idle":"2022-07-24T12:46:08.922769Z","shell.execute_reply.started":"2022-07-24T12:46:08.897015Z","shell.execute_reply":"2022-07-24T12:46:08.921903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb = XGBClassifier()\nxgb.fit(xs_train,ys_train)# fitting the data\nxgbrpred22=xgb.predict(xs_test)#predicting the price\nxgbrpred2=xgb.predict(xs_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:08.924311Z","iopub.execute_input":"2022-07-24T12:46:08.924975Z","iopub.status.idle":"2022-07-24T12:46:10.036262Z","shell.execute_reply.started":"2022-07-24T12:46:08.924931Z","shell.execute_reply":"2022-07-24T12:46:10.035376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Gradient Boosting Training Score %.2f%% '%(xgb.score(xs_train,ys_train)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:10.037803Z","iopub.execute_input":"2022-07-24T12:46:10.038413Z","iopub.status.idle":"2022-07-24T12:46:10.051387Z","shell.execute_reply.started":"2022-07-24T12:46:10.038380Z","shell.execute_reply":"2022-07-24T12:46:10.050217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Gradient Boosting Testing Score %.2f%% '%(xgb.score(xs_test,ys_test)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:10.053551Z","iopub.execute_input":"2022-07-24T12:46:10.054623Z","iopub.status.idle":"2022-07-24T12:46:10.071432Z","shell.execute_reply.started":"2022-07-24T12:46:10.054577Z","shell.execute_reply":"2022-07-24T12:46:10.070235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb2 = XGBClassifier(subsample=0.7, n_estimators=1000, max_depth= 3, learning_rate=0.3, gamma=4, colsample_bytree=0.7)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:10.073007Z","iopub.execute_input":"2022-07-24T12:46:10.073329Z","iopub.status.idle":"2022-07-24T12:46:10.078005Z","shell.execute_reply.started":"2022-07-24T12:46:10.073302Z","shell.execute_reply":"2022-07-24T12:46:10.077107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"xgb2.fit(xs_train,ys_train)# fitting the data\nxgbrpred22=xgb2.predict(xs_test)#predicting the price\nxgbrpred2=xgb2.predict(xs_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:10.079377Z","iopub.execute_input":"2022-07-24T12:46:10.079963Z","iopub.status.idle":"2022-07-24T12:46:12.569380Z","shell.execute_reply.started":"2022-07-24T12:46:10.079933Z","shell.execute_reply":"2022-07-24T12:46:12.568318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Gradient Boosting Training Score %.2f%% '%(xgb2.score(xs_train,ys_train)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:12.570840Z","iopub.execute_input":"2022-07-24T12:46:12.571769Z","iopub.status.idle":"2022-07-24T12:46:12.587744Z","shell.execute_reply.started":"2022-07-24T12:46:12.571723Z","shell.execute_reply":"2022-07-24T12:46:12.586718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Gradient Boosting Testing Score %.2f%% '%(xgb2.score(xs_test,ys_test)*100))","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:46:12.588880Z","iopub.execute_input":"2022-07-24T12:46:12.589190Z","iopub.status.idle":"2022-07-24T12:46:12.600882Z","shell.execute_reply.started":"2022-07-24T12:46:12.589163Z","shell.execute_reply":"2022-07-24T12:46:12.599862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion","metadata":{"_kg_hide-output":true}},{"cell_type":"markdown","source":"# **The Extreme Gradient Boosting after Tuning Gives the Best Score!!! **","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}