{"cells":[{"metadata":{"_uuid":"cfb17a457b3019239db43539974e622a57d95ae1"},"cell_type":"markdown","source":"# Titanic survival prediction from Name and Sex\nThis notebook is inspired by [Simple Titanic model using only Name](https://www.kaggle.com/cdeotte/titanic-using-name-only-0-81818). I take a similar approach and calculate the survival rate of the woman and children in a family with two primary differences.\n\nFirst, I adjust the `WCSurvivedPct` on a per individual basis so that it represents the survival rate of the rest of the family, not including the current individual.\n\nSecond, I use a `DecisionTreeClassifier` to create slightly more complex classification rules instead of the two rules from Chris's notebook *1)all males die except boys in families where all woman and children survive, 2) all females live except for females in families where all woman and children die*.\n\nThese changes result in an improvement from 0.82296 to 0.83253."},{"metadata":{"scrolled":true,"trusted":true,"_uuid":"893188eebb608a0e1e5b79a5c29d9d2870ad244a"},"cell_type":"code","source":"import graphviz \nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom sklearn import tree\nfrom sklearn.model_selection import cross_val_score, GridSearchCV","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1323768168e8e4954689a438b675824b603434de"},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv').set_index('PassengerId')\ntest = pd.read_csv('../input/test.csv').set_index('PassengerId')\ndf = pd.concat([train, test], axis=0, sort=False)\ndf['Title'] = df.Name.str.split(',').str[1].str.split('.').str[0].str.strip()\ndf['IsWomanOrChild'] = ((df.Title == 'Master') | (df.Sex == 'female'))\ndf['LastName'] = df.Name.str.split(',').str[0]\n\nfamily = df.groupby(df.LastName).Survived\ndf['FamilyTotalCount'] = family.transform(lambda s: s[df.IsWomanOrChild].fillna(0).count())\ndf['FamilyTotalCount'] = df.mask(df.IsWomanOrChild, df.FamilyTotalCount - 1, axis=0)\ndf['FamilySurvivedCount'] = family.transform(lambda s: s[df.IsWomanOrChild].fillna(0).sum())\ndf['FamilySurvivedCount'] = df.mask(df.IsWomanOrChild, df.FamilySurvivedCount - df.Survived.fillna(0), axis=0)\ndf['FamilySurvivalRate'] = (df.FamilySurvivedCount / df.FamilyTotalCount.replace(0, np.nan))\ndf['IsSingleTraveler'] = df.FamilyTotalCount == 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0e24207c2141a2dc4dd7734b0941f3cd057d7ad"},"cell_type":"code","source":"x = pd.concat([\n    df.FamilySurvivalRate.fillna(0),\n    df.IsSingleTraveler,\n    df.Sex.replace({'male': 0, 'female': 1}),\n], axis=1)\ntrain_x, test_x = x.loc[train.index], x.loc[test.index]\ntrain_y = df.Survived.loc[train.index]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"759439497d0c4072e1b9cc63938564186e7088ab"},"cell_type":"code","source":"clf = tree.DecisionTreeClassifier()\ngrid = GridSearchCV(clf, cv=5, param_grid={\n    'criterion': ['gini', 'entropy'], \n    'max_depth': [2, 3, 4, 5]})\ngrid.fit(train_x, train_y)\ngrid.best_params_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a5df0d091bc1553ad0a2bc3634b169f677eb668"},"cell_type":"code","source":"model = grid.best_estimator_","execution_count":null,"outputs":[]},{"metadata":{"scrolled":false,"trusted":true,"_uuid":"f45495fcb7ec2f1df98db247e40ffb2b3d10aba5"},"cell_type":"code","source":"graphviz.Source(tree.export_graphviz(model, feature_names=x.columns)) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cac645bd1c3a3d1bc5ad26a7642ed3f7545b6f03"},"cell_type":"code","source":"test_y = model.predict(test_x).astype(int)\npd.DataFrame({'Survived': test_y}, index=test.index) \\\n.reset_index() \\\n.to_csv(f'survived.csv', index=False)","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}