{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom pandas import Series, DataFrame \nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt \n%matplotlib inline sns.set_style('whitegrid')\nimport warnings \nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caf4bf79359489ccea75aef79221fdaaa8a07844"},"cell_type":"code","source":"X_Test = test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a6dc2e19c040ee5ac509c0acc98fb2c9c85cb5b"},"cell_type":"code","source":"y = train.iloc[:, 1]\nX = train.iloc[:,:].drop(columns=['Survived'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9da16f04d2ba3bf0b32e8e52080e57e1ba66a10"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0aafab58ce62f6dd1f6d5225a2b396e0b8ae3cb9"},"cell_type":"code","source":"y_train.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39492ed1b4c2a2a2b7e9fd6911e72bb80c927640"},"cell_type":"code","source":"y_train.count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3a45029e19b13ec7bf3eaf1429cc0216ac3403d"},"cell_type":"code","source":"y_train_count = y_train.value_counts() \ny_train_count","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"333883215b7b8e09bfc4ac73708531493856d3d8"},"cell_type":"raw","source":""},{"metadata":{"trusted":true,"_uuid":"5d289158b75457a6a98c5257e96e5aea95d67f32"},"cell_type":"code","source":"sns.countplot(x='Survived',data=train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93a91dde23f2324706b2d469722816bcc135c781"},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"009ed62d3e06d076cec00a253250436dbfc59c81"},"cell_type":"code","source":"X_train['Pclass'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d1c051785ee758847a7201cc78aaed47bd84d7d"},"cell_type":"code","source":"import collections, numpy\nCount_Unique_Sex = np.unique(X_train['Sex']) \ncollections.Counter(Count_Unique_Sex)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1849776e71ffb20852aecc0bd964a5586a6584a8"},"cell_type":"code","source":"X_train['Sex'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e65905f5392ea2c164f1482992dd6ecd99ef78d1"},"cell_type":"code","source":"X_train['Parch'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ff270cfdfc5878ad4feb758804853ac3bc13058a"},"cell_type":"code","source":"X_train['Ticket'].value_counts().head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b89d6da5a52dfc8246289db694bd088e7b4dfbc"},"cell_type":"code","source":"X_train.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8359793703eaaa5eb65e97b7cc3a905f0686939e"},"cell_type":"code","source":"sns.barplot(x=X_train.iloc[:,1], y=y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cf3daa5bf6cb5e474d7eec4ae64668417850aa3"},"cell_type":"code","source":"sns.barplot(x=X_train.iloc[:,3], y=y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"590af14e914738f649274bfc31307ef1b30c50ec"},"cell_type":"code","source":"g = sns.FacetGrid(train, col='Survived')\ng.map(plt.hist, 'SibSp', bins=20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f792854507a0658d4475ec178368ad4a573e5053"},"cell_type":"code","source":"g = sns.FacetGrid(train, col='Survived')\ng.map(plt.hist, 'Parch', bins=20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9247351446e601b573a8db98643224726092be48"},"cell_type":"code","source":"X_train = X_train.drop(columns=['PassengerId', 'Name', 'Ticket'], axis=1) \nX_test = X_test.drop(columns=['PassengerId', 'Name', 'Ticket'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"342415328810532ccee260469a8e607e8754edb7"},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"937dc9b240c76d43531b0bd858b5b9b1e08612bc"},"cell_type":"code","source":"g = sns.FacetGrid(train, col='Survived') \ng.map(plt.hist, 'Age', bins=20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a84e1b659ece09a7a0e4d50ada924dd83fa893b"},"cell_type":"code","source":"corr = train.corr()\ncorr.style.background_gradient()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"841a0247f3f3165df69c43419bf23dc64093baa6"},"cell_type":"code","source":"Cabin = X_train.iloc[:,6].fillna('unknown') \nCabin_test = X_test.iloc[:,6].fillna('unknown')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa1f25241248d7fb0ae0d2b4c54d4a13ab268b77"},"cell_type":"code","source":"Cabin.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88e1abcf4e55f93b9c0f0bd660cc0f2cf52eee87"},"cell_type":"code","source":"X_train['Cabin'] = Cabin \nX_test['Cabin'] = Cabin_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f217c7904cdac65fbd0898e01d952ef0354ba44"},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0a05573025f15f19c9dcd44047abd84c6cf870e"},"cell_type":"code","source":"X_train.iloc[:,[2]].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"106757e5db0e2b4c2770a678516fe6b96c914e7d"},"cell_type":"code","source":"from sklearn.preprocessing import Imputer\nimp = Imputer(missing_values='NaN', strategy='mean', axis=0) \nimp.fit(X_train.iloc[:,2].values.reshape(-1, 1))\nAge = imp.transform(X_train.iloc[:,2].values.reshape(-1, 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f04cac78bce0742b626d8e9e35190df1d7103cba"},"cell_type":"code","source":" imp.fit(X_test.iloc[:,2].values.reshape(-1, 1))\nAge_test = imp.transform(X_test.iloc[:,2].values.reshape(-1, 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af17a5b59dde0de5f6e1b5e9e43bbbc80aa70809"},"cell_type":"code","source":"X_train['Age'] = Age\nX_train['Age'] = X_train['Age'].astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e25af48cecdfc90b666a80a378171d57479f17a"},"cell_type":"code","source":"X_test['Age'] = Age_test\nX_test['Age'] = X_test['Age'].astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d658663293a0bfcec2e93f6c07ee30ee61a6cc31"},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder, OneHotEncoder \nle = LabelEncoder()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"23f38343d57b416aa9d64d2282d22f0bb1b98b4c"},"cell_type":"code","source":"Pclass_transformed = le.fit_transform(X_train.iloc[:,[0]].astype(str)) \nSibSp_transformed = le.fit_transform(X_train.iloc[:,[3]].astype(str)) \nParch_transformed = le.fit_transform(X_train.iloc[:,[4]].astype(str))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbd114aa2b8360c74cb3d7ee26b78c4341908b15"},"cell_type":"code","source":"X_train['Pclass'] = Pclass_transformed \nX_train['SibSp'] = SibSp_transformed \nX_train['Parch'] = Parch_transformed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8c941c11ed0f82b6b58aa453c92f9511fef5472"},"cell_type":"code","source":"PclassTest_transformed = le.fit_transform(X_test.iloc[:,[0]].astype(str)) \nSibSpTest_transformed = le.fit_transform(X_test.iloc[:,[3]].astype(str)) \nParchTest_transformed = le.fit_transform(X_test.iloc[:,[4]].astype(str))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67cfd69302362cb3f408171fb8c9c5bffba676fd"},"cell_type":"code","source":"X_test['Pclass'] = PclassTest_transformed \nX_test['SibSp'] = SibSpTest_transformed \nX_test['Parch'] = ParchTest_transformed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2077cdea4df5ee57544d3fe0cc8835e36c03fc2d"},"cell_type":"code","source":"X_train.iloc[:,6:8].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f2c83fcb00282d08afa202289e92c457715c7d2"},"cell_type":"code","source":"Cabin_transformed = le.fit_transform(X_train.iloc[:,[6]].astype(str)) \nEmbarked_transformed = le.fit_transform(X_train.iloc[:,[7]].astype(str)) \nSex_transformed = le.fit_transform(X_train.iloc[:,[1]].astype(str))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66c9cb52d897318ee5c85f0c9de0e7aad68bf50c"},"cell_type":"code","source":"Cabin_transformedTest = le.fit_transform(X_test.iloc[:,[6]].astype(str)) \nEmbarked_transformedTest = le.fit_transform(X_test.iloc[:,[7]].astype(str)) \nSex_transformedTest = le.fit_transform(X_test.iloc[:,[1]].astype(str))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70f97c95e3a91465aa2ec9a0a4f2eb63fc151541"},"cell_type":"code","source":"X_train['Cabin'] = Cabin_transformed \nX_train['Embarked'] = Embarked_transformed \nX_train['Sex'] = Sex_transformed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e876fb3d92d13a762f3b04a16afe34306aa6ab0f"},"cell_type":"code","source":"X_test['Cabin'] = Cabin_transformedTest \nX_test['Embarked'] = Embarked_transformedTest \nX_test['Sex'] = Sex_transformedTest","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35536bc8fe0280d3c9d592022e01a57898a6c562"},"cell_type":"code","source":"pd.DataFrame(X_train).head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b91d92e89cb2c59641dfeac2ce0956c0c1d215a"},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler \nsc = StandardScaler()\n\nX_train = sc.fit_transform(X_train) \npd.DataFrame(X_train).head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb542dd2aa1f010a52ce6bac95004334c62d5c82"},"cell_type":"code","source":"X_test = sc.fit_transform(X_test)\npd.DataFrame(X_test).head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5df6d81658e5e9d5e4fd9629578b476dc772108"},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression \nclf = LogisticRegression().fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c870e2e36a449dcaa3067505876545de01b361c0"},"cell_type":"code","source":"y_pred = clf.predict(X_test) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a5c702859c4fc6cacc0d2e636bf391a0d45be2d2"},"cell_type":"code","source":"clf.score(X_train, y_train)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dabeba75d7c3d88243831714b88c86c1d3dbcd6d"},"cell_type":"code","source":"Y_pred_proba = clf.predict_proba(X_test) \npd.DataFrame(Y_pred_proba).head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4bb46c298a39e038ca958a65f0a1586bb5e3c0e"},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix \ncm = confusion_matrix(y_test, y_pred)\ncm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ddf1047cd515a88352319911c566e21edf1a36a"},"cell_type":"code","source":"plt.clf()\nplt.imshow(cm, interpolation='nearest', cmap=plt.cm.Wistia) \nclassNames = ['Negatif','Positif']\nplt.title('Matrice de confusion')\nplt.ylabel('True label')\nplt.xlabel('Predicted label')\ntick_marks = np.arange(len(classNames)) \nplt.xticks(tick_marks, classNames, rotation=45) \nplt.yticks(tick_marks, classNames)\ns = [['VN','FP'], ['FN', 'VP']]\nfor i in range(2):\n    for j in range(2):\n        plt.text(j,i, str(s[i][j])+\" = \"+str(cm[i][j])) \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a3f1a093e642072d4ae4dc32b67f92a11e812cf"},"cell_type":"code","source":"from sklearn.metrics import accuracy_score \naccuracy_score(y_test, y_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a0a48adacd6b84fa8d47b0bc671bdde1846aa76"},"cell_type":"code","source":"Var_cible = y_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62a1a6c11a922cca846b1c05587f2bff8e7df3e3"},"cell_type":"code","source":"Proba_estime = Y_pred_proba[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d2bfd46e7543d6ad4cc871e32834a1d0e6536a9"},"cell_type":"code","source":"Proba_estime = pd.DataFrame(Proba_estime) \nProba_estime.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"87e3c66551e0e77755109310e8e7c97fa110a3ab"},"cell_type":"code","source":"y_test = pd.DataFrame(y_test) \ny_test = y_test.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75d0532a23a279b69783770aa04fcc64c89a762e"},"cell_type":"code","source":"df = y_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68b79e0599e97807ca6b9b417981b8c2cfc66544"},"cell_type":"code","source":"df['Proba'] = Proba_estime","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"57d8ca2f4da14a7e91011e935ddb16e6a9f308ac"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1c9ad872cdfb7e1c37c05d274734a6a0369df5a"},"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}