{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nfrom statistics import harmonic_mean\n\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import  SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.compose import ColumnTransformer\n\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.ensemble import GradientBoostingClassifier\n\n\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import cross_val_predict\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import f1_score\nfrom sklearn.metrics import precision_recall_curve\nfrom sklearn.metrics import roc_curve\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:29.341455Z","iopub.execute_input":"2022-07-06T18:15:29.342012Z","iopub.status.idle":"2022-07-06T18:15:29.995375Z","shell.execute_reply.started":"2022-07-06T18:15:29.341907Z","shell.execute_reply":"2022-07-06T18:15:29.993997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds= pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntitanic_ds.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:29.996857Z","iopub.execute_input":"2022-07-06T18:15:29.997168Z","iopub.status.idle":"2022-07-06T18:15:30.041885Z","shell.execute_reply.started":"2022-07-06T18:15:29.997142Z","shell.execute_reply":"2022-07-06T18:15:30.040664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.drop(\"PassengerId\",axis=1,inplace=True)\ntitanic_ds.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:30.045436Z","iopub.execute_input":"2022-07-06T18:15:30.045809Z","iopub.status.idle":"2022-07-06T18:15:30.078109Z","shell.execute_reply.started":"2022-07-06T18:15:30.045778Z","shell.execute_reply":"2022-07-06T18:15:30.076887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:30.081664Z","iopub.execute_input":"2022-07-06T18:15:30.082277Z","iopub.status.idle":"2022-07-06T18:15:30.113807Z","shell.execute_reply.started":"2022-07-06T18:15:30.082232Z","shell.execute_reply":"2022-07-06T18:15:30.112586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.hist(bins=50,figsize=(25,20))\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:30.115389Z","iopub.execute_input":"2022-07-06T18:15:30.116122Z","iopub.status.idle":"2022-07-06T18:15:31.496618Z","shell.execute_reply.started":"2022-07-06T18:15:30.116077Z","shell.execute_reply":"2022-07-06T18:15:31.495650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(titanic_ds.corr())\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:31.497796Z","iopub.execute_input":"2022-07-06T18:15:31.499103Z","iopub.status.idle":"2022-07-06T18:15:31.762964Z","shell.execute_reply.started":"2022-07-06T18:15:31.499056Z","shell.execute_reply":"2022-07-06T18:15:31.762125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"surv_corr=titanic_ds.corr().Survived.sort_values(ascending=False)\nsurv_corr","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:31.764097Z","iopub.execute_input":"2022-07-06T18:15:31.764943Z","iopub.status.idle":"2022-07-06T18:15:31.773672Z","shell.execute_reply.started":"2022-07-06T18:15:31.764907Z","shell.execute_reply":"2022-07-06T18:15:31.772869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Pclass.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:31.775158Z","iopub.execute_input":"2022-07-06T18:15:31.775480Z","iopub.status.idle":"2022-07-06T18:15:31.785664Z","shell.execute_reply.started":"2022-07-06T18:15:31.775451Z","shell.execute_reply":"2022-07-06T18:15:31.784480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Sex.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:31.787122Z","iopub.execute_input":"2022-07-06T18:15:31.788210Z","iopub.status.idle":"2022-07-06T18:15:31.799383Z","shell.execute_reply.started":"2022-07-06T18:15:31.788167Z","shell.execute_reply":"2022-07-06T18:15:31.798242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Cabin.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:31.801075Z","iopub.execute_input":"2022-07-06T18:15:31.801759Z","iopub.status.idle":"2022-07-06T18:15:31.814429Z","shell.execute_reply.started":"2022-07-06T18:15:31.801716Z","shell.execute_reply":"2022-07-06T18:15:31.813235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Embarked.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:31.816113Z","iopub.execute_input":"2022-07-06T18:15:31.816806Z","iopub.status.idle":"2022-07-06T18:15:31.827463Z","shell.execute_reply.started":"2022-07-06T18:15:31.816762Z","shell.execute_reply":"2022-07-06T18:15:31.826505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"attributes=[\"Age\",\"Fare\"]\nsns.pairplot(titanic_ds[attributes],aspect=3,height=3)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T18:15:31.829787Z","iopub.execute_input":"2022-07-06T18:15:31.830281Z","iopub.status.idle":"2022-07-06T18:15:33.097658Z","shell.execute_reply.started":"2022-07-06T18:15:31.830239Z","shell.execute_reply":"2022-07-06T18:15:33.096117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=titanic_ds,x=\"Pclass\",y=\"Fare\")","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.102354Z","iopub.execute_input":"2022-07-06T18:15:33.102733Z","iopub.status.idle":"2022-07-06T18:15:33.281726Z","shell.execute_reply.started":"2022-07-06T18:15:33.102700Z","shell.execute_reply":"2022-07-06T18:15:33.280691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.regplot(data=titanic_ds,x=\"Age\",y=\"Fare\")\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.283150Z","iopub.execute_input":"2022-07-06T18:15:33.283462Z","iopub.status.idle":"2022-07-06T18:15:33.592746Z","shell.execute_reply.started":"2022-07-06T18:15:33.283435Z","shell.execute_reply":"2022-07-06T18:15:33.591583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data=titanic_ds,x=\"Age\",kind=\"kde\")\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.594192Z","iopub.execute_input":"2022-07-06T18:15:33.595239Z","iopub.status.idle":"2022-07-06T18:15:33.849309Z","shell.execute_reply.started":"2022-07-06T18:15:33.595192Z","shell.execute_reply":"2022-07-06T18:15:33.848156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds[titanic_ds.Embarked.isna()]","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-06T18:15:33.850844Z","iopub.execute_input":"2022-07-06T18:15:33.851537Z","iopub.status.idle":"2022-07-06T18:15:33.868242Z","shell.execute_reply.started":"2022-07-06T18:15:33.851492Z","shell.execute_reply":"2022-07-06T18:15:33.866977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds[(\"113000\"<titanic_ds.Ticket) & (titanic_ds.Ticket<\"114000\")].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.869703Z","iopub.execute_input":"2022-07-06T18:15:33.870071Z","iopub.status.idle":"2022-07-06T18:15:33.890472Z","shell.execute_reply.started":"2022-07-06T18:15:33.870040Z","shell.execute_reply":"2022-07-06T18:15:33.889315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds[(\"113000\"<titanic_ds.Ticket) & (titanic_ds.Ticket<\"114000\")].Embarked.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.891870Z","iopub.execute_input":"2022-07-06T18:15:33.892884Z","iopub.status.idle":"2022-07-06T18:15:33.908297Z","shell.execute_reply.started":"2022-07-06T18:15:33.892837Z","shell.execute_reply":"2022-07-06T18:15:33.907032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Embarked.fillna(\"S\",inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.911727Z","iopub.execute_input":"2022-07-06T18:15:33.912085Z","iopub.status.idle":"2022-07-06T18:15:33.918723Z","shell.execute_reply.started":"2022-07-06T18:15:33.912055Z","shell.execute_reply":"2022-07-06T18:15:33.917120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Embarked.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.920453Z","iopub.execute_input":"2022-07-06T18:15:33.921182Z","iopub.status.idle":"2022-07-06T18:15:33.936764Z","shell.execute_reply.started":"2022-07-06T18:15:33.921137Z","shell.execute_reply":"2022-07-06T18:15:33.935650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds[titanic_ds.Ticket.str.split(\" \").apply(lambda x: True if len(x)>1 else False)]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.938545Z","iopub.execute_input":"2022-07-06T18:15:33.938994Z","iopub.status.idle":"2022-07-06T18:15:33.965274Z","shell.execute_reply.started":"2022-07-06T18:15:33.938952Z","shell.execute_reply":"2022-07-06T18:15:33.964283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Ticket=titanic_ds.Ticket.str.split(\" \").apply(lambda x: x[-1] if len(x)>1 else x[0])\ntitanic_ds.Ticket=titanic_ds.Ticket.apply(lambda x: \"0\" if x==\"LINE\" else x)\ntitanic_ds[\"Ticket\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.966620Z","iopub.execute_input":"2022-07-06T18:15:33.966941Z","iopub.status.idle":"2022-07-06T18:15:33.979762Z","shell.execute_reply.started":"2022-07-06T18:15:33.966911Z","shell.execute_reply":"2022-07-06T18:15:33.978592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Ticket=titanic_ds.Ticket.astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.981458Z","iopub.execute_input":"2022-07-06T18:15:33.981799Z","iopub.status.idle":"2022-07-06T18:15:33.989406Z","shell.execute_reply.started":"2022-07-06T18:15:33.981770Z","shell.execute_reply":"2022-07-06T18:15:33.988368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data=titanic_ds,x=\"Ticket\")\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:33.990870Z","iopub.execute_input":"2022-07-06T18:15:33.991617Z","iopub.status.idle":"2022-07-06T18:15:34.338825Z","shell.execute_reply.started":"2022-07-06T18:15:33.991535Z","shell.execute_reply":"2022-07-06T18:15:34.337656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.Cabin=titanic_ds.Cabin.apply(lambda x: x[0] if pd.isnull(x)==False else np.nan)\ntitanic_ds.Cabin","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.339931Z","iopub.execute_input":"2022-07-06T18:15:34.340236Z","iopub.status.idle":"2022-07-06T18:15:34.350218Z","shell.execute_reply.started":"2022-07-06T18:15:34.340210Z","shell.execute_reply":"2022-07-06T18:15:34.348986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.groupby([\"Ticket\"]).Cabin.agg(pd.Series.mode)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.351860Z","iopub.execute_input":"2022-07-06T18:15:34.352791Z","iopub.status.idle":"2022-07-06T18:15:34.455174Z","shell.execute_reply.started":"2022-07-06T18:15:34.352745Z","shell.execute_reply":"2022-07-06T18:15:34.454079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diff=titanic_ds.Cabin.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.456639Z","iopub.execute_input":"2022-07-06T18:15:34.456961Z","iopub.status.idle":"2022-07-06T18:15:34.461856Z","shell.execute_reply.started":"2022-07-06T18:15:34.456932Z","shell.execute_reply":"2022-07-06T18:15:34.460941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in diff[1:]:\n    print(i,\" \",titanic_ds[titanic_ds.Cabin==i].Fare.median())","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.463227Z","iopub.execute_input":"2022-07-06T18:15:34.463537Z","iopub.status.idle":"2022-07-06T18:15:34.484407Z","shell.execute_reply.started":"2022-07-06T18:15:34.463508Z","shell.execute_reply":"2022-07-06T18:15:34.483505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.drop(\"Cabin\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.485658Z","iopub.execute_input":"2022-07-06T18:15:34.486694Z","iopub.status.idle":"2022-07-06T18:15:34.492432Z","shell.execute_reply.started":"2022-07-06T18:15:34.486654Z","shell.execute_reply":"2022-07-06T18:15:34.491189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.493849Z","iopub.execute_input":"2022-07-06T18:15:34.494548Z","iopub.status.idle":"2022-07-06T18:15:34.520335Z","shell.execute_reply.started":"2022-07-06T18:15:34.494512Z","shell.execute_reply":"2022-07-06T18:15:34.519540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds[\"Title\"]=titanic_ds.Name.str.split(\",\").apply(lambda x:x[1].split(\".\")[0])\ntitanic_ds.drop(\"Name\",axis=1,inplace=True)\ntitanic_ds","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.521404Z","iopub.execute_input":"2022-07-06T18:15:34.522367Z","iopub.status.idle":"2022-07-06T18:15:34.551731Z","shell.execute_reply.started":"2022-07-06T18:15:34.522330Z","shell.execute_reply":"2022-07-06T18:15:34.550647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"people_with_same_ticket=titanic_ds.groupby([\"Ticket\"]).count().Survived\npeople_with_same_ticket","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.552921Z","iopub.execute_input":"2022-07-06T18:15:34.553380Z","iopub.status.idle":"2022-07-06T18:15:34.569077Z","shell.execute_reply.started":"2022-07-06T18:15:34.553351Z","shell.execute_reply":"2022-07-06T18:15:34.567818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds[\"same_ticket\"]=titanic_ds.Ticket.apply(lambda x:people_with_same_ticket[x])\ntitanic_ds","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.570393Z","iopub.execute_input":"2022-07-06T18:15:34.571110Z","iopub.status.idle":"2022-07-06T18:15:34.596495Z","shell.execute_reply.started":"2022-07-06T18:15:34.571066Z","shell.execute_reply":"2022-07-06T18:15:34.595606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds[\"harmonic_mean\"]=titanic_ds.SibSp.apply(lambda x : harmonic_mean([x,titanic_ds.Parch[x]]))\ntitanic_ds","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.597757Z","iopub.execute_input":"2022-07-06T18:15:34.598110Z","iopub.status.idle":"2022-07-06T18:15:34.637255Z","shell.execute_reply.started":"2022-07-06T18:15:34.598080Z","shell.execute_reply":"2022-07-06T18:15:34.635673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_ds.corr().Survived.sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.639138Z","iopub.execute_input":"2022-07-06T18:15:34.639500Z","iopub.status.idle":"2022-07-06T18:15:34.651312Z","shell.execute_reply.started":"2022-07-06T18:15:34.639467Z","shell.execute_reply":"2022-07-06T18:15:34.650515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_y=titanic_ds.Survived.copy()\ntitanic_X=titanic_ds.drop(\"Survived\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.652500Z","iopub.execute_input":"2022-07-06T18:15:34.653624Z","iopub.status.idle":"2022-07-06T18:15:34.660317Z","shell.execute_reply.started":"2022-07-06T18:15:34.653555Z","shell.execute_reply":"2022-07-06T18:15:34.659534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_X.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.661490Z","iopub.execute_input":"2022-07-06T18:15:34.662022Z","iopub.status.idle":"2022-07-06T18:15:34.679582Z","shell.execute_reply.started":"2022-07-06T18:15:34.661993Z","shell.execute_reply":"2022-07-06T18:15:34.678386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_attributes=[\"Pclass\",\"Sex\",\"Embarked\",\"Title\"]\nnum_attributes=[\"Age\",\"SibSp\",\"Parch\",\"Ticket\",\"Fare\",\"same_ticket\",\"harmonic_mean\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.680959Z","iopub.execute_input":"2022-07-06T18:15:34.681315Z","iopub.status.idle":"2022-07-06T18:15:34.692289Z","shell.execute_reply.started":"2022-07-06T18:15:34.681280Z","shell.execute_reply":"2022-07-06T18:15:34.691078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_X[cat_attributes]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.693866Z","iopub.execute_input":"2022-07-06T18:15:34.694323Z","iopub.status.idle":"2022-07-06T18:15:34.715094Z","shell.execute_reply.started":"2022-07-06T18:15:34.694287Z","shell.execute_reply":"2022-07-06T18:15:34.713789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_X[num_attributes]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.723745Z","iopub.execute_input":"2022-07-06T18:15:34.724137Z","iopub.status.idle":"2022-07-06T18:15:34.744240Z","shell.execute_reply.started":"2022-07-06T18:15:34.724102Z","shell.execute_reply":"2022-07-06T18:15:34.743389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_num=titanic_X[num_attributes]\ntitanic_cat=titanic_X[cat_attributes]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.745152Z","iopub.execute_input":"2022-07-06T18:15:34.745461Z","iopub.status.idle":"2022-07-06T18:15:34.757847Z","shell.execute_reply.started":"2022-07-06T18:15:34.745432Z","shell.execute_reply":"2022-07-06T18:15:34.756587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_pipeline = Pipeline([\n        ('imputer', SimpleImputer(strategy=\"median\")),\n        ('std_scaler', StandardScaler()),\n    ])\nfull_pipeline = ColumnTransformer([\n        (\"num\", num_pipeline, num_attributes),\n        (\"cat\", OneHotEncoder(handle_unknown='ignore'), cat_attributes),\n    ])","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.759649Z","iopub.execute_input":"2022-07-06T18:15:34.760005Z","iopub.status.idle":"2022-07-06T18:15:34.770732Z","shell.execute_reply.started":"2022-07-06T18:15:34.759965Z","shell.execute_reply":"2022-07-06T18:15:34.769896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_X_prep=full_pipeline.fit_transform(titanic_X)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.772156Z","iopub.execute_input":"2022-07-06T18:15:34.772475Z","iopub.status.idle":"2022-07-06T18:15:34.800025Z","shell.execute_reply.started":"2022-07-06T18:15:34.772447Z","shell.execute_reply":"2022-07-06T18:15:34.798670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_X_prep","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.801399Z","iopub.execute_input":"2022-07-06T18:15:34.801749Z","iopub.status.idle":"2022-07-06T18:15:34.809734Z","shell.execute_reply.started":"2022-07-06T18:15:34.801720Z","shell.execute_reply":"2022-07-06T18:15:34.808858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_scores_f1(scores):\n    print(\"F1 Scores:\", scores)\n    print(\"F1 Mean:\", scores.mean())\n    print(\"F1 Standard deviation:\", scores.std())\n    \ndef display_scores_auc(scores):\n    print(\"AUC Scores:\", scores)\n    print(\"AUC Mean:\", scores.mean())\n    print(\"AUC Standard deviation:\", scores.std())\n    \ndef display_scores(scores):\n    print(\"Accuracy:\", scores)\n    print(\"Mean:\", scores.mean())\n    print(\"Standard deviation:\", scores.std())\n    \n    \ndef plot_precision_vs_recall(precisions, recalls,axis):\n    axis[0].plot(recalls, precisions, \"b-\", linewidth=2)\n    axis[0].set_xlabel(\"Recall\", fontsize=16)\n    axis[0].set_ylabel(\"Precision\", fontsize=16)\n    axis[0].axis([0, 1, 0, 1])\n    axis[0].grid(True)\n    \ndef plot_roc_curve(fpr, tpr,axis, label=None):\n    axis[1].plot(fpr, tpr, linewidth=2, label=label)\n    axis[1].plot([0, 1], [0, 1], 'k--') \n    axis[1].axis([0, 1, 0, 1])                                    \n    axis[1].set_xlabel('False Positive Rate (Fall-Out)', fontsize=16) \n    axis[1].set_ylabel('True Positive Rate (Recall)', fontsize=16)    \n    axis[1].grid(True)                                            \n\ndef scoring_metrics(classifier):\n    figure, axis = plt.subplots(1, 2,figsize=(15,10))\n    scores_auc=cross_val_score(classifier,titanic_X_prep,titanic_y,cv=5,scoring=\"roc_auc\")\n    scores_f1=cross_val_score(classifier,titanic_X_prep,titanic_y,cv=5,scoring=\"f1\")\n    scores=cross_val_score(classifier,titanic_X_prep,titanic_y,cv=5,scoring=\"accuracy\")\n    display_scores(scores)\n    display_scores_auc(scores_auc)\n    display_scores_f1(scores_f1)\n    \n    \n    y_scores=cross_val_predict(classifier,titanic_X_prep,titanic_y,cv=5,method=\"predict_proba\")\n    y_scores=y_scores[:,1]\n    precisions, recalls, thresholds=precision_recall_curve(titanic_y,y_scores)\n    fpr, tpr, thresholds = roc_curve(titanic_y, y_scores)\n    plot_precision_vs_recall(precisions,recalls,axis)\n    plot_roc_curve(fpr,tpr,axis,thresholds)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.810889Z","iopub.execute_input":"2022-07-06T18:15:34.811212Z","iopub.status.idle":"2022-07-06T18:15:34.826829Z","shell.execute_reply.started":"2022-07-06T18:15:34.811184Z","shell.execute_reply":"2022-07-06T18:15:34.825665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nscoring_metrics(knn)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:34.828491Z","iopub.execute_input":"2022-07-06T18:15:34.828947Z","iopub.status.idle":"2022-07-06T18:15:35.411888Z","shell.execute_reply.started":"2022-07-06T18:15:34.828905Z","shell.execute_reply":"2022-07-06T18:15:35.410600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr= LogisticRegression()\nscoring_metrics(lr)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:35.413769Z","iopub.execute_input":"2022-07-06T18:15:35.414466Z","iopub.status.idle":"2022-07-06T18:15:36.344227Z","shell.execute_reply.started":"2022-07-06T18:15:35.414421Z","shell.execute_reply":"2022-07-06T18:15:36.343066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt=DecisionTreeClassifier()\nscoring_metrics(dt)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:36.345753Z","iopub.execute_input":"2022-07-06T18:15:36.346084Z","iopub.status.idle":"2022-07-06T18:15:36.736472Z","shell.execute_reply.started":"2022-07-06T18:15:36.346053Z","shell.execute_reply":"2022-07-06T18:15:36.735231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf=RandomForestClassifier()\nscoring_metrics(rf)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:36.738284Z","iopub.execute_input":"2022-07-06T18:15:36.738731Z","iopub.status.idle":"2022-07-06T18:15:40.637949Z","shell.execute_reply.started":"2022-07-06T18:15:36.738689Z","shell.execute_reply":"2022-07-06T18:15:40.636827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm=SVC(probability=True)\nscoring_metrics(svm)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:40.639418Z","iopub.execute_input":"2022-07-06T18:15:40.639855Z","iopub.status.idle":"2022-07-06T18:15:42.848646Z","shell.execute_reply.started":"2022-07-06T18:15:40.639812Z","shell.execute_reply":"2022-07-06T18:15:42.847354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gb=GradientBoostingClassifier()\nscoring_metrics(gb)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:42.850055Z","iopub.execute_input":"2022-07-06T18:15:42.850374Z","iopub.status.idle":"2022-07-06T18:15:45.959106Z","shell.execute_reply.started":"2022-07-06T18:15:42.850346Z","shell.execute_reply":"2022-07-06T18:15:45.957773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#gradient boosting seems like the best model so let's tune it\nfrom sklearn.model_selection import RandomizedSearchCV\nn_estimators = [100, 500, 900, 1100, 1500]\nmax_depth = [2, 3, 5, 10, 15]\nmin_samples_leaf = [1, 2, 4, 6, 8] \nmin_samples_split = [2, 4, 6, 10]\nmax_features = ['auto', 'sqrt', 'log2', None]\nparam_grid = {\n    'n_estimators': n_estimators,\n    'max_depth': max_depth,\n    'min_samples_leaf': min_samples_leaf,\n    'min_samples_split': min_samples_split,\n    'max_features': max_features}\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:45.960911Z","iopub.execute_input":"2022-07-06T18:15:45.961666Z","iopub.status.idle":"2022-07-06T18:15:45.971134Z","shell.execute_reply.started":"2022-07-06T18:15:45.961618Z","shell.execute_reply":"2022-07-06T18:15:45.969820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gb_clf=GradientBoostingClassifier()\nrnd_search=RandomizedSearchCV(gb_clf,param_distributions=param_grid,cv=5,scoring='accuracy')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:45.972873Z","iopub.execute_input":"2022-07-06T18:15:45.973746Z","iopub.status.idle":"2022-07-06T18:15:45.981808Z","shell.execute_reply.started":"2022-07-06T18:15:45.973696Z","shell.execute_reply":"2022-07-06T18:15:45.980757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rnd_search.fit(titanic_X_prep,titanic_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:15:45.983324Z","iopub.execute_input":"2022-07-06T18:15:45.984091Z","iopub.status.idle":"2022-07-06T18:16:45.693614Z","shell.execute_reply.started":"2022-07-06T18:15:45.984049Z","shell.execute_reply":"2022-07-06T18:16:45.692367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cvres = rnd_search.cv_results_\nfor mean_score, params in zip(cvres[\"mean_test_score\"], cvres[\"params\"]):\n    print(mean_score, params)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:45.695000Z","iopub.execute_input":"2022-07-06T18:16:45.695333Z","iopub.status.idle":"2022-07-06T18:16:45.701812Z","shell.execute_reply.started":"2022-07-06T18:16:45.695303Z","shell.execute_reply":"2022-07-06T18:16:45.700451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rnd_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:45.702996Z","iopub.execute_input":"2022-07-06T18:16:45.703320Z","iopub.status.idle":"2022-07-06T18:16:45.718479Z","shell.execute_reply.started":"2022-07-06T18:16:45.703291Z","shell.execute_reply":"2022-07-06T18:16:45.717182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scoring_metrics(rnd_search.best_estimator_)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:45.720793Z","iopub.execute_input":"2022-07-06T18:16:45.721261Z","iopub.status.idle":"2022-07-06T18:16:56.199399Z","shell.execute_reply.started":"2022-07-06T18:16:45.721227Z","shell.execute_reply":"2022-07-06T18:16:56.198519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"attributes=[]\nfor i in full_pipeline.named_transformers_[\"cat\"].categories_:\n    attributes+=list(i)\nattributes=num_attributes+attributes","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:56.200644Z","iopub.execute_input":"2022-07-06T18:16:56.201459Z","iopub.status.idle":"2022-07-06T18:16:56.206409Z","shell.execute_reply.started":"2022-07-06T18:16:56.201425Z","shell.execute_reply":"2022-07-06T18:16:56.205063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_importances= rnd_search.best_estimator_.feature_importances_\nfeature_importances","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:56.208022Z","iopub.execute_input":"2022-07-06T18:16:56.208526Z","iopub.status.idle":"2022-07-06T18:16:56.225420Z","shell.execute_reply.started":"2022-07-06T18:16:56.208482Z","shell.execute_reply":"2022-07-06T18:16:56.224219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Most important features\nsorted(zip(feature_importances, attributes), reverse=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:56.227467Z","iopub.execute_input":"2022-07-06T18:16:56.228777Z","iopub.status.idle":"2022-07-06T18:16:56.238299Z","shell.execute_reply.started":"2022-07-06T18:16:56.228725Z","shell.execute_reply":"2022-07-06T18:16:56.237501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model = rnd_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:56.239477Z","iopub.execute_input":"2022-07-06T18:16:56.240062Z","iopub.status.idle":"2022-07-06T18:16:56.249064Z","shell.execute_reply.started":"2022-07-06T18:16:56.240029Z","shell.execute_reply":"2022-07-06T18:16:56.248134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred=cross_val_predict(final_model,titanic_X_prep,titanic_y,cv=3)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:56.250394Z","iopub.execute_input":"2022-07-06T18:16:56.251045Z","iopub.status.idle":"2022-07-06T18:16:57.598737Z","shell.execute_reply.started":"2022-07-06T18:16:56.251005Z","shell.execute_reply":"2022-07-06T18:16:57.597461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"con_max=confusion_matrix(titanic_y,y_train_pred)\ncon_max","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.600786Z","iopub.execute_input":"2022-07-06T18:16:57.601249Z","iopub.status.idle":"2022-07-06T18:16:57.610901Z","shell.execute_reply.started":"2022-07-06T18:16:57.601203Z","shell.execute_reply":"2022-07-06T18:16:57.610093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(con_max)\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.611986Z","iopub.execute_input":"2022-07-06T18:16:57.612866Z","iopub.status.idle":"2022-07-06T18:16:57.819709Z","shell.execute_reply.started":"2022-07-06T18:16:57.612821Z","shell.execute_reply":"2022-07-06T18:16:57.818773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test= pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ntitanic_test2=titanic_test.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.820751Z","iopub.execute_input":"2022-07-06T18:16:57.821475Z","iopub.status.idle":"2022-07-06T18:16:57.834543Z","shell.execute_reply.started":"2022-07-06T18:16:57.821438Z","shell.execute_reply":"2022-07-06T18:16:57.833509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.836209Z","iopub.execute_input":"2022-07-06T18:16:57.837264Z","iopub.status.idle":"2022-07-06T18:16:57.861347Z","shell.execute_reply.started":"2022-07-06T18:16:57.837220Z","shell.execute_reply":"2022-07-06T18:16:57.860093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.864407Z","iopub.execute_input":"2022-07-06T18:16:57.864801Z","iopub.status.idle":"2022-07-06T18:16:57.878961Z","shell.execute_reply.started":"2022-07-06T18:16:57.864769Z","shell.execute_reply":"2022-07-06T18:16:57.878052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test.Ticket=titanic_test.Ticket.str.split(\" \").apply(lambda x: x[-1] if len(x)>1 else x[0])\ntitanic_test.Ticket=titanic_test.Ticket.apply(lambda x: \"0\" if x==\"LINE\" else x)\ntitanic_ds.Ticket=titanic_ds.Ticket.astype(int)\ntitanic_test[\"Ticket\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.880276Z","iopub.execute_input":"2022-07-06T18:16:57.881179Z","iopub.status.idle":"2022-07-06T18:16:57.892969Z","shell.execute_reply.started":"2022-07-06T18:16:57.881147Z","shell.execute_reply":"2022-07-06T18:16:57.892081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test.drop(\"PassengerId\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.894480Z","iopub.execute_input":"2022-07-06T18:16:57.894931Z","iopub.status.idle":"2022-07-06T18:16:57.904082Z","shell.execute_reply.started":"2022-07-06T18:16:57.894901Z","shell.execute_reply":"2022-07-06T18:16:57.902827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test.drop(\"Cabin\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.905924Z","iopub.execute_input":"2022-07-06T18:16:57.906721Z","iopub.status.idle":"2022-07-06T18:16:57.915181Z","shell.execute_reply.started":"2022-07-06T18:16:57.906685Z","shell.execute_reply":"2022-07-06T18:16:57.914060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test[\"Title\"]=titanic_test.Name.str.split(\",\").apply(lambda x:x[1].split(\".\")[0]) \ntitanic_test.drop(\"Name\",axis=1,inplace=True)\ntitanic_test","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.916636Z","iopub.execute_input":"2022-07-06T18:16:57.917236Z","iopub.status.idle":"2022-07-06T18:16:57.945150Z","shell.execute_reply.started":"2022-07-06T18:16:57.917202Z","shell.execute_reply":"2022-07-06T18:16:57.944165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"people_with_same_ticket=titanic_test.groupby([\"Ticket\"]).count().Embarked\ntitanic_test[\"same_ticket\"]=titanic_test.Ticket.apply(lambda x:people_with_same_ticket[x])\ntitanic_test","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.946437Z","iopub.execute_input":"2022-07-06T18:16:57.946985Z","iopub.status.idle":"2022-07-06T18:16:57.973691Z","shell.execute_reply.started":"2022-07-06T18:16:57.946952Z","shell.execute_reply":"2022-07-06T18:16:57.972510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic_test[\"harmonic_mean\"]=titanic_test.SibSp.apply(lambda x : harmonic_mean([x,titanic_test.Parch[x]]))\ntitanic_test","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:57.974953Z","iopub.execute_input":"2022-07-06T18:16:57.976504Z","iopub.status.idle":"2022-07-06T18:16:58.009024Z","shell.execute_reply.started":"2022-07-06T18:16:57.976468Z","shell.execute_reply":"2022-07-06T18:16:58.007762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_prep=full_pipeline.transform(titanic_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:58.010239Z","iopub.execute_input":"2022-07-06T18:16:58.010766Z","iopub.status.idle":"2022-07-06T18:16:58.025018Z","shell.execute_reply.started":"2022-07-06T18:16:58.010731Z","shell.execute_reply":"2022-07-06T18:16:58.023693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predictions= final_model.predict(X_test_prep)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:58.026395Z","iopub.execute_input":"2022-07-06T18:16:58.027665Z","iopub.status.idle":"2022-07-06T18:16:58.037474Z","shell.execute_reply.started":"2022-07-06T18:16:58.027605Z","shell.execute_reply":"2022-07-06T18:16:58.036314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predictions","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:58.039136Z","iopub.execute_input":"2022-07-06T18:16:58.039991Z","iopub.status.idle":"2022-07-06T18:16:58.049305Z","shell.execute_reply.started":"2022-07-06T18:16:58.039949Z","shell.execute_reply":"2022-07-06T18:16:58.048157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_solution=titanic_test2[[\"PassengerId\"]]\nfinal_solution[\"Survived\"]=final_predictions","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:58.050785Z","iopub.execute_input":"2022-07-06T18:16:58.051930Z","iopub.status.idle":"2022-07-06T18:16:58.063926Z","shell.execute_reply.started":"2022-07-06T18:16:58.051881Z","shell.execute_reply":"2022-07-06T18:16:58.062665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_solution","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:16:58.066062Z","iopub.execute_input":"2022-07-06T18:16:58.066496Z","iopub.status.idle":"2022-07-06T18:16:58.081237Z","shell.execute_reply.started":"2022-07-06T18:16:58.066454Z","shell.execute_reply":"2022-07-06T18:16:58.079972Z"},"trusted":true},"execution_count":null,"outputs":[]}]}