{"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":"!pip install umap-learn","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:00:29.316517Z","iopub.execute_input":"2022-07-04T15:00:29.317299Z","iopub.status.idle":"2022-07-04T15:00:41.102501Z","shell.execute_reply.started":"2022-07-04T15:00:29.317219Z","shell.execute_reply":"2022-07-04T15:00:41.100949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport umap","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-04T15:00:41.105432Z","iopub.execute_input":"2022-07-04T15:00:41.105902Z","iopub.status.idle":"2022-07-04T15:00:41.113867Z","shell.execute_reply.started":"2022-07-04T15:00:41.105863Z","shell.execute_reply":"2022-07-04T15:00:41.112576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:00:41.115722Z","iopub.execute_input":"2022-07-04T15:00:41.116161Z","iopub.status.idle":"2022-07-04T15:00:41.165566Z","shell.execute_reply.started":"2022-07-04T15:00:41.116110Z","shell.execute_reply":"2022-07-04T15:00:41.164270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:00:41.169982Z","iopub.execute_input":"2022-07-04T15:00:41.170421Z","iopub.status.idle":"2022-07-04T15:00:41.198953Z","shell.execute_reply.started":"2022-07-04T15:00:41.170386Z","shell.execute_reply":"2022-07-04T15:00:41.197639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\"]\nX_train = pd.get_dummies(train_data[features]).to_numpy()\nY_train = train_data[\"Survived\"].to_numpy()\n\nX_test = pd.get_dummies(test_data[features]).to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:00:41.200500Z","iopub.execute_input":"2022-07-04T15:00:41.200872Z","iopub.status.idle":"2022-07-04T15:00:41.226147Z","shell.execute_reply.started":"2022-07-04T15:00:41.200837Z","shell.execute_reply":"2022-07-04T15:00:41.224579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(X_train.shape)\ndisplay(Y_train.shape)\ndisplay(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:00:41.227649Z","iopub.execute_input":"2022-07-04T15:00:41.228025Z","iopub.status.idle":"2022-07-04T15:00:41.242935Z","shell.execute_reply.started":"2022-07-04T15:00:41.227993Z","shell.execute_reply":"2022-07-04T15:00:41.241318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mapper = umap.UMAP(random_state=42, min_dist=1, spread=3, n_neighbors=18).fit(X_train)\nfit_train = mapper.transform(X_train)\nfit_test = mapper.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:01:42.388536Z","iopub.execute_input":"2022-07-04T15:01:42.389025Z","iopub.status.idle":"2022-07-04T15:01:52.436538Z","shell.execute_reply.started":"2022-07-04T15:01:42.388986Z","shell.execute_reply":"2022-07-04T15:01:52.435104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(1, figsize=(10, 10))\nax = fig.add_subplot(111)\n\nidx = Y_train==0\nax.scatter(fit_train[idx, 0], fit_train[idx, 1], color='C0', s=5, alpha=0.8, label='Survived = 0')\n\nax.scatter(fit_train[~idx, 0], fit_train[~idx, 1], color='C1', s=5, alpha=0.8, label='Survived = 1')\n\nax.scatter(fit_test[:, 0], fit_test[:, 1], color='k', s=2, alpha=0.8, label='Test set')\n\nax.legend()\nax.set_aspect(1);","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:01:52.439607Z","iopub.execute_input":"2022-07-04T15:01:52.440285Z","iopub.status.idle":"2022-07-04T15:01:52.748113Z","shell.execute_reply.started":"2022-07-04T15:01:52.440174Z","shell.execute_reply":"2022-07-04T15:01:52.746159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data = np.vstack([fit_train, fit_test])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:01:52.750331Z","iopub.execute_input":"2022-07-04T15:01:52.750803Z","iopub.status.idle":"2022-07-04T15:01:52.756661Z","shell.execute_reply.started":"2022-07-04T15:01:52.750715Z","shell.execute_reply":"2022-07-04T15:01:52.755317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import DBSCAN","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:01:52.759957Z","iopub.execute_input":"2022-07-04T15:01:52.760441Z","iopub.status.idle":"2022-07-04T15:01:52.770823Z","shell.execute_reply.started":"2022-07-04T15:01:52.760404Z","shell.execute_reply":"2022-07-04T15:01:52.768781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clusters = DBSCAN(min_samples=5, eps=1.5).fit(all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:03:23.762310Z","iopub.execute_input":"2022-07-04T15:03:23.762767Z","iopub.status.idle":"2022-07-04T15:03:23.778546Z","shell.execute_reply.started":"2022-07-04T15:03:23.762734Z","shell.execute_reply":"2022-07-04T15:03:23.777645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.random.seed(75)\nall_labels = np.hstack([Y_train, -1 * np.ones(X_test.shape[0])])\nUDX = all_labels == -1\n\nfig = plt.figure(2, figsize=(10, 10))\nax = fig.add_subplot(111)\n\nfor l in np.unique(clusters.labels_):\n    ldx = clusters.labels_==l\n    if l == -1:\n        color = 'k'\n    else:\n        color = f'C{l % 10}'\n    survived = (all_labels[ldx] == 1).sum()\n    died = (all_labels[ldx] == 0).sum()\n    udx = ldx & UDX\n    unknown = udx.sum()\n\n    # predict unknown labels as a binomial draw based on labeled memebers of the cluster\n#     total = survived + died\n#     if total > 0:\n#         p = survived / total\n#         prediction = np.random.binomial(1, p, unknown)\n#         all_labels[udx] = prediction\n    \n    # predict unknown labels with majority rules\n    if survived >= died:\n        all_labels[udx] = 1\n    else:\n        all_labels[udx] = 0\n    \n    label = f'Cluster {l: 02}; survived {survived: 04}; died {died: 04}; test {unknown: 04}'\n    ax.scatter(all_data[ldx, 0], all_data[ldx, 1], s=5, alpha=0.8, color=color, label=label)\n\nax.legend()\nax.set_aspect(1);","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:04:29.187608Z","iopub.execute_input":"2022-07-04T15:04:29.189643Z","iopub.status.idle":"2022-07-04T15:04:29.768573Z","shell.execute_reply.started":"2022-07-04T15:04:29.189568Z","shell.execute_reply":"2022-07-04T15:04:29.767553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_test = all_labels[X_train.shape[0]:].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:04:58.708928Z","iopub.execute_input":"2022-07-04T15:04:58.709416Z","iopub.status.idle":"2022-07-04T15:04:58.715409Z","shell.execute_reply.started":"2022-07-04T15:04:58.709381Z","shell.execute_reply":"2022-07-04T15:04:58.714304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_test})\noutput.head()\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T15:05:02.652438Z","iopub.execute_input":"2022-07-04T15:05:02.653284Z","iopub.status.idle":"2022-07-04T15:05:02.662436Z","shell.execute_reply.started":"2022-07-04T15:05:02.653240Z","shell.execute_reply":"2022-07-04T15:05:02.661409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}