{"cells":[{"metadata":{"_uuid":"e06d05845bc890b3920aea1a3f559e15b914f99b"},"cell_type":"markdown","source":"\n**Welcome to my first Kernel on Kaggle.**\n\n**Overview**\nJust the basic stuffs here. Just started practicing Data Science. Please share your feedbacks and comments. \n\n**About me:**\nLike almost everyone here in Kaggle, I am a Data Science enthusiast. Eventhough I  joined kaggle an year back, this is the first time I am making a kernel.\n\n"},{"metadata":{"trusted":true,"_uuid":"5c482ba5c4c2a115ef6034ee9ee0a3d8188fd554","_kg_hide-input":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n#importing seaborn and matplotlib for plotting\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"29f711a3b969b24559a8fe3eae0a5101fb1d8284"},"cell_type":"markdown","source":"Reading dataset into a pandas dataframe : **ds**"},{"metadata":{"trusted":true,"_uuid":"b4f0e5f4128ce4d4b9ba9418e390f8e979ef4590"},"cell_type":"code","source":"ds=pd.read_csv(\"../input/train.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f557b829e0197764cb159990777dcd98c4e9d03a"},"cell_type":"markdown","source":"This is what our data looks like"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"f73e4a47ea9c5c86a5106b79a447fe35c82fec2c"},"cell_type":"code","source":"ds.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8d88b476fc1e62c8074b15f0bc586e74a3673f26"},"cell_type":"markdown","source":"Taking a subset of original data, leaving out columns like Name,Fare etc.\nEventhough its a cardinal sin to theorize before having data and doing an analysis, my aim here is to go through the  basics in EDA. So, for time being, amleaving out some columns in analysis. But  will bring them back later. :)"},{"metadata":{"trusted":true,"_uuid":"01624699e452bbb57bce24c8cb9bce2150539f5b"},"cell_type":"code","source":"data=ds.iloc[:,[0,1,2,4,5,6,7,-1]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ed20a94c10afef7983dfcc565c2e283264c6b1db"},"cell_type":"markdown","source":"This will be the data we will be working with for the time being."},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"f4c94b4fe8cc39fcb3e998612bccd9855b57075a"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c69df12ac4527cb30918e3e6e1e1c078607a2458"},"cell_type":"markdown","source":"We are going to check whether the chance of survival from this ship wreck can be influenced by any of the features given in the dataset.\n\nLets' start with the survival ratio and the Ticket Classes"},{"metadata":{"_uuid":"f8ac2bb6c657535a1fdf2fd1f94379fa8f39b378"},"cell_type":"markdown","source":"Group the data by the columns, \"Survived\" and \"Pclass\" and take the count.\nWe only need 3 columns to get the count here. Count of rows in the column \"PassengerId\" wil give us the required count of people. \n"},{"metadata":{"trusted":true,"_uuid":"c12781711a954e7cda2782a04317371efeca245f","_kg_hide-input":true},"cell_type":"code","source":"p=data.groupby([\"Survived\",\"Pclass\"])[\"PassengerId\"].count().rename(\"Count\").reset_index()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"26fad2e52b6dfad5805967be37c175172087733e"},"cell_type":"markdown","source":"Add a new column, which shows perentage of people per class.\n\nFrom here on, we will group the dataset like we have done above to get the grouping of data based on deaired categories."},{"metadata":{"trusted":true,"_uuid":"343eb61d8d2c7cd9c6126c0ce8ab8017b1c25fe3","_kg_hide-input":true},"cell_type":"code","source":"p[\"ClassPercentage\"]=p[\"Count\"]/p.groupby(\"Pclass\")[\"Count\"].transform(\"sum\")\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4be94b7ad4e4a7e93f772d685695360d1d98a971"},"cell_type":"markdown","source":"Relationship between different attributes can be easily identified by using plots. There are large number of plots available in the toolset of a statistician. But here we will be mainly using the basic kinds,like ,Bar plot, Histogram, Pie chart ,Scatten plot etc.\n\n\nNow we are going to plot a  bar graph, showing Survival ratio wrt different Classes\n#Green bars show %Survived \n#Red bars show %Died"},{"metadata":{"trusted":true,"_uuid":"aa8146e6a7000df30778da520e6c33db76264355","_kg_hide-input":true},"cell_type":"code","source":"f=plt.figure()\nax=f.add_subplot(111)\nax.set(xlabel=\"Pclass\",ylabel=\"Percentage\",xlim=(0,4),ylim=(0,1),title=\"Class vs Survival\")\nax.set_xticks([1,2,3])\nax.set_xticklabels([\"Class 1\",\"Class 2\",\"Class 3\"])\nax.bar([.8,1.8,2.8],p[p.Survived==1][\"ClassPercentage\"],color=[\"g\"],label=\"Survived\",width=.4)\nax.bar([1.2,2.2,3.2],p[p.Survived==0][\"ClassPercentage\"],color=[\"r\"],label=\"Not Survived\",width=.4)\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b28a58ace1e7e172c3aceee3130fc5d2be785883"},"cell_type":"markdown","source":"Now, we are going to see the survival chance vs the Gender."},{"metadata":{"trusted":true,"_uuid":"778f3af83dcd28b35555e55ded1edc637bc70dec","_kg_hide-input":true},"cell_type":"code","source":"p=data.groupby([\"Survived\",\"Sex\"])[\"PassengerId\"].count().rename(\"Count\").reset_index()\np[\"%bySex\"]=p.Count*100/(p.groupby(\"Sex\")[\"Count\"].transform(\"sum\"))\np","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"af82146c2ea97546d79b42dc634a210e64d46703"},"cell_type":"markdown","source":"\npie chart showing Survival ratio wrt different Gender\n#Green wedge  shows %Survived \n#Red  wedge shows %Died"},{"metadata":{"trusted":true,"_uuid":"23ea3da9d2975f2ea2aa37c0302827368bd841d8","_kg_hide-input":true},"cell_type":"code","source":"f,ax=plt.subplots(1,2,figsize=(8,6))\nax[0].set(title=\"Female\")\nax[0].pie(p[p.Sex==\"female\"][\"%bySex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\nax[1].set(title=\"Male\")\nax[1].pie(p[p.Sex==\"male\"][\"%bySex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2d5a46d336fac8f92fca0e3a81637c17a7fe1ae2"},"cell_type":"markdown","source":"Now, we are going to dig little more deeper. We will see how the Survival ratio wrt Gender varies among different Ticket Classes."},{"metadata":{"trusted":true,"_uuid":"f1ea436fb4af6cbe7ea1ec609e216abea633b985","_kg_hide-input":true},"cell_type":"code","source":"p=data.groupby([\"Survived\",\"Pclass\",\"Sex\"])[\"PassengerId\"].count().rename(\"Count\").reset_index()\np[\"%byClass_Sex\"]=p[\"Count\"]/p.groupby([\"Pclass\",\"Sex\"])[\"Count\"].transform(\"sum\")\np","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d22eab88432b9a00704100fde11c96fd72b9c482"},"cell_type":"markdown","source":"pie chart showing Survival Ratio vs Gender among different classes\n#Green wedge  shows %Survived \n#Red  wedge shows %Died"},{"metadata":{"trusted":true,"_uuid":"b5d7b59258054dbbf08cbcb207e91e0ae18031b2","_kg_hide-input":true},"cell_type":"code","source":"with plt.style.context(\"ggplot\"):\n    f,ax=plt.subplots(3,2,figsize=(8,18))\n\n    ax[0,0].set(title=\"Female\")\n    ax[0,0].pie(p[(p.Sex==\"female\")&(p.Pclass==1)][\"%byClass_Sex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\n    ax[0,1].set(title=\"Male\")\n    ax[0,1].pie(p[(p.Sex==\"male\")&(p.Pclass==1)][\"%byClass_Sex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\n    ax[0,0].set_ylabel(\"Class 1\")\n\n    ax[1,0].set(title=\"Female\")\n    ax[1,0].pie(p[(p.Sex==\"female\")&(p.Pclass==2)][\"%byClass_Sex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\n    ax[1,1].set(title=\"Male\")\n    ax[1,1].pie(p[(p.Sex==\"male\")&(p.Pclass==2)][\"%byClass_Sex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\n    ax[1,0].set_ylabel(\"Class 2\")\n\n    ax[2,0].set(title=\"Female\")\n    ax[2,0].pie(p[(p.Sex==\"female\")&(p.Pclass==3)][\"%byClass_Sex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\n    ax[2,1].set(title=\"Male\")\n    ax[2,1].pie(p[(p.Sex==\"male\")&(p.Pclass==3)][\"%byClass_Sex\"],colors=[\"r\",\"g\"],labels=p[\"Survived\"].unique(),autopct=\"%1.1f%%\",)\n    ax[2,0].set_ylabel(\"Class 3\")\n\n    plt.legend()\n    plt.suptitle(\"Survival Ratio vs Gender among different classes\")\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e69d3ee1e37cfb1e94e7445cfa1fd0e817eaeca4"},"cell_type":"markdown","source":"Eventhough the chance of survival rarely depends on the station embarked, we can't leave anything to chance. \nSo, lets see wether there is any relation between  the chance of survival and the embarked station"},{"metadata":{"trusted":true,"_uuid":"9e2cef602711cc6fe8e19e55228187dc04658bd4","_kg_hide-input":true},"cell_type":"code","source":"p=data.groupby([\"Survived\",\"Embarked\"])[\"PassengerId\"].count().rename(\"Count\").reset_index()\np[\"Emb%\"]=p[\"Count\"]/p.groupby(\"Embarked\")[\"Count\"].transform(\"sum\")\np","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"988aea450edbff4ae26b465371c90461143c289a"},"cell_type":"markdown","source":"Red bars depict the no. of people died and Green bar shows no. of people survived among different stations embarked"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"febdace3993eaa6c4e81ad8553c7bfd1a6c5cd09"},"cell_type":"code","source":"fig=plt.figure(figsize=(8,6))\nax=fig.add_subplot(111)\nax.bar([.8,1.8,2.8],list(p.loc[p[\"Survived\"]==0][\"Count\"]),width=.4,label=\"died\",color=\"r\")\nax.bar([1.2,2.2,3.2],list(p.loc[p[\"Survived\"]==1][\"Count\"]),width=.4,label=\"Survived\",color=\"g\")\nax.set_xticks([1,2,3])\nax.set_xticklabels([\"Cherbourg\",\"Queenstown\",\"Southampton\"])\nplt.legend()\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5b1864b5af6b72cc70b3489fb56c7ba94f61a2a8"},"cell_type":"markdown","source":"We will now again categorize the  above data according to different Ticket Class and plot the count as-well-as the percentage against the chance of survival."},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"819e632f7ab80dbcf5a11b02d5e8286a6dc0bb98"},"cell_type":"code","source":"p=data.groupby([\"Embarked\",\"Pclass\",\"Survived\"]).count()\np[\"ClassEmb%\"]=p[\"PassengerId\"]*100/p.groupby([\"Embarked\",\"Pclass\"])[\"PassengerId\"].transform(\"sum\")\np[\"Emb%\"]=p[\"PassengerId\"]*100/p.groupby([\"Embarked\"])[\"PassengerId\"].transform(\"sum\")\np.rename(columns={\"PassengerId\":\"Count\"},inplace=True)\np","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0dabcfe9d55acdca507fe86aab3f604218a953ec"},"cell_type":"markdown","source":"Below 3 plots show the Survival Count vs Pclass among  Embarkments and there is a pie chart depiction on side of each graphs to show the survival ratio within the classes"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"25267ba031e7a68ad5d70abb7f0a780458893064"},"cell_type":"code","source":"with plt.style.context(\"ggplot\"):\n    f=plt.figure(figsize=(18,12))\n    g=gridspec.GridSpec(3,3)\n    plt.suptitle(\"Survival Count vs Pclass among diff Embarkment\")\n  \n    #Row 1\n    ax0=plt.subplot(g[:3,:-2])\n    ax0.set(title=\"Cherbourg\")\n    ax0.set_xticks([1,2,3])\n    ax0.set_xticklabels([\"Class 1\",\"Class 2\",\"Class 3\"])\n    ax0.set_yticks(np.arange(0,300,40))\n    ax0.set_ylabel(\"Count\")\n    ax0.set_xlabel(\"Pclass\")\n    ax0.bar([.8,1.8,2.8],p.xs([\"C\",0],level=[0,2])[\"Count\"],width=.4,label=\"died\",color=\"r\")\n    ax0.bar([1.2,2.2,3.2],p.xs([\"C\",1],level=[0,2])[\"Count\"],width=.4,label=\"Survived\",color=\"g\")\n    ax0.legend()\n    \n    ax01=plt.subplot(g[0,-1])\n    ax01.set_title(\"Class 1\")\n    ax01.pie(p.xs([\"C\",1],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    ax02=plt.subplot(g[1,-1])\n    ax02.set_title(\"Class 2\")\n    ax02.pie(p.xs([\"C\",2],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    ax03=plt.subplot(g[2,-1])\n    ax03.set_title(\"Class 3\")\n    ax03.pie(p.xs([\"C\",3],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    plt.tight_layout()\n    plt.show()\n    \n    \n    \n   \n   ","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"1ca18f9e22c4c5ec0ee9c358d72643f3ba258b30"},"cell_type":"code","source":"#Row 2\nwith plt.style.context(\"ggplot\"):\n    f1=plt.figure(figsize=(18,12))\n    g=gridspec.GridSpec(3,3)\n    plt.suptitle(\"Survival Count vs Pclass among diff Embarkment\")\n   \n    ax1=plt.subplot(g[:3,:-2])\n    ax1.set(title=\"Queenstown\")\n    ax1.set_xticks([1,2,3])\n    ax1.set_xticklabels([\"Class 1\",\"Class 2\",\"Class 3\"])\n    ax1.set_yticks(np.arange(0,300,40))\n    ax1.set_ylabel(\"Count\")\n    ax1.set_xlabel(\"Pclass\")\n    ax1.bar([.8,1.8,2.8],p.xs([\"Q\",0],level=[0,2])[\"Count\"],width=.4,label=\"died\",color=\"r\")\n    ax1.bar([1.2,2.2,3.2],p.xs([\"Q\",1],level=[0,2])[\"Count\"],width=.4,label=\"Survived\",color=\"g\")\n    ax1.legend()\n    \n    ax11=plt.subplot(g[0,-1])\n    ax11.set_title(\"Class 1\")\n    ax11.pie(p.xs([\"Q\",1],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    ax12=plt.subplot(g[1,-1])\n    ax12.set_title(\"Class 2\")\n    ax12.pie(p.xs([\"Q\",2],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    ax13=plt.subplot(g[2,-1])\n    ax13.set_title(\"Class 3\")\n    ax13.pie(p.xs([\"Q\",3],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    plt.tight_layout()\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"dda154a425202d97aa275fed905462e7d43a8b7f"},"cell_type":"code","source":" #Row 3\nwith plt.style.context(\"ggplot\"):    \n    f2=plt.figure(figsize=(18,12))\n    g=gridspec.GridSpec(3,3)\n    plt.suptitle(\"Survival Count vs Pclass among diff Embarkment\")\n    \n    \n    ax2=plt.subplot(g[:3,:-2])\n    ax2.set(title=\"Southampton\")\n    ax2.set_xticks([1,2,3])\n    ax2.set_xticklabels([\"Class 1\",\"Class 2\",\"Class 3\"])\n    ax2.set_yticks(np.arange(0,300,40))\n    ax2.set_ylabel(\"Count\")\n    ax2.set_xlabel(\"Pclass\")\n    ax2.bar([.8,1.8,2.8],p.xs([\"S\",0],level=[0,2])[\"Count\"],width=.4,label=\"died\",color=\"r\")\n    ax2.bar([1.2,2.2,3.2],p.xs([\"S\",1],level=[0,2])[\"Count\"],width=.4,label=\"Survived\",color=\"g\")\n\n    ax2.legend()\n    \n    \n    ax21=plt.subplot(g[0,-1])\n    ax21.set_title(\"Class 1\")\n    ax21.pie(p.xs([\"S\",1],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    ax22=plt.subplot(g[1,-1])\n    ax22.set_title(\"Class 2\")\n    ax22.pie(p.xs([\"S\",2],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    ax23=plt.subplot(g[2,-1])\n    ax23.set_title(\"Class 3\")\n    ax23.pie(p.xs([\"S\",3],level=[0,1])[\"ClassEmb%\"],colors=[\"r\",\"g\"],labels=[\"Died\",\"Survived\"],autopct=\"%1.1f%%\")\n    plt.tight_layout()\n    plt.show()   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e4d0cad0a92c45477728d293b9b997ffd76d8c23"},"cell_type":"markdown","source":"We will now see, the different age groups and the which age group where prominant among survived and what not."},{"metadata":{"trusted":true,"_uuid":"f6309a3c5c794b954d1b07154f51fa70c841a037"},"cell_type":"code","source":"data[\"Age\"].fillna(-50).plot.hist(bins=np.arange(0,90,5),figsize=(8,6),density=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d826ac8378f1ea0a45b0e77d180b514f2254e011"},"cell_type":"code","source":"data.loc[data[\"Survived\"]==1][\"Age\"].reset_index()[\"Age\"].fillna(-50).plot.hist(histtype=\"bar\",density=True,color=\"g\", bins=np.arange(0,90,5),figsize=(8,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3362fc5ca2443b521a5461ad485deab0bd7d06e2"},"cell_type":"code","source":"data.loc[data[\"Survived\"]==0][\"Age\"].reset_index()[\"Age\"].fillna(-50).plot.hist(histtype=\"bar\",density=True,color=\"r\", bins=np.arange(0,90,5),figsize=(8,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f7bd33e547fb59b96a43182977cce1b91d6dff39"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}