{"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\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.tree import DecisionTreeClassifier\nimport xgboost as xgb\nfrom tqdm import tqdm\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.ensemble import StackingClassifier\nfrom sklearn.svm import LinearSVC\nfrom sklearn.svm import NuSVC\nfrom sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:45:52.741744Z","iopub.execute_input":"2022-07-15T12:45:52.742896Z","iopub.status.idle":"2022-07-15T12:45:52.750782Z","shell.execute_reply.started":"2022-07-15T12:45:52.742826Z","shell.execute_reply":"2022-07-15T12:45:52.749381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/spaceship-titanic/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/spaceship-titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.636200Z","iopub.execute_input":"2022-07-15T12:12:23.636512Z","iopub.status.idle":"2022-07-15T12:12:23.718067Z","shell.execute_reply.started":"2022-07-15T12:12:23.636483Z","shell.execute_reply":"2022-07-15T12:12:23.717100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.719342Z","iopub.execute_input":"2022-07-15T12:12:23.719668Z","iopub.status.idle":"2022-07-15T12:12:23.752096Z","shell.execute_reply.started":"2022-07-15T12:12:23.719638Z","shell.execute_reply":"2022-07-15T12:12:23.751311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.753559Z","iopub.execute_input":"2022-07-15T12:12:23.754166Z","iopub.status.idle":"2022-07-15T12:12:23.778687Z","shell.execute_reply.started":"2022-07-15T12:12:23.754132Z","shell.execute_reply":"2022-07-15T12:12:23.777420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info(), test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.782541Z","iopub.execute_input":"2022-07-15T12:12:23.783570Z","iopub.status.idle":"2022-07-15T12:12:23.839339Z","shell.execute_reply.started":"2022-07-15T12:12:23.783516Z","shell.execute_reply":"2022-07-15T12:12:23.837799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Name column is noninformative, so we can drop it","metadata":{}},{"cell_type":"code","source":"train = train.drop([\"Name\"], axis=1)\ntest = test.drop([\"Name\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.841665Z","iopub.execute_input":"2022-07-15T12:12:23.842543Z","iopub.status.idle":"2022-07-15T12:12:23.856074Z","shell.execute_reply.started":"2022-07-15T12:12:23.842495Z","shell.execute_reply":"2022-07-15T12:12:23.854435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Can we use group ID as a predictor? Let's see if group IDs intersect on train and test.","metadata":{}},{"cell_type":"code","source":"train_groups = np.array(list(map(lambda x: int(x.split(\"_\")[0]), train.PassengerId)))\ntrain_groups","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.858898Z","iopub.execute_input":"2022-07-15T12:12:23.859664Z","iopub.status.idle":"2022-07-15T12:12:23.883249Z","shell.execute_reply.started":"2022-07-15T12:12:23.859604Z","shell.execute_reply":"2022-07-15T12:12:23.881735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_groups = np.array(list(map(lambda x: int(x.split(\"_\")[0]), test.PassengerId)))\ntest_groups","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.886617Z","iopub.execute_input":"2022-07-15T12:12:23.887048Z","iopub.status.idle":"2022-07-15T12:12:23.900528Z","shell.execute_reply.started":"2022-07-15T12:12:23.886997Z","shell.execute_reply":"2022-07-15T12:12:23.898938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(test_groups) & set(train_groups))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.902335Z","iopub.execute_input":"2022-07-15T12:12:23.903389Z","iopub.status.idle":"2022-07-15T12:12:23.918010Z","shell.execute_reply.started":"2022-07-15T12:12:23.903334Z","shell.execute_reply":"2022-07-15T12:12:23.915698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"They dont, so group ID is just an identificator for us.","metadata":{}},{"cell_type":"code","source":"np.mean(train.Transported == 1), np.mean(train.Transported == 0)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.920057Z","iopub.execute_input":"2022-07-15T12:12:23.920877Z","iopub.status.idle":"2022-07-15T12:12:23.931337Z","shell.execute_reply.started":"2022-07-15T12:12:23.920810Z","shell.execute_reply":"2022-07-15T12:12:23.930046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Classes are balanced","metadata":{}},{"cell_type":"markdown","source":"Let's look at money spent on luxory amenities and  see if they ary informative","metadata":{}},{"cell_type":"code","source":"train[\"Transported\"] = train[\"Transported\"].astype(int)\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.933254Z","iopub.execute_input":"2022-07-15T12:12:23.934036Z","iopub.status.idle":"2022-07-15T12:12:23.966139Z","shell.execute_reply.started":"2022-07-15T12:12:23.933990Z","shell.execute_reply":"2022-07-15T12:12:23.964464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coorelation_matrix = np.corrcoef(train.dropna()[[\"RoomService\", \"FoodCourt\", \"ShoppingMall\", \"Spa\", \"VRDeck\", \"Transported\"]], rowvar = 0)\ncoorelation_matrix","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.967539Z","iopub.execute_input":"2022-07-15T12:12:23.968239Z","iopub.status.idle":"2022-07-15T12:12:23.993626Z","shell.execute_reply.started":"2022-07-15T12:12:23.968193Z","shell.execute_reply":"2022-07-15T12:12:23.992584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = train.dropna()[[\"RoomService\", \"FoodCourt\", \"ShoppingMall\", \"Spa\", \"VRDeck\", \"Transported\"]].corr()\ncorr.style.background_gradient(cmap='coolwarm')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:23.995427Z","iopub.execute_input":"2022-07-15T12:12:23.996180Z","iopub.status.idle":"2022-07-15T12:12:24.106615Z","shell.execute_reply.started":"2022-07-15T12:12:23.996131Z","shell.execute_reply":"2022-07-15T12:12:24.105494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Room service, SPA and VR deck have small non-zero correlation with target. They should be good for prediction","metadata":{}},{"cell_type":"code","source":"fig = plt.figure()\naxes = [fig.add_subplot(311), fig.add_subplot(312), fig.add_subplot(313)]\ncols = [\"RoomService\", \"Spa\", \"VRDeck\"]\nfor i in range(3):\n    axes[i].hist(train[cols[i]][train.Transported == 1], color=\"green\", alpha=0.5, label=\"Transported\")\n    axes[i].hist(train[cols[i]][train.Transported == 0], color=\"red\", alpha=0.5, bins = 50, label=\"Not transported\")\n    axes[i].set_xlabel(cols[i])\n    axes[i].legend()\n\nfig.set_figheight(8)\nfig.set_figwidth(15)\nplt.subplots_adjust(wspace=0.5, hspace=0.6)\nplt.show()\nplt.close()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:24.111916Z","iopub.execute_input":"2022-07-15T12:12:24.112339Z","iopub.status.idle":"2022-07-15T12:12:25.031978Z","shell.execute_reply.started":"2022-07-15T12:12:24.112306Z","shell.execute_reply":"2022-07-15T12:12:25.030859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\naxes = [fig.add_subplot(211), fig.add_subplot(212)]\ncols = [\"FoodCourt\", \"ShoppingMall\"]\nfor i in range(2):\n    axes[i].hist(train[cols[i]][train.Transported == 1], color=\"green\", alpha=0.5, bins = 70, label=\"Transported\")\n    axes[i].hist(train[cols[i]][train.Transported == 0], color=\"red\", alpha=0.5, bins = 50, label=\"Not transported\")\n    axes[i].set_xlabel(cols[i])\n    axes[i].legend()\n\nfig.set_figheight(8)\nfig.set_figwidth(15)\nplt.subplots_adjust(wspace=0.5, hspace=0.6)\nplt.show()\nplt.close()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:25.033470Z","iopub.execute_input":"2022-07-15T12:12:25.033801Z","iopub.status.idle":"2022-07-15T12:12:26.121213Z","shell.execute_reply.started":"2022-07-15T12:12:25.033771Z","shell.execute_reply":"2022-07-15T12:12:26.120080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Histograms of features of transported and not transported passengers differ significantly on RoomService, Spa, VRDeck and not such significantly on FoodCourt, ShoppingMall","metadata":{}},{"cell_type":"code","source":"train.Cabin.unique().shape[0], train.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:12:26.122967Z","iopub.execute_input":"2022-07-15T12:12:26.123996Z","iopub.status.idle":"2022-07-15T12:12:26.135206Z","shell.execute_reply.started":"2022-07-15T12:12:26.123948Z","shell.execute_reply":"2022-07-15T12:12:26.133838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most cabins are unique, so by itself this column is noninformative, but we can reduce it to deck and side of cabin as different features","metadata":{}},{"cell_type":"code","source":"def deck_side_encode(df):\n    \"\"\"\n    Splits Cabin feature itno two features: Side of cabin and Deck of cabin\n    returns copy of argument with two new columns Side and Deck and deleted Cabin column\n    \"\"\"\n    split_cabin = pd.DataFrame(list(map(lambda x: str(x).split(\"/\"), df.Cabin)))\n    new_df = df.copy()\n    new_df[\"Side\"] = split_cabin[2]\n    new_df[\"Deck\"] = split_cabin[0]\n    new_df = new_df.drop([\"Cabin\"], axis=1)\n    return new_df","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:20:50.486442Z","iopub.execute_input":"2022-07-15T12:20:50.486861Z","iopub.status.idle":"2022-07-15T12:20:50.494048Z","shell.execute_reply.started":"2022-07-15T12:20:50.486812Z","shell.execute_reply":"2022-07-15T12:20:50.492886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dse = deck_side_encode(train)\ndse.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:14:15.872014Z","iopub.execute_input":"2022-07-15T12:14:15.872526Z","iopub.status.idle":"2022-07-15T12:14:15.928485Z","shell.execute_reply.started":"2022-07-15T12:14:15.872480Z","shell.execute_reply":"2022-07-15T12:14:15.927358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"some numbers of cabiins are met more frequently, so they could be informative","metadata":{}},{"cell_type":"code","source":"train.HomePlanet.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:16:55.521501Z","iopub.execute_input":"2022-07-14T14:16:55.522246Z","iopub.status.idle":"2022-07-14T14:16:55.535134Z","shell.execute_reply.started":"2022-07-14T14:16:55.522196Z","shell.execute_reply":"2022-07-14T14:16:55.533830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.Destination.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:16:55.537373Z","iopub.execute_input":"2022-07-14T14:16:55.538445Z","iopub.status.idle":"2022-07-14T14:16:55.549250Z","shell.execute_reply.started":"2022-07-14T14:16:55.538388Z","shell.execute_reply":"2022-07-14T14:16:55.548098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"One Hot Encoding should work good for Home Planet and Destination columns","metadata":{}},{"cell_type":"code","source":"pd.get_dummies(train, dummy_na=True, columns=[\"HomePlanet\", \"Destination\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:16:55.551089Z","iopub.execute_input":"2022-07-14T14:16:55.552074Z","iopub.status.idle":"2022-07-14T14:16:55.597486Z","shell.execute_reply.started":"2022-07-14T14:16:55.552016Z","shell.execute_reply":"2022-07-14T14:16:55.596172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_enc = pd.get_dummies(deck_side_encode(train), dummy_na=True, columns=[\"HomePlanet\", \"Destination\", \"Side\", \"Deck\"])\ntrain_enc.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:16:55.599187Z","iopub.execute_input":"2022-07-14T14:16:55.599587Z","iopub.status.idle":"2022-07-14T14:16:55.663658Z","shell.execute_reply.started":"2022-07-14T14:16:55.599545Z","shell.execute_reply":"2022-07-14T14:16:55.662116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/spaceship-titanic/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/spaceship-titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:19:23.511775Z","iopub.execute_input":"2022-07-15T12:19:23.512224Z","iopub.status.idle":"2022-07-15T12:19:23.567945Z","shell.execute_reply.started":"2022-07-15T12:19:23.512188Z","shell.execute_reply":"2022-07-15T12:19:23.566804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fill Nan's with means and modes","metadata":{}},{"cell_type":"code","source":"def data_prep(tr, te, drop=None):\n    tr_no_na = tr.dropna()\n    drop_arr = [\"Name\", \"PassengerId\"]\n    tr_1 = tr.drop(drop_arr, axis=1)\n    te_1 = te.drop(drop_arr, axis=1)\n    value = {\n        \"CryoSleep\": tr_no_na.CryoSleep.mode()[0],\n        \"Age\" : tr_no_na.Age.mean(),\n        \"VIP\" : tr_no_na.VIP.mode()[0],\n        \"RoomService\" : tr_no_na.RoomService.mean(),\n        \"FoodCourt\" : tr_no_na.FoodCourt.mean(),\n        \"ShoppingMall\" : tr_no_na.ShoppingMall.mean(),\n        \"Spa\" : tr_no_na.Spa.mean(),\n        \"VRDeck\" : tr_no_na.VRDeck.mean()\n    }\n    tr_1 = tr_1.fillna(value=value)\n    te_1 = te_1.fillna(value=value)\n    tr_1[\"Transported\"] = tr_1[\"Transported\"].astype(int)\n    tr_1[\"CryoSleep\"] = tr_1[\"CryoSleep\"].astype(int)\n    tr_1[\"VIP\"] = tr_1[\"VIP\"].astype(int)\n    te_1[\"CryoSleep\"] = te_1[\"CryoSleep\"].astype(int)\n    te_1[\"VIP\"] = te_1[\"VIP\"].astype(int)\n    tr_1 = pd.get_dummies(deck_side_encode(tr_1), dummy_na=True, columns=[\"HomePlanet\", \"Destination\", \"Side\", \"Deck\"])\n    te_1 = pd.get_dummies(deck_side_encode(te_1), dummy_na=True, columns=[\"HomePlanet\", \"Destination\", \"Side\", \"Deck\"])\n    return tr_1, te_1","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:20:53.313049Z","iopub.execute_input":"2022-07-15T12:20:53.313642Z","iopub.status.idle":"2022-07-15T12:20:53.325535Z","shell.execute_reply.started":"2022-07-15T12:20:53.313609Z","shell.execute_reply":"2022-07-15T12:20:53.324519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_prep,  test_prep = data_prep(train, test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:26:10.528611Z","iopub.execute_input":"2022-07-15T12:26:10.529023Z","iopub.status.idle":"2022-07-15T12:26:10.601669Z","shell.execute_reply.started":"2022-07-15T12:26:10.528988Z","shell.execute_reply":"2022-07-15T12:26:10.600835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sum(train_prep == None)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:26:10.827970Z","iopub.execute_input":"2022-07-15T12:26:10.828377Z","iopub.status.idle":"2022-07-15T12:26:10.840729Z","shell.execute_reply.started":"2022-07-15T12:26:10.828342Z","shell.execute_reply":"2022-07-15T12:26:10.839620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's predict","metadata":{}},{"cell_type":"code","source":"X_train = np.array(train_prep.drop([\"Transported\"], axis=1))\ny_train = np.array(train_prep.Transported)\nX_test = np.array(test_prep)\nX_train.shape, X_test.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:26:11.243174Z","iopub.execute_input":"2022-07-15T12:26:11.243799Z","iopub.status.idle":"2022-07-15T12:26:11.257798Z","shell.execute_reply.started":"2022-07-15T12:26:11.243764Z","shell.execute_reply":"2022-07-15T12:26:11.257027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe = make_pipeline(StandardScaler(), LogisticRegression())\ncross_val_score(pipe, X_train, y_train, cv=3, scoring=\"accuracy\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:26:14.438314Z","iopub.execute_input":"2022-07-15T12:26:14.438722Z","iopub.status.idle":"2022-07-15T12:26:14.585883Z","shell.execute_reply.started":"2022-07-15T12:26:14.438687Z","shell.execute_reply":"2022-07-15T12:26:14.584628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(DecisionTreeClassifier(max_depth = 10), X_train, y_train, cv=3, scoring=\"accuracy\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:26:16.644537Z","iopub.execute_input":"2022-07-15T12:26:16.645214Z","iopub.status.idle":"2022-07-15T12:26:16.728313Z","shell.execute_reply.started":"2022-07-15T12:26:16.645171Z","shell.execute_reply":"2022-07-15T12:26:16.727129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe = make_pipeline(StandardScaler(), NuSVC())\ncross_val_score(pipe, X_train, y_train, cv=3, scoring=\"accuracy\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:43:50.503485Z","iopub.execute_input":"2022-07-15T12:43:50.503911Z","iopub.status.idle":"2022-07-15T12:43:59.019836Z","shell.execute_reply.started":"2022-07-15T12:43:50.503866Z","shell.execute_reply":"2022-07-15T12:43:59.018577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies = []\nnus = [i/100 for i in range(5, 90, 20)]\nfor nu in tqdm(nus):\n    pipe = make_pipeline(StandardScaler(), NuSVC(nu=nu))\n    accuracies.append(cross_val_score(pipe, X_train, y_train, cv=3, scoring=\"accuracy\").mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:14:26.532834Z","iopub.execute_input":"2022-07-15T13:14:26.533777Z","iopub.status.idle":"2022-07-15T13:17:22.076969Z","shell.execute_reply.started":"2022-07-15T13:14:26.533740Z","shell.execute_reply":"2022-07-15T13:17:22.075842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(nus, accuracies)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:17:42.674723Z","iopub.execute_input":"2022-07-15T13:17:42.675752Z","iopub.status.idle":"2022-07-15T13:17:42.866128Z","shell.execute_reply.started":"2022-07-15T13:17:42.675713Z","shell.execute_reply":"2022-07-15T13:17:42.864931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies = []\nkernels = ['linear', 'poly', 'rbf', \"sigmoid\"]\nfor kernel in tqdm(kernels):\n    pipe = make_pipeline(StandardScaler(), NuSVC(kernel=kernel))\n    accuracies.append(cross_val_score(pipe, X_train, y_train, cv=3, scoring=\"accuracy\").mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:21:54.890771Z","iopub.execute_input":"2022-07-15T13:21:54.891179Z","iopub.status.idle":"2022-07-15T13:22:27.500559Z","shell.execute_reply.started":"2022-07-15T13:21:54.891146Z","shell.execute_reply":"2022-07-15T13:22:27.497551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(kernels, accuracies)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:22:27.502366Z","iopub.execute_input":"2022-07-15T13:22:27.503521Z","iopub.status.idle":"2022-07-15T13:22:27.658350Z","shell.execute_reply.started":"2022-07-15T13:22:27.503476Z","shell.execute_reply":"2022-07-15T13:22:27.657248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies = []\nneighbors = [i for i in range(20, 50, 2)]\nfor n in tqdm(neighbors):\n    accuracies.append(cross_val_score(KNeighborsClassifier(n), X_train, y_train, cv=3, scoring=\"accuracy\").mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:09:08.132721Z","iopub.execute_input":"2022-07-15T13:09:08.133423Z","iopub.status.idle":"2022-07-15T13:09:30.713865Z","shell.execute_reply.started":"2022-07-15T13:09:08.133385Z","shell.execute_reply":"2022-07-15T13:09:30.712821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(neighbors, accuracies)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:09:36.261395Z","iopub.execute_input":"2022-07-15T13:09:36.261783Z","iopub.status.idle":"2022-07-15T13:09:36.438428Z","shell.execute_reply.started":"2022-07-15T13:09:36.261749Z","shell.execute_reply":"2022-07-15T13:09:36.437345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"estimators = [\n    ('xgb', xgb.XGBClassifier(max_depth=3, n_estimators=91, subsample=0.65, colsample_bytree=0.65, eval_metric=accuracy_score)),\n    ('tree', DecisionTreeClassifier(max_depth = 10)),\n    ('nusvc', make_pipeline(StandardScaler(), NuSVC())),\n    ('knn', KNeighborsClassifier(n_neighbors=38))\n]\nclf = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:10:09.613498Z","iopub.execute_input":"2022-07-15T13:10:09.614517Z","iopub.status.idle":"2022-07-15T13:10:09.621566Z","shell.execute_reply.started":"2022-07-15T13:10:09.614477Z","shell.execute_reply":"2022-07-15T13:10:09.620360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(clf, X_train, y_train, cv=3, scoring=\"accuracy\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:10:10.092693Z","iopub.execute_input":"2022-07-15T13:10:10.093430Z","iopub.status.idle":"2022-07-15T13:10:56.369115Z","shell.execute_reply.started":"2022-07-15T13:10:10.093389Z","shell.execute_reply":"2022-07-15T13:10:56.367669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"estimators = [\n    ('xgb', xgb.XGBClassifier(max_depth=3, n_estimators=91, subsample=0.65, colsample_bytree=0.65, eval_metric=accuracy_score)),\n    ('tree', DecisionTreeClassifier(max_depth = 10)),\n    ('nusvc', make_pipeline(StandardScaler(), NuSVC())),\n    ('knn', KNeighborsClassifier(n_neighbors=38))\n]\nclf = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:25:30.430551Z","iopub.execute_input":"2022-07-15T13:25:30.430981Z","iopub.status.idle":"2022-07-15T13:25:58.893190Z","shell.execute_reply.started":"2022-07-15T13:25:30.430935Z","shell.execute_reply":"2022-07-15T13:25:58.892088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:25:58.895758Z","iopub.execute_input":"2022-07-15T13:25:58.896550Z","iopub.status.idle":"2022-07-15T13:26:01.525731Z","shell.execute_reply.started":"2022-07-15T13:25:58.896507Z","shell.execute_reply":"2022-07-15T13:26:01.524428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_subm = pd.read_csv(\"/kaggle/input/spaceship-titanic/sample_submission.csv\")\nsample_subm.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:26:01.528130Z","iopub.execute_input":"2022-07-15T13:26:01.529091Z","iopub.status.idle":"2022-07-15T13:26:01.556764Z","shell.execute_reply.started":"2022-07-15T13:26:01.529040Z","shell.execute_reply":"2022-07-15T13:26:01.555586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_subm[\"Transported\"] = pred.astype(bool)\nsample_subm.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T13:26:01.559824Z","iopub.execute_input":"2022-07-15T13:26:01.560697Z","iopub.status.idle":"2022-07-15T13:26:01.574863Z","shell.execute_reply.started":"2022-07-15T13:26:01.560649Z","shell.execute_reply":"2022-07-15T13:26:01.573691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_subm.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T15:12:05.329565Z","iopub.execute_input":"2022-07-14T15:12:05.330024Z","iopub.status.idle":"2022-07-14T15:12:05.348648Z","shell.execute_reply.started":"2022-07-14T15:12:05.329988Z","shell.execute_reply":"2022-07-14T15:12:05.347678Z"},"trusted":true},"execution_count":null,"outputs":[]}]}