{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-03T17:10:35.224122Z","iopub.execute_input":"2022-08-03T17:10:35.224584Z","iopub.status.idle":"2022-08-03T17:10:35.256560Z","shell.execute_reply.started":"2022-08-03T17:10:35.224494Z","shell.execute_reply":"2022-08-03T17:10:35.255792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=pd.read_csv('../input/titanic/train.csv')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:46:41.685070Z","iopub.execute_input":"2022-08-03T17:46:41.685497Z","iopub.status.idle":"2022-08-03T17:46:41.735608Z","shell.execute_reply.started":"2022-08-03T17:46:41.685465Z","shell.execute_reply":"2022-08-03T17:46:41.734381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:46:46.856091Z","iopub.execute_input":"2022-08-03T17:46:46.856458Z","iopub.status.idle":"2022-08-03T17:46:46.883947Z","shell.execute_reply.started":"2022-08-03T17:46:46.856431Z","shell.execute_reply":"2022-08-03T17:46:46.882795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:46:52.549866Z","iopub.execute_input":"2022-08-03T17:46:52.550225Z","iopub.status.idle":"2022-08-03T17:46:52.560124Z","shell.execute_reply.started":"2022-08-03T17:46:52.550196Z","shell.execute_reply":"2022-08-03T17:46:52.558916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:46:55.725109Z","iopub.execute_input":"2022-08-03T17:46:55.725468Z","iopub.status.idle":"2022-08-03T17:46:56.239190Z","shell.execute_reply.started":"2022-08-03T17:46:55.725440Z","shell.execute_reply":"2022-08-03T17:46:56.238155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_data.isna(),cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:46:58.890900Z","iopub.execute_input":"2022-08-03T17:46:58.891285Z","iopub.status.idle":"2022-08-03T17:46:59.219349Z","shell.execute_reply.started":"2022-08-03T17:46:58.891256Z","shell.execute_reply":"2022-08-03T17:46:59.218181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def val_count(df):\n    for i in df.columns:\n        if i not in ['PassengerId','Name']:\n            value=df[i].value_counts()\n            print(value)\n            print('*'*20)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:47:05.644796Z","iopub.execute_input":"2022-08-03T17:47:05.645188Z","iopub.status.idle":"2022-08-03T17:47:05.651168Z","shell.execute_reply.started":"2022-08-03T17:47:05.645156Z","shell.execute_reply":"2022-08-03T17:47:05.650079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_count(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:47:13.940491Z","iopub.execute_input":"2022-08-03T17:47:13.940936Z","iopub.status.idle":"2022-08-03T17:47:13.961021Z","shell.execute_reply.started":"2022-08-03T17:47:13.940892Z","shell.execute_reply":"2022-08-03T17:47:13.959943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_data.corr(),annot=True,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:47:24.076779Z","iopub.execute_input":"2022-08-03T17:47:24.077168Z","iopub.status.idle":"2022-08-03T17:47:24.490648Z","shell.execute_reply.started":"2022-08-03T17:47:24.077137Z","shell.execute_reply":"2022-08-03T17:47:24.489639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,8))\nsns.histplot(train_data.Age,kde=True,color='green')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:47:31.041932Z","iopub.execute_input":"2022-08-03T17:47:31.042333Z","iopub.status.idle":"2022-08-03T17:47:31.346965Z","shell.execute_reply.started":"2022-08-03T17:47:31.042302Z","shell.execute_reply":"2022-08-03T17:47:31.345784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax=plt.subplots(2,2,figsize=(12,10))\nsns.countplot(train_data.Survived,hue=train_data.Sex,ax=ax[0,0])\nsns.countplot(train_data.Sex,hue=train_data.Pclass,ax=ax[0,1],palette='Set2')\nsns.countplot(train_data.Pclass,hue=train_data.Survived,ax=ax[1,0],palette='Blues_d')\nax[1,1]=sns.countplot(train_data.Embarked,hue=train_data.Survived,palette='PuRd')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:47:37.286814Z","iopub.execute_input":"2022-08-03T17:47:37.287230Z","iopub.status.idle":"2022-08-03T17:47:38.010285Z","shell.execute_reply.started":"2022-08-03T17:47:37.287199Z","shell.execute_reply":"2022-08-03T17:47:38.009425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Female survived more than men on titanic but members of `Pclass 1` survived more than other class members(rich people survived more).\n\nPeople who were on `Embarked C` have more chance of survival than people on other embarked","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\nsns.histplot(train_data.Fare);","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:47:48.859411Z","iopub.execute_input":"2022-08-03T17:47:48.859865Z","iopub.status.idle":"2022-08-03T17:47:49.243407Z","shell.execute_reply.started":"2022-08-03T17:47:48.859831Z","shell.execute_reply":"2022-08-03T17:47:49.242241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are three clusters of fare btetween 0-100,100-200 and 200-300.Value of fare above 500 (only three rows) seems to be an outlier so remove this data from dataset.","metadata":{}},{"cell_type":"code","source":"train_data.drop(train_data[train_data.Fare>500].index,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:47:57.563117Z","iopub.execute_input":"2022-08-03T17:47:57.563978Z","iopub.status.idle":"2022-08-03T17:47:57.570404Z","shell.execute_reply.started":"2022-08-03T17:47:57.563938Z","shell.execute_reply":"2022-08-03T17:47:57.569442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Feature Engineering and missing value","metadata":{}},{"cell_type":"code","source":"train_data['title']=train_data['Name'].str.split(',',expand=True)[1]\ntrain_data['title']=train_data['title'].str.split('.',expand=True)[0]\ntrain_data.title.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:00.780065Z","iopub.execute_input":"2022-08-03T17:48:00.780809Z","iopub.status.idle":"2022-08-03T17:48:00.796206Z","shell.execute_reply.started":"2022-08-03T17:48:00.780768Z","shell.execute_reply":"2022-08-03T17:48:00.795160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.title.replace(['Dr','Rev','Mlle',\"Major\",'Col','Capt','Sir','Don','Jonkheer'],'Mr',regex=True,inplace=True)\ntrain_data.title.replace(['the Countess','Ms','Lady','Mme'],'Miss',regex=True,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:06.674540Z","iopub.execute_input":"2022-08-03T17:48:06.675782Z","iopub.status.idle":"2022-08-03T17:48:06.701992Z","shell.execute_reply.started":"2022-08-03T17:48:06.675710Z","shell.execute_reply":"2022-08-03T17:48:06.701253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.title.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:09.939752Z","iopub.execute_input":"2022-08-03T17:48:09.940557Z","iopub.status.idle":"2022-08-03T17:48:09.948071Z","shell.execute_reply.started":"2022-08-03T17:48:09.940525Z","shell.execute_reply":"2022-08-03T17:48:09.947202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['sec']=train_data['Cabin'].str[0]\ntrain_data.sec.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:15.891635Z","iopub.execute_input":"2022-08-03T17:48:15.892624Z","iopub.status.idle":"2022-08-03T17:48:15.903290Z","shell.execute_reply.started":"2022-08-03T17:48:15.892580Z","shell.execute_reply":"2022-08-03T17:48:15.902166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.sec.fillna('other',inplace=True)\n\n# To avoid mismatch in no. of columns in test and train set we replace sec T with other as their is no sec T in test data\ntrain_data.replace('T','other',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:18.765670Z","iopub.execute_input":"2022-08-03T17:48:18.766452Z","iopub.status.idle":"2022-08-03T17:48:18.775308Z","shell.execute_reply.started":"2022-08-03T17:48:18.766403Z","shell.execute_reply":"2022-08-03T17:48:18.773829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['Embarked'].fillna('S',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:22.546413Z","iopub.execute_input":"2022-08-03T17:48:22.546833Z","iopub.status.idle":"2022-08-03T17:48:22.552465Z","shell.execute_reply.started":"2022-08-03T17:48:22.546781Z","shell.execute_reply":"2022-08-03T17:48:22.551780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fill the missing age of men and women by their respective median values\ntrain_data.loc[train_data['Sex']=='female','Age']=train_data.loc[train_data['Sex']=='female','Age'].fillna(train_data.Age.median())\ntrain_data.loc[train_data['Sex']=='male','Age']=train_data.loc[train_data['Sex']=='male','Age'].fillna(train_data.Age.median())","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:25.822601Z","iopub.execute_input":"2022-08-03T17:48:25.823759Z","iopub.status.idle":"2022-08-03T17:48:25.834501Z","shell.execute_reply.started":"2022-08-03T17:48:25.823694Z","shell.execute_reply":"2022-08-03T17:48:25.833785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:48:28.591581Z","iopub.execute_input":"2022-08-03T17:48:28.592011Z","iopub.status.idle":"2022-08-03T17:48:28.602961Z","shell.execute_reply.started":"2022-08-03T17:48:28.591979Z","shell.execute_reply":"2022-08-03T17:48:28.601885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Modelling,Evaluation and Parameter Tuning\n\nSplit the data into training and validation set.Training set is used to train the model(learn patterns from data) and validation set to evaluate the model and tuning the model.\n\nEvaluation is based on accuracy of model(how many times model predicted correct value)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:02:38.657904Z","iopub.execute_input":"2022-06-22T18:02:38.658259Z","iopub.status.idle":"2022-06-22T18:02:38.662568Z","shell.execute_reply.started":"2022-06-22T18:02:38.658229Z","shell.execute_reply":"2022-06-22T18:02:38.661078Z"}}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:49:47.460298Z","iopub.execute_input":"2022-08-03T17:49:47.460708Z","iopub.status.idle":"2022-08-03T17:49:47.520834Z","shell.execute_reply.started":"2022-08-03T17:49:47.460675Z","shell.execute_reply":"2022-08-03T17:49:47.519804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train_data.drop(['PassengerId','Survived','Name','Ticket','Cabin'],axis=1)\ny=train_data['Survived']\nX=pd.get_dummies(X)\nnp.random.seed(44)\nX_train,X_val,y_train,y_val=train_test_split(X,y,test_size=0.3)\nX_train.shape,X_val.shape,y_train.shape,y_val.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:49:50.199712Z","iopub.execute_input":"2022-08-03T17:49:50.200679Z","iopub.status.idle":"2022-08-03T17:49:50.220043Z","shell.execute_reply.started":"2022-08-03T17:49:50.200643Z","shell.execute_reply":"2022-08-03T17:49:50.219014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier,GradientBoostingClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import cross_val_score,RandomizedSearchCV,GridSearchCV\nfrom sklearn.metrics import confusion_matrix,accuracy_score,plot_roc_curve","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:49:57.367021Z","iopub.execute_input":"2022-08-03T17:49:57.367414Z","iopub.status.idle":"2022-08-03T17:49:57.463788Z","shell.execute_reply.started":"2022-08-03T17:49:57.367383Z","shell.execute_reply":"2022-08-03T17:49:57.462681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models={'linear':LogisticRegression(solver='liblinear'),\n        'support_vector':SVC(),\n       'tree':DecisionTreeClassifier(),\n       'random':RandomForestClassifier(),\n       'boosting':GradientBoostingClassifier()}","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:50:00.746960Z","iopub.execute_input":"2022-08-03T17:50:00.748097Z","iopub.status.idle":"2022-08-03T17:50:00.753264Z","shell.execute_reply.started":"2022-08-03T17:50:00.748058Z","shell.execute_reply":"2022-08-03T17:50:00.752062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# since we have to evaluate our model on classification accuracy let's check accuarcy score of model and tune the model \ndef acc_score(X_train,y_train,X_val,y_val,models):\n    scores={}\n    for name,model in models.items():\n        mod=model.fit(X_train,y_train)\n        y_pred=model.predict(X_val)\n        scores[name]=accuracy_score(y_val,y_pred)\n    return scores","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:50:27.858688Z","iopub.execute_input":"2022-08-03T17:50:27.859672Z","iopub.status.idle":"2022-08-03T17:50:27.866499Z","shell.execute_reply.started":"2022-08-03T17:50:27.859629Z","shell.execute_reply":"2022-08-03T17:50:27.864874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result1=acc_score(X_train,y_train,X_val,y_val,models)\nresult1","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:50:39.227208Z","iopub.execute_input":"2022-08-03T17:50:39.227570Z","iopub.status.idle":"2022-08-03T17:50:39.590761Z","shell.execute_reply.started":"2022-08-03T17:50:39.227543Z","shell.execute_reply":"2022-08-03T17:50:39.589801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's drop svc model as it has least score and try to tune other models for better accuracy score\ndecision_ran={'max_depth':[4,8,15],\n              'min_samples_split':[14,8,6],\n              'min_samples_leaf':[3,7,10],\n              'max_leaf_nodes':[None,4,6,10]}\nrandom_ran={'n_estimators':[100,200,400,500],\n            'max_depth':[None,5,10,17],\n            'min_samples_split':[2,6,8,10],\n            'min_samples_leaf':[1,4,6,8,12],\n            'max_leaf_nodes':[None,2,4,6,8,12]}\ngrdient_ran={'n_estimators':[200,350,550,700],\n             'min_samples_split':[6,9,4],\n             'min_samples_leaf':[1,6,9],\n             'max_depth':[3,9,12],\n             'max_features':[None,18,20],\n             'max_leaf_nodes':[None,4,6,8]}","metadata":{"execution":{"iopub.status.busy":"2022-08-01T08:55:55.372100Z","iopub.execute_input":"2022-08-01T08:55:55.372507Z","iopub.status.idle":"2022-08-01T08:55:55.381916Z","shell.execute_reply.started":"2022-08-01T08:55:55.372475Z","shell.execute_reply":"2022-08-01T08:55:55.380653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mod2=RandomizedSearchCV(DecisionTreeClassifier(),param_distributions=decision_ran,cv=8)\nmod2.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T08:43:26.752453Z","iopub.execute_input":"2022-08-01T08:43:26.752851Z","iopub.status.idle":"2022-08-01T08:43:27.245618Z","shell.execute_reply.started":"2022-08-01T08:43:26.752819Z","shell.execute_reply":"2022-08-01T08:43:27.244649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mod2.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-30T18:31:55.118599Z","iopub.execute_input":"2022-07-30T18:31:55.119012Z","iopub.status.idle":"2022-07-30T18:31:55.126052Z","shell.execute_reply.started":"2022-07-30T18:31:55.118979Z","shell.execute_reply":"2022-07-30T18:31:55.124835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rand_tune_models={'linear':LogisticRegression(solver='liblinear',max_iter=200),\n       'tree':DecisionTreeClassifier(min_samples_split=14,\n                                     min_samples_leaf= 7,\n                                     max_leaf_nodes=6,\n                                     max_depth=15),\n       'random':RandomForestClassifier(n_estimators=200,\n                                         min_samples_split=8,\n                                         min_samples_leaf=6,\n                                         max_depth=10,max_leaf_nodes=12),\n       'boosting':GradientBoostingClassifier(n_estimators=550,\n                                             min_samples_split=6,\n                                             min_samples_leaf=9,\n                                             max_leaf_nodes=4,\n                                             max_features=18,\n                                             max_depth=9)}","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:51:02.190347Z","iopub.execute_input":"2022-08-03T17:51:02.191043Z","iopub.status.idle":"2022-08-03T17:51:02.197464Z","shell.execute_reply.started":"2022-08-03T17:51:02.191002Z","shell.execute_reply":"2022-08-03T17:51:02.196599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result2=acc_score(X_train,y_train,X_val,y_val,rand_tune_models)\nresult2","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:51:08.418628Z","iopub.execute_input":"2022-08-03T17:51:08.419016Z","iopub.status.idle":"2022-08-03T17:51:09.267535Z","shell.execute_reply.started":"2022-08-03T17:51:08.418989Z","shell.execute_reply":"2022-08-03T17:51:09.266361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's drop linear model as it's score did not improve and further try to improve other models by grid search.\n\n> Accuracy score of decision tree,random forest and gradient boosting is increased","metadata":{"execution":{"iopub.status.busy":"2022-07-30T18:48:26.320988Z","iopub.execute_input":"2022-07-30T18:48:26.321358Z","iopub.status.idle":"2022-07-30T18:48:26.326316Z","shell.execute_reply.started":"2022-07-30T18:48:26.321329Z","shell.execute_reply":"2022-07-30T18:48:26.325153Z"}}},{"cell_type":"code","source":"decision_grid={'max_depth':[7,14,18],\n              'min_samples_split':[6,12,18],\n              'min_samples_leaf':[3,15,9],\n              'max_leaf_nodes':[None,4,7]}\nforest_grid={'n_estimators':[100,300,500],\n            'max_depth':[5,11],\n            'min_samples_split':[2,5,10],\n            'min_samples_leaf':[1,10,4],\n            'max_leaf_nodes':[None,4,6]}\ngrdient_grid={'n_estimators':[1000,300,700],\n             'min_samples_split':[28,6,17],\n             'min_samples_leaf':[25,9,14],\n             'max_depth':[9,12,4],\n             'max_leaf_nodes':[15,4,8]}","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:03:02.824534Z","iopub.execute_input":"2022-08-01T18:03:02.826031Z","iopub.status.idle":"2022-08-01T18:03:02.834664Z","shell.execute_reply.started":"2022-08-01T18:03:02.825967Z","shell.execute_reply":"2022-08-01T18:03:02.833634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# search best parameters for other models also\nmd1=GridSearchCV(DecisionTreeClassifier(),param_grid=decision_grid,cv=8)\nmd1.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:35:33.484125Z","iopub.execute_input":"2022-08-01T16:35:33.484518Z","iopub.status.idle":"2022-08-01T16:35:37.075062Z","shell.execute_reply.started":"2022-08-01T16:35:33.484487Z","shell.execute_reply":"2022-08-01T16:35:37.074205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"md1.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:35:40.748214Z","iopub.execute_input":"2022-08-01T16:35:40.748659Z","iopub.status.idle":"2022-08-01T16:35:40.755657Z","shell.execute_reply.started":"2022-08-01T16:35:40.748620Z","shell.execute_reply":"2022-08-01T16:35:40.754345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_tune_models={'tree':DecisionTreeClassifier(min_samples_split=6,\n                                     min_samples_leaf= 3,\n                                     max_leaf_nodes=7,\n                                     max_depth=15),\n       'random':RandomForestClassifier(max_depth=5,\n                                     max_leaf_nodes=None,\n                                     min_samples_leaf=1,\n                                     min_samples_split=10,\n                                     n_estimators=500),\n       'boosting':GradientBoostingClassifier(n_estimators=300,\n                                             min_samples_split=28,\n                                             min_samples_leaf=25,\n                                             max_leaf_nodes=4,\n                                             max_depth=9)}","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:52:44.443422Z","iopub.execute_input":"2022-08-03T17:52:44.443863Z","iopub.status.idle":"2022-08-03T17:52:44.450547Z","shell.execute_reply.started":"2022-08-03T17:52:44.443829Z","shell.execute_reply":"2022-08-03T17:52:44.449808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result3=acc_score(X_train,y_train,X_val,y_val,grid_tune_models)\nresult3","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:52:51.040430Z","iopub.execute_input":"2022-08-03T17:52:51.040847Z","iopub.status.idle":"2022-08-03T17:52:52.150596Z","shell.execute_reply.started":"2022-08-03T17:52:51.040815Z","shell.execute_reply":"2022-08-03T17:52:52.149582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Score for decision tree increased little but decreased for gradient boosting and remain same for random forest.\n\nSince decision tree and random forest have same score`85%`. Let's check both models for AUC and select model for prediction of test data.","metadata":{}},{"cell_type":"code","source":"eval1=RandomForestClassifier(max_depth=5,\n                             max_leaf_nodes=None,\n                             min_samples_leaf=1,\n                             min_samples_split=10,\n                             n_estimators=500).fit(X_train,y_train)\npred1=eval1.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:53:17.046280Z","iopub.execute_input":"2022-08-03T17:53:17.046665Z","iopub.status.idle":"2022-08-03T17:53:17.911682Z","shell.execute_reply.started":"2022-08-03T17:53:17.046636Z","shell.execute_reply":"2022-08-03T17:53:17.910703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(y_val,pred1))\nprint(accuracy_score(y_val,pred1))","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:53:21.046884Z","iopub.execute_input":"2022-08-03T17:53:21.047243Z","iopub.status.idle":"2022-08-03T17:53:21.055658Z","shell.execute_reply.started":"2022-08-03T17:53:21.047215Z","shell.execute_reply":"2022-08-03T17:53:21.054760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc_curve(eval1,X_val,y_val);","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:53:24.942065Z","iopub.execute_input":"2022-08-03T17:53:24.942500Z","iopub.status.idle":"2022-08-03T17:53:25.210948Z","shell.execute_reply.started":"2022-08-03T17:53:24.942467Z","shell.execute_reply":"2022-08-03T17:53:25.209892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval2=DecisionTreeClassifier(min_samples_split=6,\n                             min_samples_leaf= 3,\n                             max_leaf_nodes=7,\n                             max_depth=15).fit(X_train,y_train)\npred2=eval2.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:53:33.245440Z","iopub.execute_input":"2022-08-03T17:53:33.245846Z","iopub.status.idle":"2022-08-03T17:53:33.257202Z","shell.execute_reply.started":"2022-08-03T17:53:33.245814Z","shell.execute_reply":"2022-08-03T17:53:33.256004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(y_val,pred2))\nprint(accuracy_score(y_val,pred2))","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:53:38.390802Z","iopub.execute_input":"2022-08-03T17:53:38.391558Z","iopub.status.idle":"2022-08-03T17:53:38.399202Z","shell.execute_reply.started":"2022-08-03T17:53:38.391528Z","shell.execute_reply":"2022-08-03T17:53:38.398085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc_curve(eval2,X_val,y_val);","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:53:40.939778Z","iopub.execute_input":"2022-08-03T17:53:40.940884Z","iopub.status.idle":"2022-08-03T17:53:41.142182Z","shell.execute_reply.started":"2022-08-03T17:53:40.940843Z","shell.execute_reply":"2022-08-03T17:53:41.140904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As AUC of Random Forest is slightly better(0.89) than Decision Tree let's choose Random Forest Model to predict values.","metadata":{}},{"cell_type":"markdown","source":"### Preparing test data","metadata":{"execution":{"iopub.status.busy":"2022-07-04T08:31:18.526655Z","iopub.execute_input":"2022-07-04T08:31:18.527873Z","iopub.status.idle":"2022-07-04T08:31:18.542077Z","shell.execute_reply.started":"2022-07-04T08:31:18.527823Z","shell.execute_reply":"2022-07-04T08:31:18.540921Z"}}},{"cell_type":"code","source":"test_data=pd.read_csv('../input/titanic/test.csv')\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:53:59.285472Z","iopub.execute_input":"2022-08-03T17:53:59.286106Z","iopub.status.idle":"2022-08-03T17:53:59.312413Z","shell.execute_reply.started":"2022-08-03T17:53:59.286044Z","shell.execute_reply":"2022-08-03T17:53:59.311707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:54:05.176833Z","iopub.execute_input":"2022-08-03T17:54:05.177186Z","iopub.status.idle":"2022-08-03T17:54:05.191724Z","shell.execute_reply.started":"2022-08-03T17:54:05.177151Z","shell.execute_reply":"2022-08-03T17:54:05.190401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:54:10.733884Z","iopub.execute_input":"2022-08-03T17:54:10.734435Z","iopub.status.idle":"2022-08-03T17:54:10.745204Z","shell.execute_reply.started":"2022-08-03T17:54:10.734393Z","shell.execute_reply":"2022-08-03T17:54:10.744108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_count(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:54:15.984714Z","iopub.execute_input":"2022-08-03T17:54:15.985203Z","iopub.status.idle":"2022-08-03T17:54:16.000982Z","shell.execute_reply.started":"2022-08-03T17:54:15.985159Z","shell.execute_reply":"2022-08-03T17:54:15.999882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['title']=test_data['Name'].str.split(',',expand=True)[1]\ntest_data['title']=test_data['title'].str.split('.',expand=True)[0]\ntest_data.title.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:54:30.499588Z","iopub.execute_input":"2022-08-03T17:54:30.500654Z","iopub.status.idle":"2022-08-03T17:54:30.512620Z","shell.execute_reply.started":"2022-08-03T17:54:30.500615Z","shell.execute_reply":"2022-08-03T17:54:30.511799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.title.replace(['Col','Rev','Dr'],'Mr',regex=True,inplace=True)\ntest_data.title.replace(['Ms','Dona'],'Miss',regex=True,inplace=True)\ntest_data.title.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:55:39.709594Z","iopub.execute_input":"2022-08-03T17:55:39.710004Z","iopub.status.idle":"2022-08-03T17:55:39.724653Z","shell.execute_reply.started":"2022-08-03T17:55:39.709972Z","shell.execute_reply":"2022-08-03T17:55:39.723571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.groupby('Sex')['Age'].median()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:55:45.422237Z","iopub.execute_input":"2022-08-03T17:55:45.422605Z","iopub.status.idle":"2022-08-03T17:55:45.431502Z","shell.execute_reply.started":"2022-08-03T17:55:45.422574Z","shell.execute_reply":"2022-08-03T17:55:45.430658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['Age']=test_data['Age'].fillna(27)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:55:48.031717Z","iopub.execute_input":"2022-08-03T17:55:48.032346Z","iopub.status.idle":"2022-08-03T17:55:48.038433Z","shell.execute_reply.started":"2022-08-03T17:55:48.032313Z","shell.execute_reply":"2022-08-03T17:55:48.037331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['sec']=test_data['Cabin'].str[0]\ntest_data.sec.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:55:50.512133Z","iopub.execute_input":"2022-08-03T17:55:50.512687Z","iopub.status.idle":"2022-08-03T17:55:50.522771Z","shell.execute_reply.started":"2022-08-03T17:55:50.512657Z","shell.execute_reply":"2022-08-03T17:55:50.521615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.sec=test_data.sec.fillna('other')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:55:55.699278Z","iopub.execute_input":"2022-08-03T17:55:55.699921Z","iopub.status.idle":"2022-08-03T17:55:55.704574Z","shell.execute_reply.started":"2022-08-03T17:55:55.699877Z","shell.execute_reply":"2022-08-03T17:55:55.703797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.Fare.fillna(test_data.Fare.median(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:55:59.813415Z","iopub.execute_input":"2022-08-03T17:55:59.813847Z","iopub.status.idle":"2022-08-03T17:55:59.820658Z","shell.execute_reply.started":"2022-08-03T17:55:59.813817Z","shell.execute_reply":"2022-08-03T17:55:59.819687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=test_data.drop(['PassengerId','Name','Ticket','Cabin'],axis=1)\nX_test=pd.get_dummies(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:56:02.797151Z","iopub.execute_input":"2022-08-03T17:56:02.798191Z","iopub.status.idle":"2022-08-03T17:56:02.811785Z","shell.execute_reply.started":"2022-08-03T17:56:02.798144Z","shell.execute_reply":"2022-08-03T17:56:02.810791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:56:05.595319Z","iopub.execute_input":"2022-08-03T17:56:05.596435Z","iopub.status.idle":"2022-08-03T17:56:05.606386Z","shell.execute_reply.started":"2022-08-03T17:56:05.596395Z","shell.execute_reply":"2022-08-03T17:56:05.605307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_sol=eval1.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:56:11.560882Z","iopub.execute_input":"2022-08-03T17:56:11.561306Z","iopub.status.idle":"2022-08-03T17:56:11.646618Z","shell.execute_reply.started":"2022-08-03T17:56:11.561271Z","shell.execute_reply":"2022-08-03T17:56:11.645557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_sol","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:56:14.810933Z","iopub.execute_input":"2022-08-03T17:56:14.811315Z","iopub.status.idle":"2022-08-03T17:56:14.818695Z","shell.execute_reply.started":"2022-08-03T17:56:14.811283Z","shell.execute_reply":"2022-08-03T17:56:14.817840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission=pd.DataFrame({'PassengerId':test_data.PassengerId,\n                           'Survived':y_pred_sol})","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:56:20.856642Z","iopub.execute_input":"2022-08-03T17:56:20.857333Z","iopub.status.idle":"2022-08-03T17:56:20.862804Z","shell.execute_reply.started":"2022-08-03T17:56:20.857301Z","shell.execute_reply":"2022-08-03T17:56:20.861534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T17:57:16.967143Z","iopub.execute_input":"2022-08-03T17:57:16.967828Z","iopub.status.idle":"2022-08-03T17:57:16.975850Z","shell.execute_reply.started":"2022-08-03T17:57:16.967794Z","shell.execute_reply":"2022-08-03T17:57:16.975156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}