{"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 pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-30T20:39:38.314423Z","iopub.execute_input":"2022-07-30T20:39:38.315296Z","iopub.status.idle":"2022-07-30T20:39:38.837731Z","shell.execute_reply.started":"2022-07-30T20:39:38.315186Z","shell.execute_reply":"2022-07-30T20:39:38.836814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/spaceship-titanic/train.csv')\ntest = pd.read_csv('../input/spaceship-titanic/test.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:38.839429Z","iopub.execute_input":"2022-07-30T20:39:38.840109Z","iopub.status.idle":"2022-07-30T20:39:38.916429Z","shell.execute_reply.started":"2022-07-30T20:39:38.840052Z","shell.execute_reply":"2022-07-30T20:39:38.915557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:38.917587Z","iopub.execute_input":"2022-07-30T20:39:38.918070Z","iopub.status.idle":"2022-07-30T20:39:38.925150Z","shell.execute_reply.started":"2022-07-30T20:39:38.918039Z","shell.execute_reply":"2022-07-30T20:39:38.924053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:38.928187Z","iopub.execute_input":"2022-07-30T20:39:38.928561Z","iopub.status.idle":"2022-07-30T20:39:38.955705Z","shell.execute_reply.started":"2022-07-30T20:39:38.928528Z","shell.execute_reply":"2022-07-30T20:39:38.954589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.nunique()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:38.957950Z","iopub.execute_input":"2022-07-30T20:39:38.959005Z","iopub.status.idle":"2022-07-30T20:39:38.978314Z","shell.execute_reply.started":"2022-07-30T20:39:38.958942Z","shell.execute_reply":"2022-07-30T20:39:38.977480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe(exclude='object').T","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:38.979447Z","iopub.execute_input":"2022-07-30T20:39:38.980219Z","iopub.status.idle":"2022-07-30T20:39:39.019202Z","shell.execute_reply.started":"2022-07-30T20:39:38.980182Z","shell.execute_reply":"2022-07-30T20:39:39.017946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(round(((train.isna().sum())/len(train))*100, 2))","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:39.020558Z","iopub.execute_input":"2022-07-30T20:39:39.021683Z","iopub.status.idle":"2022-07-30T20:39:39.043281Z","shell.execute_reply.started":"2022-07-30T20:39:39.021637Z","shell.execute_reply":"2022-07-30T20:39:39.042051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['Early', 'Mid', 'Senior']\nbins=np.linspace(min(train['Age']), max(train['Age']), 4)\ntrain['Age_Bin']=pd.cut(train['Age'], bins, labels=labels, include_lowest=True)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:39.044923Z","iopub.execute_input":"2022-07-30T20:39:39.045735Z","iopub.status.idle":"2022-07-30T20:39:39.075453Z","shell.execute_reply.started":"2022-07-30T20:39:39.045684Z","shell.execute_reply":"2022-07-30T20:39:39.074252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,7))\nsns.countplot(train.HomePlanet)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:39.076941Z","iopub.execute_input":"2022-07-30T20:39:39.077268Z","iopub.status.idle":"2022-07-30T20:39:39.264012Z","shell.execute_reply.started":"2022-07-30T20:39:39.077239Z","shell.execute_reply":"2022-07-30T20:39:39.263016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.countplot(x=train.HomePlanet, hue=train.Age_Bin);","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:39.265297Z","iopub.execute_input":"2022-07-30T20:39:39.265598Z","iopub.status.idle":"2022-07-30T20:39:39.488356Z","shell.execute_reply.started":"2022-07-30T20:39:39.265570Z","shell.execute_reply":"2022-07-30T20:39:39.487245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.countplot(x=train.HomePlanet, hue=train.Transported);","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:39.489933Z","iopub.execute_input":"2022-07-30T20:39:39.490309Z","iopub.status.idle":"2022-07-30T20:39:39.699629Z","shell.execute_reply.started":"2022-07-30T20:39:39.490278Z","shell.execute_reply":"2022-07-30T20:39:39.698869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.scatterplot(x='Age', y ='RoomService', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:39.700794Z","iopub.execute_input":"2022-07-30T20:39:39.701344Z","iopub.status.idle":"2022-07-30T20:39:39.937237Z","shell.execute_reply.started":"2022-07-30T20:39:39.701310Z","shell.execute_reply":"2022-07-30T20:39:39.935992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.scatterplot(x='Age', y ='FoodCourt', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:39.941003Z","iopub.execute_input":"2022-07-30T20:39:39.941480Z","iopub.status.idle":"2022-07-30T20:39:40.177122Z","shell.execute_reply.started":"2022-07-30T20:39:39.941442Z","shell.execute_reply":"2022-07-30T20:39:40.176057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.scatterplot(x='Age', y ='ShoppingMall', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:40.178914Z","iopub.execute_input":"2022-07-30T20:39:40.179675Z","iopub.status.idle":"2022-07-30T20:39:40.405791Z","shell.execute_reply.started":"2022-07-30T20:39:40.179626Z","shell.execute_reply":"2022-07-30T20:39:40.404951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[['HomePlanet', 'CryoSleep', 'Age', 'VIP', 'RoomService', 'FoodCourt',\n           'ShoppingMall', 'Spa', 'VRDeck']]\n\ny = train.Transported","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:40.407049Z","iopub.execute_input":"2022-07-30T20:39:40.407731Z","iopub.status.idle":"2022-07-30T20:39:40.415214Z","shell.execute_reply.started":"2022-07-30T20:39:40.407691Z","shell.execute_reply":"2022-07-30T20:39:40.413810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:41.730159Z","iopub.execute_input":"2022-07-30T20:39:41.730589Z","iopub.status.idle":"2022-07-30T20:39:41.743311Z","shell.execute_reply.started":"2022-07-30T20:39:41.730554Z","shell.execute_reply":"2022-07-30T20:39:41.742536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_cols = ['Age', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']\ncat_cols = ['HomePlanet', 'CryoSleep', 'VIP']\nfor i in num_cols:\n    X[i].fillna(np.mean(X[i]), inplace=True)\n    \nfor j in cat_cols:\n    X[j].fillna(X[i].mode()[0], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:42.611596Z","iopub.execute_input":"2022-07-30T20:39:42.612246Z","iopub.status.idle":"2022-07-30T20:39:42.632612Z","shell.execute_reply.started":"2022-07-30T20:39:42.612208Z","shell.execute_reply":"2022-07-30T20:39:42.631333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:39:44.944398Z","iopub.execute_input":"2022-07-30T20:39:44.945162Z","iopub.status.idle":"2022-07-30T20:39:44.959258Z","shell.execute_reply.started":"2022-07-30T20:39:44.945107Z","shell.execute_reply":"2022-07-30T20:39:44.957566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = pd.get_dummies(X, columns=cat_cols, drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:42:32.639632Z","iopub.execute_input":"2022-07-30T20:42:32.640157Z","iopub.status.idle":"2022-07-30T20:42:32.656266Z","shell.execute_reply.started":"2022-07-30T20:42:32.640115Z","shell.execute_reply":"2022-07-30T20:42:32.654885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:42:38.308875Z","iopub.execute_input":"2022-07-30T20:42:38.310047Z","iopub.status.idle":"2022-07-30T20:42:38.316405Z","shell.execute_reply.started":"2022-07-30T20:42:38.309961Z","shell.execute_reply":"2022-07-30T20:42:38.314919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:42:38.619238Z","iopub.execute_input":"2022-07-30T20:42:38.619989Z","iopub.status.idle":"2022-07-30T20:42:38.625028Z","shell.execute_reply.started":"2022-07-30T20:42:38.619926Z","shell.execute_reply":"2022-07-30T20:42:38.624149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=100)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:42:39.070074Z","iopub.execute_input":"2022-07-30T20:42:39.071353Z","iopub.status.idle":"2022-07-30T20:42:39.081420Z","shell.execute_reply.started":"2022-07-30T20:42:39.071305Z","shell.execute_reply":"2022-07-30T20:42:39.080370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = StandardScaler()\n\nX_train = sc.fit_transform(X_train)\nX_train = pd.DataFrame(X_train, columns=X_test.columns)\n\nX_test = sc.transform(X_test)\nX_test = pd.DataFrame(X_test, columns = X_train.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:42:39.517301Z","iopub.execute_input":"2022-07-30T20:42:39.517984Z","iopub.status.idle":"2022-07-30T20:42:39.532587Z","shell.execute_reply.started":"2022-07-30T20:42:39.517925Z","shell.execute_reply":"2022-07-30T20:42:39.531295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Random Forest Classifier**","metadata":{}},{"cell_type":"code","source":"param_grid = param_grid = { \n    'n_estimators': [200, 300,400,500,600],\n    'max_features': ['auto', 'sqrt', 'log2'],\n    'max_depth' : [4,5,6,7,8],\n    'criterion' :['gini', 'entropy']\n}\n\n\nRF = RandomForestClassifier(random_state = 100)\n\n\ngridcv_rf = GridSearchCV(estimator=RF, param_grid=param_grid, \n                    cv=3,verbose=True)\ngridcv_rf.fit(X_train, y_train)\nrf_model = gridcv_rf.best_estimator_\nrf_model","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:42:44.144543Z","iopub.execute_input":"2022-07-30T20:42:44.145661Z","iopub.status.idle":"2022-07-30T20:55:12.438438Z","shell.execute_reply.started":"2022-07-30T20:42:44.145614Z","shell.execute_reply":"2022-07-30T20:55:12.437552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_model.fit(X_train, y_train)\nrf_pred = rf_model.predict(X_test)\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import classification_report\nprint(f'Accuracy for Random Forest is: {accuracy_score(y_test, rf_pred)}')\nprint(f'Classification Report for Random Forest \\n{classification_report(y_test, rf_pred)}')","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:55:26.806362Z","iopub.execute_input":"2022-07-30T20:55:26.807197Z","iopub.status.idle":"2022-07-30T20:55:29.250862Z","shell.execute_reply.started":"2022-07-30T20:55:26.807152Z","shell.execute_reply":"2022-07-30T20:55:29.249736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Logistic Regression Classifier**","metadata":{}},{"cell_type":"code","source":"folds = KFold(n_splits=5, shuffle=True, random_state=4)\n\n# Specify params\nparams = {\"C\": [0.01, 0.1, 1, 10, 100, 1000]}\n\n# Specifing score as recall as we are more focused on acheiving the higher sensitivity than the accuracy\nmodel_cv = GridSearchCV(estimator = LogisticRegression(),\n                        param_grid = params, \n                        scoring= 'recall', \n                        cv = folds, \n                        verbose = 1,\n                        return_train_score=True) \n\n# Fit the model\nmodel_cv.fit(X_train, y_train)\nlr_model = model_cv.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:55:39.102048Z","iopub.execute_input":"2022-07-30T20:55:39.102594Z","iopub.status.idle":"2022-07-30T20:55:40.599307Z","shell.execute_reply.started":"2022-07-30T20:55:39.102555Z","shell.execute_reply":"2022-07-30T20:55:40.597679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_model.fit(X_train, y_train)\nlr_pred = lr_model.predict(X_test)\nprint(f'Accuracy for Logistic Reg is: {accuracy_score(y_test, lr_pred)}')\nprint(f'Classification Report for Logistic Reg \\n{classification_report(y_test, lr_pred)}')","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:56:47.681852Z","iopub.execute_input":"2022-07-30T20:56:47.683286Z","iopub.status.idle":"2022-07-30T20:56:47.749382Z","shell.execute_reply.started":"2022-07-30T20:56:47.683234Z","shell.execute_reply":"2022-07-30T20:56:47.748185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_results = pd.DataFrame(model_cv.cv_results_)\nplt.figure(figsize=(8, 6))\nplt.plot(cv_results['param_C'], cv_results['mean_test_score'])\nplt.plot(cv_results['param_C'], cv_results['mean_train_score'])\nplt.xlabel('C')\nplt.ylabel('sensitivity')\nplt.legend(['test result', 'train result'], loc='upper left')\nplt.xscale('log')","metadata":{"execution":{"iopub.status.busy":"2022-07-30T20:58:04.172889Z","iopub.execute_input":"2022-07-30T20:58:04.174308Z","iopub.status.idle":"2022-07-30T20:58:04.946484Z","shell.execute_reply.started":"2022-07-30T20:58:04.174257Z","shell.execute_reply":"2022-07-30T20:58:04.945341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Support Vector Classifier**","metadata":{}},{"cell_type":"code","source":"hyper_params = [ {'gamma': [1e-2, 1e-3, 1e-4],\n                     'C': [1, 10, 100, 1000]}]\n\n\n# specify model with RBF kernel\nsvc = SVC(kernel=\"rbf\")\n\n# set up GridSearchCV()\nsvc_cv = GridSearchCV(estimator = svc, \n                        param_grid = hyper_params, \n                        scoring= 'accuracy', \n                        cv = 3, \n                        verbose = 1,\n                        return_train_score=True)      \n\n# fit the model\nsvc_cv.fit(X_train, y_train)\nsvc_model = svc_cv.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-30T21:01:16.731917Z","iopub.execute_input":"2022-07-30T21:01:16.732525Z","iopub.status.idle":"2022-07-30T21:02:43.388907Z","shell.execute_reply.started":"2022-07-30T21:01:16.732467Z","shell.execute_reply":"2022-07-30T21:02:43.388027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svc_model.fit(X_train, y_train)\nsvc_pred = svc_model.predict(X_test)\nprint(f'Accuracy for Logistic Reg is: {accuracy_score(y_test, svc_pred)}')\nprint(f'Classification Report for Logistic Reg \\n{classification_report(y_test, svc_pred)}')","metadata":{"execution":{"iopub.status.busy":"2022-07-30T21:03:06.211149Z","iopub.execute_input":"2022-07-30T21:03:06.211593Z","iopub.status.idle":"2022-07-30T21:03:14.183132Z","shell.execute_reply.started":"2022-07-30T21:03:06.211555Z","shell.execute_reply":"2022-07-30T21:03:14.181051Z"},"trusted":true},"execution_count":null,"outputs":[]}]}