{"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)\nimport matplotlib.pyplot as plt # For Data Visualization\nimport xgboost as xgb\nimport shap\nfrom xgboost import XGBClassifier,cv # Model to be used for classification\n\n#and cv for cross_val\n\nfrom sklearn.preprocessing import LabelEncoder,OneHotEncoder\nfrom sklearn.inspection import permutation_importance\nfrom sklearn.model_selection import GridSearchCV\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\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\npd.set_option('max_rows',1000)\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-03T14:20:34.978852Z","iopub.execute_input":"2022-08-03T14:20:34.979347Z","iopub.status.idle":"2022-08-03T14:20:38.189875Z","shell.execute_reply.started":"2022-08-03T14:20:34.979252Z","shell.execute_reply":"2022-08-03T14:20:38.188632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"The summary of the whole process can be explained as follows : \n1. Read the Dataset\n2. Visualize and fill the missing values with a simple idea\n\n    -> If the data has too many outliers, we take the median\n    \n    -> If the data appears to be normalized, we take the mean\n    \n    -> For categorical data, we usually take the mode\n    \n    Deleting the data is not an option as we have to provide a result for each given test case\n3. Finalize the necessary features ( where we have considered the cabin portion by dviding it into three portions\n4. Perform necessary encoduing ( one hot encoding for non-sequential categories and label encoding for sequential categories ) to prepare the data for training\n5. Decide a model and tune hyperparameters to get the necessary results","metadata":{}},{"cell_type":"markdown","source":"# Read Dataset\n","metadata":{}},{"cell_type":"code","source":"#Read all datasets\nsample=pd.read_csv('/kaggle/input/spaceship-titanic/sample_submission.csv')\nX=pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')\nxtest=pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:38.191784Z","iopub.execute_input":"2022-08-03T14:20:38.192158Z","iopub.status.idle":"2022-08-03T14:20:38.317579Z","shell.execute_reply.started":"2022-08-03T14:20:38.192125Z","shell.execute_reply":"2022-08-03T14:20:38.316494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:38.319127Z","iopub.execute_input":"2022-08-03T14:20:38.319494Z","iopub.status.idle":"2022-08-03T14:20:38.349155Z","shell.execute_reply.started":"2022-08-03T14:20:38.319463Z","shell.execute_reply":"2022-08-03T14:20:38.348105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check for missing values\nX.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:38.350539Z","iopub.execute_input":"2022-08-03T14:20:38.350947Z","iopub.status.idle":"2022-08-03T14:20:38.365515Z","shell.execute_reply.started":"2022-08-03T14:20:38.350909Z","shell.execute_reply":"2022-08-03T14:20:38.364484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"#Drop the unnecessary columns\nxtrain=X.drop(['Name','PassengerId','Transported'],axis=1)\n#Get the test ids\nids=xtest['PassengerId']\n#Drop unnecessary columns in test dataset\nxtest=xtest.drop(['Name','PassengerId'],axis=1)\n#Target column for training dataset\ny=X['Transported'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:38.368012Z","iopub.execute_input":"2022-08-03T14:20:38.368352Z","iopub.status.idle":"2022-08-03T14:20:38.379275Z","shell.execute_reply.started":"2022-08-03T14:20:38.368322Z","shell.execute_reply":"2022-08-03T14:20:38.378144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fill Age with mean\nplt.hist(xtrain['Age'])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:38.380613Z","iopub.execute_input":"2022-08-03T14:20:38.381180Z","iopub.status.idle":"2022-08-03T14:20:38.647053Z","shell.execute_reply.started":"2022-08-03T14:20:38.381144Z","shell.execute_reply":"2022-08-03T14:20:38.645946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fill destination with mode\n#Fill CryoSleep with mode\n#Fill Cabin with hybrid mode\n#Fill Homeplanet with mode\nxtrain['Destination'].value_counts().plot(kind='bar')\nplt.show()\nxtrain['CryoSleep'].value_counts().plot(kind='bar')\nplt.show()\n#plt.figure(figsize=(10,10))\n#xtrain['Cabin'].value_counts().plot(kind='bar')\n#plt.show()\nxtrain['HomePlanet'].value_counts().plot(kind='bar')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:38.648148Z","iopub.execute_input":"2022-08-03T14:20:38.648958Z","iopub.status.idle":"2022-08-03T14:20:39.203360Z","shell.execute_reply.started":"2022-08-03T14:20:38.648918Z","shell.execute_reply":"2022-08-03T14:20:39.202274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fill services columns with zero ie Mode\nfig,axes=plt.subplots(3,2,figsize=(10,10))\n\naxes[0][0].hist(xtrain['Spa'],bins=20)\naxes[0][1].hist(xtrain['VRDeck'],bins=20)\naxes[1][0].hist(xtrain['RoomService'],bins=20)\naxes[1][1].hist(xtrain['FoodCourt'],bins=20)\naxes[2][0].hist(xtrain['ShoppingMall'],bins=20)\nplt.show()\n#Drop the name column\n#Fill VIP with False\nxtrain['VIP'].value_counts().plot(kind='bar')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:39.204877Z","iopub.execute_input":"2022-08-03T14:20:39.205605Z","iopub.status.idle":"2022-08-03T14:20:40.360915Z","shell.execute_reply.started":"2022-08-03T14:20:39.205551Z","shell.execute_reply":"2022-08-03T14:20:40.359843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Use the different portions of cabin to make a judgement : \nxtrain['Deck']=xtrain['Cabin'].apply(lambda x : str(x)[0])\nxtrain['Num']=xtrain['Cabin'].apply(lambda x : str(x)[2])\nxtrain['Side']=xtrain['Cabin'].apply(lambda x : str(x)[-1])\n\nxtest['Deck']=xtest['Cabin'].apply(lambda x : str(x)[0])\nxtest['Num']=xtest['Cabin'].apply(lambda x : str(x)[2])\nxtest['Side']=xtest['Cabin'].apply(lambda x : str(x)[-1])\nxtrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.362808Z","iopub.execute_input":"2022-08-03T14:20:40.363358Z","iopub.status.idle":"2022-08-03T14:20:40.407196Z","shell.execute_reply.started":"2022-08-03T14:20:40.363310Z","shell.execute_reply":"2022-08-03T14:20:40.406389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mode_cols=['Spa','VRDeck','ShoppingMall','FoodCourt','RoomService','VIP','Destination','CryoSleep','HomePlanet']\nxtrain.fillna({col : xtrain[col].mode()[0] for col in mode_cols},inplace=True)\nxtest.fillna({col : xtrain[col].mode()[0] for col in mode_cols},inplace=True)\nxtrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.408753Z","iopub.execute_input":"2022-08-03T14:20:40.409127Z","iopub.status.idle":"2022-08-03T14:20:40.449315Z","shell.execute_reply.started":"2022-08-03T14:20:40.409093Z","shell.execute_reply":"2022-08-03T14:20:40.448319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain['Num'].replace('n',xtrain['Num'].mode()[0],inplace=True)\nxtest['Num'].replace('n',xtrain['Num'].mode()[0],inplace=True)\n\nxtrain['Side'].replace('n',xtrain['Side'].mode()[0],inplace=True)\nxtest['Side'].replace('n',xtrain['Side'].mode()[0],inplace=True)\n\nxtrain['Deck'].replace('n',xtrain['Num'].mode()[0],inplace=True)\nxtest['Deck'].replace('n',xtrain['Num'].mode()[0],inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.450649Z","iopub.execute_input":"2022-08-03T14:20:40.451078Z","iopub.status.idle":"2022-08-03T14:20:40.468987Z","shell.execute_reply.started":"2022-08-03T14:20:40.451043Z","shell.execute_reply":"2022-08-03T14:20:40.467842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convert booleans to ints\nxtrain['VIP']=xtrain['VIP'].astype(bool).astype(int)\nxtest['VIP']=xtest['VIP'].astype(bool).astype(int)\nxtrain['CryoSleep']=xtrain['CryoSleep'].astype(bool).astype(int)\nxtest['CryoSleep']=xtest['CryoSleep'].astype(bool).astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.470568Z","iopub.execute_input":"2022-08-03T14:20:40.471224Z","iopub.status.idle":"2022-08-03T14:20:40.480561Z","shell.execute_reply.started":"2022-08-03T14:20:40.471187Z","shell.execute_reply":"2022-08-03T14:20:40.479590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fill Age with mean\nxtrain['Age'].fillna(xtrain['Age'].mean(),inplace=True)\nxtest['Age'].fillna(xtrain['Age'].mean(),inplace=True)\nxtrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.481771Z","iopub.execute_input":"2022-08-03T14:20:40.482396Z","iopub.status.idle":"2022-08-03T14:20:40.506109Z","shell.execute_reply.started":"2022-08-03T14:20:40.482362Z","shell.execute_reply":"2022-08-03T14:20:40.504750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.509271Z","iopub.execute_input":"2022-08-03T14:20:40.511527Z","iopub.status.idle":"2022-08-03T14:20:40.525096Z","shell.execute_reply.started":"2022-08-03T14:20:40.511482Z","shell.execute_reply":"2022-08-03T14:20:40.524076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain['Num']=xtrain['Num'].astype(int)\nxtest['Num']=xtest['Num'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.527580Z","iopub.execute_input":"2022-08-03T14:20:40.528457Z","iopub.status.idle":"2022-08-03T14:20:40.538798Z","shell.execute_reply.started":"2022-08-03T14:20:40.528408Z","shell.execute_reply":"2022-08-03T14:20:40.537575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le=LabelEncoder()\nohe=OneHotEncoder(sparse=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.541769Z","iopub.execute_input":"2022-08-03T14:20:40.542445Z","iopub.status.idle":"2022-08-03T14:20:40.548585Z","shell.execute_reply.started":"2022-08-03T14:20:40.542274Z","shell.execute_reply":"2022-08-03T14:20:40.547821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def categorize(column,headers):\n    le_result=le.fit_transform(column)\n    le_result=le_result.reshape(len(column),1)\n    ohe_result=pd.DataFrame(ohe.fit_transform(le_result),columns=headers)\n    return ohe_result","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.550126Z","iopub.execute_input":"2022-08-03T14:20:40.551211Z","iopub.status.idle":"2022-08-03T14:20:40.561040Z","shell.execute_reply.started":"2022-08-03T14:20:40.551162Z","shell.execute_reply":"2022-08-03T14:20:40.560118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get name of encoded columns to use in the final dataframe\ndestinations=sorted(xtrain['Destination'].unique())\nhomes=sorted(xtrain['HomePlanet'].unique())\ndecks=sorted(xtrain['Deck'].unique())\nsides=sorted(xtrain['Side'].unique())\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.563042Z","iopub.execute_input":"2022-08-03T14:20:40.563806Z","iopub.status.idle":"2022-08-03T14:20:40.576278Z","shell.execute_reply.started":"2022-08-03T14:20:40.563759Z","shell.execute_reply":"2022-08-03T14:20:40.575317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get one hot encoded results\nohe_train_destination=categorize(xtrain['Destination'],destinations)\nohe_train_home=categorize(xtrain['HomePlanet'],homes)\nohe_train_deck=categorize(xtrain['Deck'],decks)\nohe_train_side=categorize(xtrain['Side'],sides)\nxtrain.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.578975Z","iopub.execute_input":"2022-08-03T14:20:40.579996Z","iopub.status.idle":"2022-08-03T14:20:40.618152Z","shell.execute_reply.started":"2022-08-03T14:20:40.579955Z","shell.execute_reply":"2022-08-03T14:20:40.617192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain=xtrain.join([ohe_train_destination,ohe_train_home,ohe_train_deck,ohe_train_side])\n","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.619390Z","iopub.execute_input":"2022-08-03T14:20:40.619768Z","iopub.status.idle":"2022-08-03T14:20:40.633933Z","shell.execute_reply.started":"2022-08-03T14:20:40.619733Z","shell.execute_reply":"2022-08-03T14:20:40.632736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.drop(['HomePlanet','Destination','Cabin','Deck','Side'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.635265Z","iopub.execute_input":"2022-08-03T14:20:40.635629Z","iopub.status.idle":"2022-08-03T14:20:40.642845Z","shell.execute_reply.started":"2022-08-03T14:20:40.635595Z","shell.execute_reply":"2022-08-03T14:20:40.641965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.644350Z","iopub.execute_input":"2022-08-03T14:20:40.644868Z","iopub.status.idle":"2022-08-03T14:20:40.662148Z","shell.execute_reply.started":"2022-08-03T14:20:40.644831Z","shell.execute_reply":"2022-08-03T14:20:40.661377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Same for Test dataset\nohe_test_destination=categorize(xtest['Destination'],destinations)\nohe_test_home=categorize(xtest['HomePlanet'],homes)\nohe_test_deck=categorize(xtest['Deck'],decks)\nohe_test_side=categorize(xtest['Side'],sides)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.663574Z","iopub.execute_input":"2022-08-03T14:20:40.664305Z","iopub.status.idle":"2022-08-03T14:20:40.681268Z","shell.execute_reply.started":"2022-08-03T14:20:40.664257Z","shell.execute_reply":"2022-08-03T14:20:40.680235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest=xtest.join([ohe_test_destination,ohe_test_home,ohe_test_deck,ohe_test_side])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.682561Z","iopub.execute_input":"2022-08-03T14:20:40.682885Z","iopub.status.idle":"2022-08-03T14:20:40.692269Z","shell.execute_reply.started":"2022-08-03T14:20:40.682857Z","shell.execute_reply":"2022-08-03T14:20:40.691073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest.drop(['HomePlanet','Destination','Cabin','Deck','Side'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.693771Z","iopub.execute_input":"2022-08-03T14:20:40.694171Z","iopub.status.idle":"2022-08-03T14:20:40.703830Z","shell.execute_reply.started":"2022-08-03T14:20:40.694124Z","shell.execute_reply":"2022-08-03T14:20:40.702989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.705546Z","iopub.execute_input":"2022-08-03T14:20:40.706502Z","iopub.status.idle":"2022-08-03T14:20:40.719680Z","shell.execute_reply.started":"2022-08-03T14:20:40.706462Z","shell.execute_reply":"2022-08-03T14:20:40.718905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.720839Z","iopub.execute_input":"2022-08-03T14:20:40.721542Z","iopub.status.idle":"2022-08-03T14:20:40.767047Z","shell.execute_reply.started":"2022-08-03T14:20:40.721493Z","shell.execute_reply":"2022-08-03T14:20:40.766086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# XGB Classifier\nparams = {'colsample_bytree': [0.6,0.7],\n          'learning_rate': [0.05,0.2,0.4],\n          'max_depth': [20,40,60],\n          'n_estimators':[20,60,100,350],\n          'alpha': [2,4,6,14]\n          }\nxgb_clf = XGBClassifier(objective= 'binary:logistic',\n                        eval_metric='mlogloss',\n                        nthread=-1,\n                        seed=42)\nprint(xgb_clf)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.768345Z","iopub.execute_input":"2022-08-03T14:20:40.769133Z","iopub.status.idle":"2022-08-03T14:20:40.778397Z","shell.execute_reply.started":"2022-08-03T14:20:40.769096Z","shell.execute_reply":"2022-08-03T14:20:40.777259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndmatrix = xgb.DMatrix(data=xtrain,label=y)\nxgb_clf.fit(xtrain,y)\nprint(f'Training Score : {xgb_clf.score(xtrain,y):.2f}')\n'''","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.779828Z","iopub.execute_input":"2022-08-03T14:20:40.780400Z","iopub.status.idle":"2022-08-03T14:20:40.791987Z","shell.execute_reply.started":"2022-08-03T14:20:40.780359Z","shell.execute_reply":"2022-08-03T14:20:40.791032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nxgb_cv = cv(dtrain=dmatrix, params=params, nfold=3,\n                    num_boost_round=50, early_stopping_rounds=10, \n                    metrics=\"auc\", as_pandas=True, seed=420)\n'''","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.793275Z","iopub.execute_input":"2022-08-03T14:20:40.793619Z","iopub.status.idle":"2022-08-03T14:20:40.804215Z","shell.execute_reply.started":"2022-08-03T14:20:40.793591Z","shell.execute_reply":"2022-08-03T14:20:40.803206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#xgb_cv.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.805823Z","iopub.execute_input":"2022-08-03T14:20:40.806636Z","iopub.status.idle":"2022-08-03T14:20:40.815199Z","shell.execute_reply.started":"2022-08-03T14:20:40.806600Z","shell.execute_reply":"2022-08-03T14:20:40.814065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search = GridSearchCV(\n    estimator=xgb_clf,\n    param_grid=params,\n    scoring = 'roc_auc',\n    n_jobs = -1,\n    cv = 3,\n    verbose=3\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.816302Z","iopub.execute_input":"2022-08-03T14:20:40.816657Z","iopub.status.idle":"2022-08-03T14:20:40.826082Z","shell.execute_reply.started":"2022-08-03T14:20:40.816614Z","shell.execute_reply":"2022-08-03T14:20:40.824987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search.fit(xtrain, y)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:20:40.827549Z","iopub.execute_input":"2022-08-03T14:20:40.828478Z","iopub.status.idle":"2022-08-03T14:30:40.807413Z","shell.execute_reply.started":"2022-08-03T14:20:40.828426Z","shell.execute_reply":"2022-08-03T14:30:40.806443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:30:40.811706Z","iopub.execute_input":"2022-08-03T14:30:40.812392Z","iopub.status.idle":"2022-08-03T14:30:40.822495Z","shell.execute_reply.started":"2022-08-03T14:30:40.812333Z","shell.execute_reply":"2022-08-03T14:30:40.821885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nexplainer = shap.TreeExplainer(grid_search)\nshap_values = explainer.shap_values(xtrain)\nshap.summary_plot(shap_values, xtrain, plot_type=\"bar\")\n'''","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:30:40.823683Z","iopub.execute_input":"2022-08-03T14:30:40.824133Z","iopub.status.idle":"2022-08-03T14:30:40.996078Z","shell.execute_reply.started":"2022-08-03T14:30:40.824102Z","shell.execute_reply":"2022-08-03T14:30:40.994674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results=grid_search.predict(xtest)\nresults=results.astype(bool)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:30:40.997186Z","iopub.status.idle":"2022-08-03T14:30:40.997733Z","shell.execute_reply.started":"2022-08-03T14:30:40.997479Z","shell.execute_reply":"2022-08-03T14:30:40.997508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df=pd.DataFrame({'Passengerid':ids,'Transported':results})\nfinal_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T14:30:40.998813Z","iopub.status.idle":"2022-08-03T14:30:40.999340Z","shell.execute_reply.started":"2022-08-03T14:30:40.999089Z","shell.execute_reply":"2022-08-03T14:30:40.999115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}