{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# titanicdeath.com\n", "\n", "This is the writeup for titanicdeath.com. Besides having a truly fantastic name, this little website I created tells you whether or not you would be likely to die on the titanic provided some small amount of personal information.\n", "\n", "The first question you are probably asking is: why did i die? or why did i live?\n", "\n", "The biggest determining factor in whether you lived or died is your gender. Females were much more likely to survive the sinking of the titanic than men. If you have ever heard the phrase 'women and children first' you will have an intuitive understanding of why this is.\n", "\n", "The next bigest determinant of survival is what class you travelled in; passengers in first class were more likely to survive. This makes some sense; often times first class passengers receive benefits. One of the benefits on a sinking ship may have been easier access to lifeboats. Third class passengers were below deck and may not have had the means to escape.\n", "\n", "Age is another important factor, the younger you are the more likely it is that you survived. It seems young people and children had some level of priority on lifeboats.\n", "\n", "Whether you were traveling alone or with others on the titanic helps to deterimine whether you passed away tragically or miraculously survived. This is why titanicdeath.com asks if you are married or have siblings. Travellers who were alone on the titanic or travelling with large numbers of relatives were more likely to have died. Travellers who were alone may have had to wait or may not have had lower priority (compared with women and children) on lifeboats. Many large families were in third class where death rates were high.\n", "\n", "If you'd like a little more evidence proceed below.\n", "## I want to try to survive the titanic again." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#let us load the logistic regression model used\n", "import pandas as pd\n", "data_df = pd.read_csv('../titanicdeath/static/train.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The 'sex' input has the biggest effect on survival.\n", "\n", "You can see below the survival rates for women are 75% but for men are only about 19%." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SexSurvived
1male0.188908
0female0.742038
\n", "
" ], "text/plain": [ " Sex Survived\n", "1 male 0.188908\n", "0 female 0.742038" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_df[['Sex', 'Survived']].groupby(['Sex'], as_index=False).mean().sort_values(by='Survived')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The 'class' input has the next biggest effect on survival.\n", "\n", "In our data the class of a passenger has the name 'Pclass.' You can see here below that the survival rates ase highest for first class - 63%, second class - 47%, third class - 24%." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PclassSurvived
010.629630
120.472826
230.242363
\n", "
" ], "text/plain": [ " Pclass Survived\n", "0 1 0.629630\n", "1 2 0.472826\n", "2 3 0.242363" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean().sort_values(by='Survived', ascending=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The age input affects your chances of survival as well.\n", "\n", "People from ages 0 - 16 have the highest chance of survival. Everyone older than that has lowered chances of surviving the sinking of the titanic." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
age_groupSurvived
0(0.34, 16.336]0.550000
1(16.336, 32.252]0.369942
2(32.252, 48.168]0.404255
3(48.168, 64.084]0.434783
4(64.084, 80]0.090909
\n", "
" ], "text/plain": [ " age_group Survived\n", "0 (0.34, 16.336] 0.550000\n", "1 (16.336, 32.252] 0.369942\n", "2 (32.252, 48.168] 0.404255\n", "3 (48.168, 64.084] 0.434783\n", "4 (64.084, 80] 0.090909" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_df['age_group'] = pd.cut(data_df['Age'], 5)\n", "data_df[['age_group', 'Survived']].groupby(['age_group'], as_index=False).mean().sort_values(by='age_group', ascending=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Whether you are travelling alone or with family can affect your chances of survival.\n", "\n", "In our data the number of people you are travelling with is reprsented by the 'companions' column. If you travelled with 1,2, or 3 companions your chances of survival were decent (roughly 50-70%). If you travelled alone your chances of survival went down significantly to 30%. If you travelled with more than 5 people your chances were just as bad or worse. " ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
companionsSurvived
330.724138
220.578431
110.552795
660.333333
000.303538
440.200000
550.136364
770.000000
8100.000000
\n", "
" ], "text/plain": [ " companions Survived\n", "3 3 0.724138\n", "2 2 0.578431\n", "1 1 0.552795\n", "6 6 0.333333\n", "0 0 0.303538\n", "4 4 0.200000\n", "5 5 0.136364\n", "7 7 0.000000\n", "8 10 0.000000" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_df['companions'] = data_df['SibSp'] + data_df['Parch']\n", "data_df[['companions', 'Survived']].groupby(['companions'], as_index=False).mean().sort_values(by='Survived', ascending=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the next section we will load a model (logistic regression) that I have taught how to take an individual passenger's information and give me the probability of survival." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from sklearn.externals import joblib\n", "logreg = joblib.load('../titanicdeath/static/logreg.pkl')\n", "age = 2\n", "fare = 0\n", "embarkation = 2\n", "title = 1\n", "is_alone = 1\n", "age_class = 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This next bit of code takes in a passenger's input values and tells us what probability that user has of living and dying. the number shows our probability of survival. 7.8% here if we are a man (sex=0) and in third class (passenger_class=3)." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.078280156772079126" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "passenger_class = 3 \n", "sex = 0 \n", "\n", "passenger_input = pd.DataFrame([[passenger_class, sex, age, fare, embarkation, title, is_alone, age_class]])\n", "pred = logreg.predict_proba(passenger_input)\n", "pred[0][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we change the sex from male to female (sex=1) we see a huge improvement in our chances of survival (now 43%)." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.43427750040904095" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "passenger_class = 3 \n", "sex = 1\n", "\n", "passenger_input = pd.DataFrame([[passenger_class, sex, age, fare, embarkation, title, is_alone, age_class]])\n", "pred = logreg.predict_proba(passenger_input)\n", "pred[0][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we change the class from third class to first class (passenger_class=1) we improve odds of suvival even further to 77%." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.77444683956853377" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "passenger_class = 1 \n", "sex = 1\n", "\n", "passenger_input = pd.DataFrame([[passenger_class, sex, age, fare, embarkation, title, is_alone, age_class]])\n", "pred = logreg.predict_proba(passenger_input)\n", "pred[0][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This was intended to be a simple exploration of this titanic dataset. If you would like to explore this data yourself there is a really nice tutorial here:\n", "\n", "https://www.kaggle.com/c/titanic\n", "\n", "https://www.kaggle.com/startupsci/titanic/titanic-data-science-solutions\n", "\n", "If you are interested in machine learning and a very good overview of how an algorithm like logistic regression works i highly recommend you check out the first 3 lectures of andrew ng's coursera course:\n", "\n", "https://www.coursera.org/learn/machine-learning\n", "\n", "You can check out some other stuff I have done here:\n", "\n", "http://yvanscher.com/" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }