{"cells":[{"metadata":{"trusted":true,"_uuid":"dce84f6e67adb241151cb0cb008b20e324657ed3"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np \nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85838bab5c5388908ebf0cd0b60daf0fe9dd94a7"},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee3f0e2fbc9707ad9b438a22fe470066b7eeea07"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6907b865674ce6bf7a5b8484e002a4d8bddbd526"},"cell_type":"code","source":"sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a198a284923c8c220e498f4bb8b7a534dee90883"},"cell_type":"code","source":"sns.countplot(data=train,x='Survived',hue='Pclass')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6835aedef133f532f15c96cd6d9d87af10e4bfa1"},"cell_type":"code","source":"sns.distplot(train['Age'].dropna(),kde=False,bins=30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11c54016dabcd1d703282a48f7d22fa9fac16826"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"840821a1694083533c5bf6f0b6a2d2fd6e5db9ab"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e122f189dbf69749d9b8aef59e72996ba98d15e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76b1fcd3b1b42aaa54793ad616b9f226de340e31"},"cell_type":"code","source":"sns.boxplot(x='Pclass',y='Age',data=train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecb255e03d80e90ca7ceb993c3b9d74d5f7ffed9"},"cell_type":"code","source":"def calculate_age(cols):\n    Age=cols[0]\n    Pclass=cols[1]\n    if pd.isnull(Age):\n        if Pclass == 1:\n            return 37\n        elif Pclass==2:\n            return 29\n        else:\n            return 24\n    else:\n        return Age","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"317f13a8f361fbfc900f9cb13fb5a675d317a737"},"cell_type":"code","source":"train[['Age','Pclass']].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5313fd6506953e66d33e22eba56ff2e95ae48033"},"cell_type":"code","source":"train['Age']=train[['Age','Pclass']].apply(calculate_age,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e48047d65b24a27f9a5ebcb46825eb269b833273"},"cell_type":"code","source":"sns.heatmap(train.isnull(),cmap='viridis',yticklabels=False,cbar=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fcaa16e513b731fad36abb9abe6f2d6a9f5fbda"},"cell_type":"code","source":"train.drop('Cabin',axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e93777e4dc252f6cb0b3baf33273e3dd1bf4d9a9"},"cell_type":"code","source":"sns.heatmap(train.isnull(),cmap='viridis',yticklabels=False,cbar=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7ba749ccc6b8822fa74946d019c8b150328ddc1"},"cell_type":"code","source":"train.dropna(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2805804d37bd9e969c8286ef04d34a93fe480bf5"},"cell_type":"code","source":"sns.heatmap(train.isnull(),cmap='viridis',yticklabels=False,cbar=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7a4196636700aaf16a44ad53d537b852d5c43b6"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"89d8e99281a4fd740580c5f9d33eb15fa15f3f4d"},"cell_type":"code","source":"pd.get_dummies(train['Sex']).head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9f7ea10381d21a7b63e3616eeaed516ab85c9bd"},"cell_type":"code","source":"sex=pd.get_dummies(train['Sex'],drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f784a23a5602ea3899c448dd94f7cba3883f71d"},"cell_type":"code","source":"embark=pd.get_dummies(train['Embarked'],drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f29291545b23800fbb0db3659058f0865c2abbd6"},"cell_type":"code","source":"train=pd.concat([train,sex,embark],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eafd47ddf607321ce207b8a4e32a1654be4546b9"},"cell_type":"code","source":"train.head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dec0383dacdf0c61fa4e512a2fb62ad136ee1a2f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf267a23be4a0ca33b28ee8e9f48906c7f4ba11e"},"cell_type":"code","source":"train.drop('PassengerId',axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1319f502115431dae24ba6f502ab11c32041ed3a"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"23a32ff4087a8b6ce72d07f94b7f85c37316e091"},"cell_type":"code","source":"PC=pd.get_dummies(train['Pclass'],drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95fa3bb523d078e9d5bbd89cae70f3b4ffb7b99b"},"cell_type":"code","source":"train=pd.concat([train,PC],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6623635e8084fde594dcaf8d59bcb4067da82e26"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f31b0d1a9e6ba7a64514e36ffa428d14468d71a6"},"cell_type":"code","source":"#train.drop([2,3],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0525271e19ce625959be53efa4e21c77f2e8a262"},"cell_type":"code","source":"train.drop(['Embarked'],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b5dd7bfcc3238d0d80e801fb089ee989fc92f83"},"cell_type":"code","source":"train.drop(['Name','Sex','Ticket','Pclass'],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48a15c5c1d6a801d6f371cacdd7d559009052f3c"},"cell_type":"code","source":"X=train.drop('Survived',axis=1)\ny=train['Survived']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96e327048fb64d60e1a687547a0469a800faa742"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6853e5227ae421effe34748bb718a493e00ce4cc"},"cell_type":"code","source":"X.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"be162b7157e4dec51e69a2f0ea4706b7178a29ab"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a2479915f26d62f936ef431b6dd1667be80ab37"},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de0593d48874b29b4c619db99d5e293ffc877d80"},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"22393e61d67172729a87f106bcfb21ac91f9d4e4"},"cell_type":"code","source":"logmodel=LogisticRegression()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c9ac4db4231185f79f3775c45d733b86c3ebb21"},"cell_type":"code","source":"logmodel.fit(X_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76cfd2f3aa5fbfd115f810bdf19d78535a23e025"},"cell_type":"code","source":"predictions=logmodel.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d4a4e5a16add5d8a98b9f4d3489480937070f92"},"cell_type":"code","source":"from sklearn.metrics import classification_report","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b74a69035dc11c7eac377f75b7c566911b5181fd"},"cell_type":"code","source":"print(classification_report(y_test,predictions))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b08e201a5ea2a7c166b3e3e9527f83abc5f707d4"},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"02b4516e9da9b116c368b1e67ebf7a463d2213a0"},"cell_type":"code","source":"confusion_matrix(y_test,predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"135d4453de718b5cb0961778db5e20893c48692d"},"cell_type":"code","source":"test_data = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"692e85ca49cce09a78ccc1a426af2cfbae531e2d"},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6988e0857976a657e1e69dec133f85aaaa4ec71"},"cell_type":"code","source":"sns.boxplot(x=test_data['Pclass'],y=test_data['Age'],data=test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70945b0ac47d62bc2c071ee8f06edb9badc9c05d"},"cell_type":"code","source":"test_data['Age']=test_data[['Age','Pclass']].apply(calculate_age,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c9ede462d2a1d54a29d32db7e287b7fb2ac93d9"},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e01397b237be5a45f81370fa5c22c3a601b02aa"},"cell_type":"code","source":"sns.heatmap(test_data.isnull(),cmap='viridis',yticklabels=False,cbar=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca28c0df755dd81d8c7dde61105044ab1b184b6b"},"cell_type":"code","source":"test_data.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a174cae776ae96c992e11c202f9c889c968b7377"},"cell_type":"code","source":"test_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"379c225aa5e603c5f1b8897171bfc023aff5d46f"},"cell_type":"code","source":"\nsns.boxplot(x='Pclass',y='Fare',data=test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b25e34e65c6c7615c5e6eeae938ace1470606e28"},"cell_type":"code","source":"test_data[test_data['Pclass'] == 3]['Fare'].median()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ee8180499a73c3b664968e45b63f0b94611c810"},"cell_type":"code","source":"def calculate_Fare(cols):\n    Fare=cols[0]\n    Pclass=cols[1]\n    if pd.isnull(Fare):\n        if Pclass == 1:\n            return 60\n        elif Pclass==2:\n            return 15.75\n        else:\n            return 7.8958\n    else:\n        return Fare","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2398e8acc471c18f29bfb8929aa640cea0044f8d"},"cell_type":"code","source":"test_data['Fare'] = test_data[['Fare','Pclass']].apply(calculate_Fare,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba7bb1b667609791849a76d292565abb6eb9278a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5f7bb8215fb1397428fb20b5caa4c5b1ef0125f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33b1f66d72df035aea26690aaa7d6d2b9451e3c2"},"cell_type":"code","source":"sex = pd.get_dummies(test_data['Sex'],drop_first=True)\nembark = pd.get_dummies(test_data['Embarked'],drop_first=True)\nPC = pd.get_dummies(test_data['Pclass'],drop_first=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c58d1d7ce667005065a2abb5e6a519b953924804"},"cell_type":"code","source":"test_data = pd.concat([test_data,sex,embark,PC],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b1d56fea36016b88d93b034e604d9702bc863c1"},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8bd3ffdd4e5d51c2322a9a01dbe433cc37ada40"},"cell_type":"code","source":"test_data.drop(['Sex','Embarked','Pclass','Name','Ticket','Cabin'],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dfad311efcb6a21713372c5e582699a7798dd5c2"},"cell_type":"code","source":"test_data.head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8ab27ee63776e4cbcae50fe369da206d55c3088"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74c1b3d2c3148caf04c5e3ad45ee130fcf8db9ac"},"cell_type":"code","source":"test_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70a19a41337b6f8370fb2ad8931a200ab3fbf9c8"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"daac7718525d6202c63161b0df225b350ddd6efe"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"acf685076a948dade16e6e6e3562ae3ee99a5da7"},"cell_type":"code","source":"idx.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebb8ff03000abc32f3f9b25f27e3a666d1986eca"},"cell_type":"code","source":"idx.rename(columns={'idx':'PassengerId'},inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"052e0cc0ef505e60a720bbba92956ffa9df99cdc"},"cell_type":"code","source":"idx.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77e10b7d3d68e333db2da6cc2e1abe92f0c044fd"},"cell_type":"code","source":"test_data.drop('PassengerId',inplace=True,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53e229a4cec0585a2cc2bb6c8fc54766059e106b"},"cell_type":"code","source":"predictions_new = logmodel.predict(test_data)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7cda3d09a656abe28ae707d03fa2f0f8af342c57"},"cell_type":"code","source":"survived = pd.DataFrame({'Survived':predictions_new})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f7f59df0931d945f8dd0fa7574e6bd2ea9c24343"},"cell_type":"code","source":"survived.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1dc501bbc4fe183ec1e4d823558a232cd2c38e54"},"cell_type":"code","source":"submission=pd.concat([idx,survived],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"65e67ad2141ab84889f296aa3077e7e441a91cc5"},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d739e6994761573db86e3a7b3ce0542913b05a62"},"cell_type":"code","source":"OUTPUT_RESULT=\"submission1.csv\"\nsubmission.to_csv(OUTPUT_RESULT,index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6224b9c0e83c1932d2ae02de53d9f7988d1f4e0a"},"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}