{"cells":[{"metadata":{"_uuid":"0fec343b0df2714ac8e136cc12d4f8738e98ca2c","_cell_guid":"aa834426-8858-44d7-8225-1cdcae6b3d13"},"cell_type":"markdown","source":"**Importing  necessary libraries**"},{"metadata":{"_uuid":"7cf961bb99ebd5f2a36f63e125730e5e30129191","_cell_guid":"6ef27fa1-8837-4afb-9f0f-aa65af41139b","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sbn\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e8c0a5fded711e0c0dbff4ee34adfd32314305a0","_cell_guid":"a4bfc4e4-7e60-44db-bd07-09a969c1ba10","trusted":true},"cell_type":"code","source":"dt_train = pd.read_csv(\"../input/train.csv\")\ndt_test = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1528151dfa2e58dbf805afbc26cf0365fdb9f667","_cell_guid":"48b22774-b674-427f-acc8-62806c951a99","trusted":true},"cell_type":"code","source":"dt_train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bb7f62297a53c58828fb47d97667de5706b03bfd","_cell_guid":"300ff3fb-97ad-4391-86d7-0f1262249027","trusted":true},"cell_type":"code","source":"dt_train.info()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7ddf31a84a6d755b29ebf9c8cf09fd44193d8c21","_cell_guid":"9d4bfbfd-2da4-4152-bdae-8d922cc7786a","trusted":true},"cell_type":"code","source":"dt_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cf26bea8a6611330f4f7417fb4028ea86dab4d40","_cell_guid":"c47f62b4-d749-4ccd-8e0b-06b23476db60"},"cell_type":"markdown","source":"Biding the datasets together and then looking for Null values:"},{"metadata":{"_kg_hide-output":false,"_kg_hide-input":false,"_uuid":"9f1082d2e4e44f4b7afb92f556f1b2ef93576f0a","_cell_guid":"484ef441-23ed-4bb9-9b83-17fc41a9229a","trusted":true},"cell_type":"code","source":"#Survived = dt_train['Survived']\n#dt_train.drop('Survived', axis=1, inplace=True)\ndt = pd.concat([dt_train.drop('Survived', axis=1), dt_test])\ndt.info()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7fa003cbc400828ee1f1bee7458501af85afbca0","_cell_guid":"d3c21307-409d-4ac0-8614-e44eccfc362e","trusted":true},"cell_type":"code","source":"plt.figure(figsize=(14,7))\nsbn.heatmap(dt.isnull(), yticklabels=False, cbar=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8b6a0de65b7f0692f226c5b28aa9ef99c275c799","_cell_guid":"f8beaa7d-b9e1-449d-91a5-4ae4d7b01d9b","trusted":true},"cell_type":"code","source":"print(dt['Embarked'].isnull().sum())\nprint(dt['Fare'].isnull().sum())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"909e8e7bf927e336bd84518767f03f17adba624b","_cell_guid":"99a1d374-d17d-4dfa-a3e4-fb66b531c1e8","trusted":true},"cell_type":"code","source":"dt['Age'].hist()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b561f1ea4d1d23ad4b3b1483bfe827035b29712","_cell_guid":"4cc230c2-08d9-4bbc-85b3-9b32fb848eda"},"cell_type":"markdown","source":"Null values at the \"Age\" column will take the median value of the data set. And for the \"Fare\" column, the mean value will be given."},{"metadata":{"_uuid":"6a9f073ac191fea39b9f0c9b85dde532bfdce83f","_cell_guid":"7171bd5a-bb3e-4fe0-89d9-20b07c1dd84a","trusted":true},"cell_type":"code","source":"dt_train['Age'].fillna(dt['Age'].median(skipna=True), inplace=True)\ndt_test['Age'].fillna(dt['Age'].median(skipna=True), inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f693c2706195e3ba485ec40759c9e50ab49cf6d2","_cell_guid":"96063a72-e963-4886-9a28-3951bb03e657","trusted":true},"cell_type":"code","source":"dt_train['Fare'].fillna(dt['Fare'].mean(skipna=True), inplace=True)\ndt_test['Fare'].fillna(dt['Fare'].mean(skipna=True), inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a45a3c9544276954d3197c6c1a8d9271fcb1f0ee","_cell_guid":"74ae5202-e0e7-4025-a05d-aa2a04a49688"},"cell_type":"markdown","source":"Looking at the different values of the column \"Embarked\"."},{"metadata":{"_uuid":"c50f508a1fb8934b9d22764bd447514a3cdba55b","_cell_guid":"bd8302b7-93ce-4799-9546-b7f39d9d1811","trusted":true},"cell_type":"code","source":"print(dt['Embarked'].value_counts())\nprint(pd.crosstab(dt_train.Survived, dt_train.Embarked))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"256e4fe0e63807f54df0b776bd7e2c243c346fe5","_cell_guid":"f2f44ef9-e6a1-4093-a9c4-c03b2675f869"},"cell_type":"markdown","source":"Since most of the observations have \"S\" for this variable, we'll fill the Null values with it."},{"metadata":{"_uuid":"8cfb9c8065215043226d4ff1ef0b4e4090b112e2","_cell_guid":"0adb8965-1e77-40e1-a795-a5d4bd85d638","trusted":true},"cell_type":"code","source":"dt_train['Embarked'].fillna('S', inplace=True)\ndt_test['Embarked'].fillna('S', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"912ee4dd5ae09e871954f07d8fb78489c9612871","_cell_guid":"c6917435-7654-44e4-a406-e1cb06d45f01","trusted":true},"cell_type":"code","source":"dt = pd.concat([dt_train.drop('Survived', axis=1), dt_test])\ndt.info()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b58521a0d2db9fac1678ee206fe9e380a219502e","_cell_guid":"220feecd-43ba-4072-b43f-870bdef0357e"},"cell_type":"markdown","source":"Now all the observations are complete."},{"metadata":{"_uuid":"e8e97ec464ebdc6ce3d6d89b77164ea910fda21b","scrolled":true,"_cell_guid":"8969aeea-f506-4df7-83e2-fee28d2de325","trusted":true},"cell_type":"code","source":"dt_train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6c6c617dad43a465ec51cc6be3eb1220f54ce390","_cell_guid":"830047f9-f6a6-467c-bd7d-e7d0cb4ade6a","trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsbn.countplot(x=\"Sex\", data=dt_train, hue=\"Survived\", palette=\"Set1\")\nplt.title('Sex x Survived')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b2f31f97d512c3d4ee31999992ea7eb534b2dac7","_cell_guid":"bfd4802d-cd09-40ea-b65b-0de6f2311edc","trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsbn.countplot(x=\"Embarked\", data=dt_train, hue=\"Survived\", palette=\"Set1\")\nplt.title('Embarked x Survived')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"91f6dbbdbf10896145bcb84adf7a68ce8481298a","_cell_guid":"005e8df2-bb3a-45fa-88b7-9261e4b1ba5a"},"cell_type":"markdown","source":"We are going to create 3 categories for the \"Age\" column: Under the quantile 0.08,  between the 0.08 and the 0.75 quantile, over the 0.75 quantile."},{"metadata":{"_uuid":"47c6aadb2e9a92398bc1af42c3936ee8f4825441","_cell_guid":"d8df13d0-2436-450d-8e6f-853cec0b82b8","trusted":true},"cell_type":"code","source":"intervals = (0, dt['Age'].quantile(q=0.08), dt['Age'].quantile(q=0.75), 150)\ncats = [\"under_ages\", \"between_ages\", \"upper_ages\"]\n\ndt_train[\"Age_cat\"] = pd.cut(dt_train.Age, intervals, labels=cats)\ndt_test[\"Age_cat\"] = pd.cut(dt_test.Age, intervals, labels=cats)\n\ndt_train.drop('Age', axis=1, inplace=True)\ndt_test.drop('Age', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d24e4531c2b420d6c4bc8c14711fbbfdcbd0c1dd","_cell_guid":"82af0726-f063-4ad8-a363-d707d19e796b","trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsbn.countplot(x=\"Age_cat\", data=dt_train, hue=\"Survived\", palette=\"Set1\")\nplt.title('Age_cat x Survived')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e62b3f0537b48eb3844049027bb5973773741f18","_cell_guid":"da98c4c1-6a7b-4484-98c8-8c3f4bf98cd4","trusted":false},"cell_type":"code","source":"dt['Fare'].hist(bins=16)\nprint(dt['Fare'].quantile(q=[0.5,0.75]))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d68ab78ef4dd01231727ad64ccb4d8dc71cb89b4","_cell_guid":"96944723-8564-4af8-ae3e-a8a186d0960a"},"cell_type":"markdown","source":"Also, we are going to create 3 categories for the \"Fare\" column: Under the median value, between the median value and the 0.75 quantile, over the 0.75 quantile."},{"metadata":{"collapsed":true,"_uuid":"cee09938f06087965a28036847057b58a5581e2d","_cell_guid":"b53f8217-a84d-4d13-8cf1-49d3a75dad10","trusted":false},"cell_type":"code","source":"intervals = (dt['Fare'].min(), dt['Fare'].quantile(q=0.5), dt['Fare'].quantile(q=0.75), dt['Fare'].max())\ncats = [\"cheap\", \"expensive\", \"millionaire\"]\n\ndt_train[\"Fare_cat\"] = pd.cut(dt_train.Fare, intervals, labels=cats)\ndt_test[\"Fare_cat\"] = pd.cut(dt_test.Fare, intervals, labels=cats)\n\ndt_train.drop('Fare', axis=1, inplace=True)\ndt_test.drop('Fare', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"467f2bdcd74b7dcaca16f313578eca3f41ab6065","_cell_guid":"f131be73-0ebb-41c7-98b6-d12f8b1c9003","trusted":false},"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsbn.countplot(x=\"Fare_cat\", data=dt_train, hue=\"Survived\", palette=\"Set1\")\nplt.title('Fare_cat x Survived')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c9fb5ba7bd26efdb4779bd642fd3e2588ec3f5ae","_cell_guid":"70c888f2-542a-4182-8a98-917dc7453637","trusted":false},"cell_type":"code","source":"dt_train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"57e11bc7a99ba2c7cbedd5d24ce0e43aed48e84c","_cell_guid":"f61e845c-cf1a-4a7e-984d-a05a924a6ab2"},"cell_type":"markdown","source":"The columns \"Name\", \"Cabin\" and \"Ticket\" are not going to help with anything, so we're going to drop them."},{"metadata":{"collapsed":true,"_uuid":"d215cd2ebc9c73b0615c9fd0028ee8d214d144b7","_cell_guid":"cd3a2b22-03ef-4f20-8a5e-a4d02930c6b2","trusted":false},"cell_type":"code","source":"dt_train.drop(['Name','Cabin', 'Ticket'], axis=1, inplace=True)\ndt_test.drop(['Name','Cabin', 'Ticket'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0e1d7be7f950e1ef9c79ad43c20b21ad330019ef","_cell_guid":"a686cc6d-2996-4f6c-aa61-6d196adfe8c8"},"cell_type":"markdown","source":"The \"SibSp\" and \"Parch\" columns are going to form the \"Family\" column."},{"metadata":{"collapsed":true,"_uuid":"4d38dd62611398877987e4263a8c06b32682f9b1","_cell_guid":"72303fc9-b143-4dc9-a463-bc6906d8e898","trusted":false},"cell_type":"code","source":"dt_train['Family'] = dt_train['SibSp']+dt_train['Parch']+1\ndt_test['Family'] = dt_test['SibSp']+dt_test['Parch']+1\n\ndt_train.drop(['SibSp', 'Parch'], axis=1, inplace=True)\ndt_test.drop(['SibSp', 'Parch'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ae23bc3849c0752cce09bf947bf2f3620052599a","_cell_guid":"46ae2bc8-c36a-4d18-93c8-2c55da92fee0","trusted":false},"cell_type":"code","source":"print(dt_train['Family'].value_counts())","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"6af2ba770052726072fe7c55aa04f6ace2531363","_cell_guid":"aff83bab-34b3-44d2-84d3-73153fb7ccc8","trusted":false},"cell_type":"code","source":"intervals = (0, 1, 2, 11)\ncats = [\"alone\", \"couple\", \"family\"]\n\ndt_train[\"Fam_cat\"] = pd.cut(dt_train.Family, intervals, labels=cats)\ndt_test[\"Fam_cat\"] = pd.cut(dt_test.Family, intervals, labels=cats)\n\ndt_train.drop('Family', axis=1, inplace=True)\ndt_test.drop('Family', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ca5c9cff87d206ef311a59531773c88a8f2a5cdb","_cell_guid":"88344bc8-8568-4353-baa6-745350572768","trusted":false},"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsbn.countplot(x=\"Fam_cat\", data=dt_train, hue=\"Survived\", palette=\"Set1\")\nplt.title('Fam_cat x Survived')","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"3b2113486b31f6dcde7ddf6e77ba01a75957d600","_cell_guid":"6a52e198-178c-4a06-8fdc-d3858892c5fe","trusted":false},"cell_type":"code","source":"dt_train = pd.get_dummies(dt_train, columns=['Pclass', 'Sex', 'Embarked', 'Age_cat', 'Fare_cat', 'Fam_cat'], drop_first=True)\ndt_test = pd.get_dummies(dt_test, columns=['Pclass', 'Sex', 'Embarked', 'Age_cat', 'Fare_cat', 'Fam_cat'], drop_first=True)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2e27f381989dac004b532f3ce02b7321223e05e9","_cell_guid":"fca50009-bd4e-4bb3-bf51-6c6a3b79284c","trusted":false},"cell_type":"code","source":"dt_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"e6d48d0ade69149435a64792e728ba1b029c051b","_cell_guid":"1e5c3a54-fa33-4b55-9567-7d4a52dda0ea","trusted":false},"cell_type":"code","source":"from xgboost import XGBClassifier","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"a7fbfa9323030d4d48b54347f78414604c1b856c","_cell_guid":"9ac96ddd-f4e9-44ac-b2d6-9128be575d43","trusted":false},"cell_type":"code","source":"classifier =  XGBClassifier(n_estimators=1000, learning_rate=0.05,n_jobs=-1)\n","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"ec8fde50e9ea130758e63651cdca7685f6b50ba3","_cell_guid":"cee060b1-1b95-4440-bf2c-34fd6e2c0990","trusted":false},"cell_type":"code","source":"X_train = dt_train.drop(['Survived', 'PassengerId'], axis=1)\ny_train = dt_train['Survived']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c3e0661214ce827b31e13c643a23f5c96319c540","_cell_guid":"645cd80b-c91d-4fd1-99c9-515b41de87bf","trusted":false},"cell_type":"code","source":"classifier.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"f97c25c42dbed868f5a0051535f804c8638a41c5","_cell_guid":"59a915ba-9ffc-43ff-841e-052f5a2ba7bd","trusted":false},"cell_type":"code","source":"predictions = classifier.predict(dt_test.drop('PassengerId', axis=1))\noutput = pd.DataFrame()\noutput['PassengerId'] = dt_test['PassengerId']\noutput['Survived'] = predictions","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2ac589ffb28646910026294fe8b314d4f94e852","_cell_guid":"4f1c6ad5-e892-4cba-924c-91fc49b9974c","trusted":false},"cell_type":"code","source":"output.head()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"69450d6439bee11df7188398d1a93980bc462203","_cell_guid":"0732442e-fbd0-4929-823d-795cf051d9bd","trusted":false},"cell_type":"code","source":"output.to_csv(\"output.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"2135292bc9407b2c1a33561712710475c3f1312e","_cell_guid":"a42b7369-fd18-4174-886d-ee9857dcd754","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}