{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('darkgrid')\n%matplotlib inline\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b4bfbec3adfa938ac87e5f9ed36e5f8d52758ee"},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"Everyone knows the story of Titanic, the british ship which sank in the North Atlantic ocean in 1912. And probably anyone reading this kernel will have seen the movie at least once. I think all of us sympathized with the 3rd class passenegers, with the parents who did everything to save their kids and with the parents who couldn't. The impression we took from the movie was that the 3rd class passengers were locked-out on purpose to be able to rescue the 1st class passengers, which may not be particularly accurate, but it holds some truth in that most of the 3rd class passengers perished in that event.  \n\nLet's explore the stories of the people who were on that ship. \n\nThe ships consisted of 3 passenger facilities, where the 1st class aimed to meet the highest standards of luxury. It was designed in style similar to the Ritz hotel. Try to imagine the 1st class facilites with this description:  \n\n \"Among the more novel features available to first-class passengers was a 7 ft. deep saltwater swimming pool, a gymnasium, a squash court, and a Turkish bath which comprised electric bath, steam room, cool room, massage room, and hot room. First-class common rooms were impressive in scope and lavishly decorated. They included a Lounge in the style of the Palace of Versailles, an enormous Reception Room, a men's Smoking Room, and a Reading and Writing Room. There was an À la Carte Restaurant in the style of the Ritz Hotel which was run as a concession by the famous Italian restaurateur Gaspare Gatti.\"  \n\nThe 1st class facilities were basically heaven on earth. While the 2nd and 3rd class facilities weren't comparable to the 1st class ones, they were significantly better than most of the ships in that time.\n\nLet's see how many people belonged to each class."},{"metadata":{"trusted":true,"_uuid":"b6105fea984de400e84768ff09aac240448abbeb"},"cell_type":"code","source":"train['Pclass'].value_counts().sort_index().plot(kind='bar', title='Counts of passengers in each class')\n# The 3rd class are the major class in our dataset","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"849e6891ac71d29188a6fa818d6b24f0de6abff7"},"cell_type":"raw","source":"How about the ages of the people in the ship overall? In other words, what is the age distriburion?"},{"metadata":{"trusted":true,"_uuid":"e36a722d08cabd628c4900155f96581d6c88bc54"},"cell_type":"code","source":"train['Age'].plot(kind='hist', title='Age distribution')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5aa08b29266ff30b0011762bb1f58d4f403aef49"},"cell_type":"raw","source":"In order to make use of the age feature and see it's interactions with other features further in the analysis, it is better to make age bins in order to easily deal with the feature."},{"metadata":{"trusted":true,"_uuid":"e4c090c57e841059056f457fe08147a725fdc6e1"},"cell_type":"code","source":"ages = [0, 14, 30, 60, 80] # My choice for bins was arbitrary \nbins = ['Children', 'Youth', 'Adults', 'Elderly']\ntrain['age_bin'] = pd.cut(train.Age, ages, labels=bins)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b1c7e0fa823af14849b24e1c8e3183128c7fd91"},"cell_type":"code","source":"# Now let's check the distribution\ntrain['age_bin'].value_counts().plot(kind='barh', title='Passengers by Age bins')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"620b47efd14346a3b4d1a52bf3adf242dfba0416"},"cell_type":"raw","source":"How many males and females were on the ship?"},{"metadata":{"trusted":true,"_uuid":"45b8af6322c0932cb8ae68aeeeea4418083d7a67"},"cell_type":"code","source":"train['Sex'].value_counts().plot(kind='barh', title='Passengers by Sex')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6983ced3397a78ea222e36b44fda232c74e7e886"},"cell_type":"markdown","source":"We have two features which are related to family members:\n1. SibSp: no. of siblings/spouses\n2. Patch: no. of parents/children\n\nLet's look into them."},{"metadata":{"trusted":true,"_uuid":"a53b622bd3af39b463da2fa7e4125463c2c94dce"},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(16,5))\ntrain['SibSp'].value_counts().plot(kind='bar', ax=axes[0], title='Siblings/Spouse')\ntrain['Parch'].value_counts().plot(kind='bar', title='Parents/Children', ax=axes[1])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e0719603ff4909917332621579e48f5f68582cb2"},"cell_type":"markdown","source":"There are several features which could be engineered from these two features, for example:\n\n\n1. marital status\n2. no. of children aboard (for married passengers)\n3. no. of parents aboard (for children)\n4. total no. of familiy members aboard\n5. no. of siblings aboard\n6. Is the passenger alone?\n\nOf course not all features would be useful, but we'll get to that part later when training the model"},{"metadata":{"trusted":true,"_uuid":"ad49a335de9f289218f97263590449d5b35ed8e9"},"cell_type":"code","source":"is_married = (train.Age > 16) & (train.SibSp == 1) # This feature maybe noisy as it some passengers\n                                                  # with siblings may slip in, and also maybe some\n                                                  # married passengers have siblings aboard too\n                                                  # so it needs to be tuned while training the model\nis_kid = train.Age < 14\nis_alone = (train.SibSp == 0) & (train.Parch == 0)\n\ntrain['is_married'] = np.where(is_married, 1, 0)\ntrain['parents_aboard'] = np.where(is_kid, train.Parch, 0)\ntrain['kids_aboard'] = np.where(is_married, train.Parch, 0)\ntrain['family_aboard'] = train['SibSp'] + train['Parch']\ntrain['sibs_aboard'] = np.where(is_kid, train.SibSp, 0)\ntrain['is_alone'] = np.where(is_alone, 1, 0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b4050047d6ad605a24a682c830665aa48a344bc3"},"cell_type":"markdown","source":"Now let's explore more univariate plots, and also bivariate relationships between the features we have and the target. Here are some questions:\n\n1. Which age group had more chances of survival?\n2. Which sex had more chances of survival?\n3. Does different classes have different chances of survival between different age groups and sexes?\n4. How many passengers in each class were married? and how did that affect their survival?\n5. Is a child's survival affected by the presence or absence of this parents? \n6. How many passengers in each class were totally alone? and how did that fare for their survival?\n7. Were parents more likely to survive with their kids if they weren't in the 3rd class?"},{"metadata":{"trusted":true,"_uuid":"69df9a8bb06a05ea3bea0e8385159a15b8036a3d"},"cell_type":"code","source":"# which age group had more chances of survival?\nsns.catplot(x='age_bin', y='count', hue='Survived', data=train.groupby([\n                                                            'age_bin', 'Survived'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar')\n\n# The children were the group with higher chances of survival. Adults also had better chances compared to\n# youth, but I guess we can see better if we take sex into consideration","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41ea1d7356081d1a6def83c3e8b4ec6c1cf6d5e1"},"cell_type":"code","source":"sns.catplot(x='age_bin', y='count', hue='Survived', col='Sex', data=train.groupby([\n                                                            'age_bin', 'Survived', 'Sex'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar')\n\n# Now these two plot tell a very different stroy. Youth and adults in males only had lower chances of \n# survival, on the other hand females fared better in all ages, and that makes sence, as the rule in\n# these situations is to save women and children first.\n# Now we answered the first two questions, where we found out that females had much higher chances of\n# Survival compared to males, and all male age groups had pretty low chances of survival.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f32314822f92b5141eff936484b66d2a8b132a0"},"cell_type":"code","source":"# Does different classes have different chances of survival between different age groups and sexes?\n# Will our previous inferences about sex and age survival hold when we segregate the classes?\n\nsns.catplot(x='age_bin', y='count', hue='Survived', col='Sex', row='Pclass', data=train.groupby([\n                                                            'age_bin', 'Survived', 'Sex', 'Pclass'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar',\n                                                            sharex=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a1dad959a39ea3ad1b4485b2e5f9b1a59f9fc7c"},"cell_type":"markdown","source":"We can see that the inferences can have several faces based on the class, for example:\n1. 1st class males have higher chances of survival compared to males in the other classes\n2. 3rd class females have lower chances of survival compared to females of other classes"},{"metadata":{"trusted":true,"_uuid":"84f5ebad83ab46b1657c7d9336691fc439a8b3e0"},"cell_type":"code","source":"# How many passengers in each class were married? and how did that affect their survival?\nsns.catplot(x='is_married', y='count', hue='Survived', col='Pclass', data=train.groupby([\n                                                            'is_married', 'Survived', 'Pclass'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar',\n                                                            sharex=False)\n\n# Married people are more present in the 1st and 2nd classes, and married people in the 3rd class had lower\n# chances of survival compared to other classes.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf224fd26a554bfca15e8e740659f430166c247b"},"cell_type":"code","source":"# Is a child's survival affected by the presence or absence of this parents?\nsns.catplot(x='parents_aboard', y='count', hue='Survived', data=train[is_kid].groupby([\n                                                            'parents_aboard', 'Survived'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar',\n                                                            sharex=False)\n\n# kids with only one parent had higher chances of survival, but I really can't make sense of why that\n# may happen. Let's look at this plot segregatted by classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0e20ca83f044e09ce1bb93e6cb99eb81241a15f"},"cell_type":"code","source":"sns.catplot(x='parents_aboard', y='count', hue='Survived', col='Pclass', data=train[is_kid].groupby([\n                                                            'parents_aboard', 'Survived', 'Pclass'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar',\n                                                            sharex=False)\n\n# Now it kind of makes sense. In the 1st class there aren't much kids to begin with. In the 2nd class, most\n# kids have only 1 parent, and even those with 2 parents also survived. In the 3rd class, some kids didn't\n# have their parents with them, and they had higher chances of survival, while kids with 1 or 2 parents \n# had lower chances. It might be that kids that had parents refused to leave their parents.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef42059bb9de675773487e936d32d7cfa2666cd9"},"cell_type":"code","source":"# How many passengers in each class were totally alone? and how did that fare for their survival?\nsns.catplot(x='is_alone', y='count', hue='Survived', col='Pclass', data=train.groupby([\n                                                            'is_alone', 'Survived', 'Pclass'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar',\n                                                            sharex=False)\n\n# Being alone in the 3rd class significantly reduces a passengers chances of survival. And also we can see\n# that most passengers in the 3rd class were alone. I guess they were also youth, which makes sense why\n# they didn't have any family aboard, just like Jack in the movie.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9896b2d109130f0a12a23d4beffaa0b3eb600210"},"cell_type":"code","source":"# Were parents more likely to survive with their kids if they weren't in the 3rd class?\n# My answer is yes before we even plot, but let's see\nsns.catplot(x='kids_aboard', y='count', hue='Survived', col='Pclass', data=train[(is_married) & \n                                                            (train.kids_aboard > 0)].groupby([\n                                                            'kids_aboard', 'Survived', 'Pclass'])[\n                                                            'PassengerId'].count().reset_index().rename(\n                                                            columns={'PassengerId':'count'}), kind='bar',\n                                                            sharex=False)\n\n# We can infer from this that parents in the 1st class also had higher chances of survival compared to the\n# 2nd and 3rs class parents.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"67b894f8516eaaa00fb54aa95dac1803f8d22575"},"cell_type":"markdown","source":"There is yet more features to make using only the features we explored and the ones we made, including different variations of mean encodings. We will explore them in the next time.   \n Also we'll explore other features and see how we engineer them into more useful features.  \n\nIf you liked this kernel upvote, and if you have any comments please feel free to share them. This is my first kernel and I really appreciate any feedback."}],"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}