{"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 scipy as sp\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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.\n\ntitanic = pd.read_csv(\"../input/train.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"Before anything else, explore the data a little bit - get a sense of what it contains."},{"metadata":{"trusted":true,"_uuid":"3703a30043b9b62fd1d0f3409dc65972bcdb88bb"},"cell_type":"code","source":"titanic","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e02330e1d777c88db14d2e3c53e5d510985bc521"},"cell_type":"markdown","source":"Let's do some data mangling. To get numeric features, we will want to do **one-hot encodings** of several variables. We might also want to extract the first letter in the cabin as a marker for where on the ship a passenger may have started."},{"metadata":{"trusted":true,"_uuid":"978d6356210934313fd3c54a8ae8064ff5c10d46"},"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\nfrom sklearn.linear_model import LogisticRegression\nohe = OneHotEncoder()\n\ntitanic = pd.read_csv(\"../input/train.csv\")\n\ntitanic[\"CabinGroup\"] = titanic[\"Cabin\"].fillna(\"\").str[:1]\n\ny_train = titanic[\"Survived\"].values\nX_numeric = titanic[[\"Age\", \"Fare\"]].fillna(0).values\nX_categorical = ohe.fit_transform(titanic[[\"Pclass\", \"Sex\", \"Embarked\", \"CabinGroup\"]].fillna(\"\").values)\nX_train = sp.hstack([X_numeric,X_categorical.todense()])\n\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\nmodel.score(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4cd15b144526c4c2cc1d9f9540cad6f18845cbd1"},"cell_type":"markdown","source":"Accuracy of ~80% isn't too shabby for a first shot. This number is going to be inflated: because it uses the training data to evaluate the model. We really should have split off a test set to get a better value here.\n\nNow, let's submit this to Kaggle and see where it lands!"},{"metadata":{"trusted":true,"_uuid":"b8b588e8c355669b46c301f5b715b44545d24410"},"cell_type":"code","source":"titanic = pd.read_csv(\"../input/test.csv\")\ntitanic[\"CabinGroup\"] = titanic[\"Cabin\"].fillna(\"\").str[:1]\nX_numeric = titanic[[\"Age\", \"Fare\"]].fillna(0).values\nX_categorical = ohe.transform(titanic[[\"Pclass\", \"Sex\", \"Embarked\", \"CabinGroup\"]].fillna(\"\").values)\nX_test = sp.hstack([X_numeric,X_categorical.todense()])\n\ny_pred = model.predict(X_test)\n\n\nsubmission = pd.DataFrame({\"PassengerId\": titanic[\"PassengerId\"],\n                           \"Survived\": y_pred})\nsubmission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97d8454b42ca01c52061269034b1411e6cf53b88"},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}