{"cells":[{"metadata":{"trusted":true,"_uuid":"1361271d750697b8fa88e61e2245b66523ad1ae8"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f757d68b6c7cbc0215af20ac6cbdeffc99c02e94"},"cell_type":"code","source":"#Check for the comptetition files\nimport os\nprint(os.listdir(\"../input\"))\n#Load test dataset\ntrain = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')\nsubmit = pd.read_csv('../input/gender_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f5e3f64cd528d625222a64472c98664bf6089d8"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6286cb567403a9db33e51ab2caeb3fe378316e4"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"484bdbc89e422c4659f421484643853d4f4eadb3"},"cell_type":"code","source":"sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"426a874ac9013bb9d1d6958cdfed482b8433b0e7"},"cell_type":"code","source":"sns.heatmap(test.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad3626aa5e54dace963f041efbf9e64c5c709e76"},"cell_type":"code","source":"sns.set_style('whitegrid')\nsns.countplot(x='Survived',data=train,palette='RdBu_r')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f54d2e0bd5476580aec4ad01d543ae529d956949"},"cell_type":"code","source":"sns.set_style('whitegrid')\nsns.countplot(x='Survived',hue='Sex',data=train,palette='RdBu_r')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"87a351f3a0d07bb74984c33ccfea16c7d6fb4fc3"},"cell_type":"code","source":"sns.set_style('whitegrid')\nsns.countplot(x='Survived',hue='Pclass',data=train,palette='rainbow')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a588ba551c8e4880964ea850651f9daedd312b7"},"cell_type":"code","source":"sns.distplot(train['Age'].dropna(),kde=False,color='darkred',bins=30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2206a5e585513ae9e487c8b95acc6661cfd841d1"},"cell_type":"code","source":"train['Age'].hist(bins=30,color='darkred',alpha=0.7)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8db4ca6967ac50feaf87b222f021120b56e16d3b"},"cell_type":"code","source":"sns.countplot(x='SibSp',data=train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2511caf75a8b1b9e7f6d64a321aba99679acd1f2"},"cell_type":"code","source":"train['Fare'].hist(color='green',bins=40,figsize=(8,4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"064e265d179d209af77da4c098fb0f8f1e2a8007"},"cell_type":"code","source":"plt.figure(figsize=(12, 7))\nsns.boxplot(x='Pclass',y='Age',data=train,palette='winter')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e69680d15b60c8763b6b66bf20f26215398425bf"},"cell_type":"code","source":"def impute_age(cols):\n    Age = cols[0]\n    Pclass = cols[1]\n    \n    if pd.isnull(Age):\n\n        if Pclass == 1:\n            return 37\n\n        elif Pclass == 2:\n            return 29\n\n        else:\n            return 24\n\n    else:\n        return Age","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56e5e9b12d2f3d2e18844ac6b8f9276f3ea9b8bb"},"cell_type":"code","source":"train['Age'] = train[['Age','Pclass']].apply(impute_age,axis=1)\ntest['Age'] = train[['Age','Pclass']].apply(impute_age,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2b3055352db2da68a9579b89753ca22c77b4409"},"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82696e9faa12e92fb1795ffaae707696d1f89ede"},"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.heatmap(test.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"185008b47b63dcd284907621b52780da20e6bdc5"},"cell_type":"code","source":"train.drop('Cabin',axis=1,inplace=True)\ntest.drop('Cabin',axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2dafe2845bf3348818b9612e943170271dac25b7"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f45e153151766c8464f86def1ba9e3868a29cbf"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"250e851d2a70d934e99324311038ad1aabc33ecd"},"cell_type":"code","source":"def fare(x):\n    if pd.isnull(x):\n        return np.mean(test['Fare'])\n    else:\n        return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f7cb29a118162a91169cff2e252d9f5004f75fd"},"cell_type":"code","source":"train.dropna(inplace=True)\ntest['Fare'] = test['Fare'].apply(fare)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6713dd25273d5c0174e1a7e548fa3d02619d4b86"},"cell_type":"code","source":"test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19fe6eccf5f120698c9552ef6573bf8e959f0424"},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c95bcc3e94f990e51fe99b180015edb1d77961f"},"cell_type":"code","source":"sex = pd.get_dummies(train['Sex'],drop_first=True)\nembark = pd.get_dummies(train['Embarked'],drop_first=True)\nsex1 = pd.get_dummies(test['Sex'],drop_first=True)\nembark1 = pd.get_dummies(test['Embarked'],drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e6964e9cab2411a2697cc64d64862aca4bfdc76"},"cell_type":"code","source":"train.drop(['Sex','Embarked','Name','Ticket'],axis=1,inplace=True)\ntest.drop(['Sex','Embarked','Name','Ticket'],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf79f31050681678d48e0968bf9724fe8e1c88d9"},"cell_type":"code","source":"train = pd.concat([train,sex,embark],axis=1)\ntest = pd.concat([test,sex1,embark1],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3082c2560248395b098f727844c9710555779f15"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f17b157d3020b05486bc3c2581098f0c18d5983"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01cac7f0ddc959611ce67cd0527a622e22b5423a"},"cell_type":"code","source":"X_train = train.drop('Survived',axis = 1)\ny_train = train['Survived']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f5c9b03e7b8dfe492330e8e809836652e95b58e0"},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed5028c3eef8cab4abbcfad55d5fb8aa5e209113"},"cell_type":"code","source":"logmodel = LogisticRegression()\nlogmodel.fit(X_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"546d150b2393908c2270fdbfd407ae49075fb786"},"cell_type":"code","source":"predictions = logmodel.predict(test)\npredictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8fb6d3a2cfc8cc2ccfb192e70a7ab8cde040954"},"cell_type":"code","source":"submit.drop('PassengerId', axis = 1, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03e2198f27b4a5ce99b0111eaa629de2ea331d5d"},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e821af34fb53af7a04ff97b3e086cd405a68678"},"cell_type":"code","source":"print(confusion_matrix(submit, predictions))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33c4c676d698e8fb1527b07f8a822d5c660b20fc"},"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.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}