{"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":"markdown","source":"# I simply fine-tuned the hyperparameters and added comments.\nNote: This notebook is available thanks to the original version: https://www.kaggle.com/code/tanxxx/vote-coordinate-with-nearestneighbor-new","metadata":{}},{"cell_type":"code","source":"# Importing libraries\nimport numpy as np \nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:48:28.421990Z","iopub.execute_input":"2022-07-20T13:48:28.423838Z","iopub.status.idle":"2022-07-20T13:48:29.735072Z","shell.execute_reply.started":"2022-07-20T13:48:28.423737Z","shell.execute_reply":"2022-07-20T13:48:29.731436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading the input data\nSAMPLE = pd.read_csv('../input/smartphone-decimeter-2022/sample_submission.csv')\ndisplay(SAMPLE)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:48:29.737117Z","iopub.execute_input":"2022-07-20T13:48:29.737868Z","iopub.status.idle":"2022-07-20T13:48:29.914977Z","shell.execute_reply.started":"2022-07-20T13:48:29.737813Z","shell.execute_reply":"2022-07-20T13:48:29.914085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading the earlier submissions\npath1 = '../input/gsdc224581/submission.csv' \npath2 = '../input/gsdc224376/submission.csv' \npath3 = '../input/gsdc223355/submission.csv'\npath4 = '../input/carriersmoothingrobust-submission-score-3013/submission.csv'\npath  = [path1, path2, path3, path4]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:48:29.916395Z","iopub.execute_input":"2022-07-20T13:48:29.917610Z","iopub.status.idle":"2022-07-20T13:48:29.923592Z","shell.execute_reply.started":"2022-07-20T13:48:29.917569Z","shell.execute_reply":"2022-07-20T13:48:29.922460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reorganizing the earlier submissions\nQT = [[], [], [], []]\nQN = [[], [], [], []]\n\nfor k in range(len(path)):    \n    sub_k = pd.read_csv(path[k]).values  \n    PT = []\n    PN = []    \n    for j in range(len(SAMPLE)):\n        PT.append([sub_k[j][2]])     \n        PN.append([sub_k[j][3]])   \n    QT[k] = PT  \n    QN[k] = PN  ","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:48:29.925076Z","iopub.execute_input":"2022-07-20T13:48:29.927063Z","iopub.status.idle":"2022-07-20T13:48:31.533706Z","shell.execute_reply.started":"2022-07-20T13:48:29.927012Z","shell.execute_reply":"2022-07-20T13:48:31.531826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A plotting function\ndef near_plt(points, best_score, support, best_1, generated):\n    plt.style.use('seaborn-whitegrid') \n    plt.figure(figsize=(10, 10), facecolor='lightblue')\n    plt.title(f'\\nC O O R D I N A T E\\n\\n{SAMPLE.iloc[i][:2]}')   \n    plt.scatter(points[0], points[1], s=200, facecolor='lightblue', edgecolor='black', label='All Points')\n    plt.scatter(best_score[0], best_score[1], s=200, facecolor='violet', edgecolor='black', label='Best Score')\n    plt.scatter(support[0], support[1], s=200, facecolor='yellow', edgecolor='black', label='Support')    \n    plt.scatter(generated[0], generated[1], s=150, marker='x', label='Generated')\n    plt.scatter(best_1[0], best_1[1], s=150, marker='x', label='Best-1 (To Check)')\n    plt.legend(fontsize=12)\n    plt.xlabel('LatitudeDegrees', fontsize=12)\n    plt.ylabel('LongitudeDegrees', fontsize=12)\n    plt.savefig(f'Coordinate_{i}.png')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:48:31.536468Z","iopub.execute_input":"2022-07-20T13:48:31.536938Z","iopub.status.idle":"2022-07-20T13:48:31.549047Z","shell.execute_reply.started":"2022-07-20T13:48:31.536879Z","shell.execute_reply":"2022-07-20T13:48:31.547967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import NearestNeighbors\nrandom_examples = np.random.randint(len(SAMPLE), size=12) # Number of examples to print\nT = [] # Latitude Degrees\nN = [] # Longitude Degrees\n\nfor i in range(len(SAMPLE)): \n    XT = [QT[0][i], QT[1][i], QT[2][i], QT[3][i]]\n    XN = [QN[0][i], QN[1][i], QN[2][i], QN[3][i]]\n    nbrs = NearestNeighbors(n_neighbors=3, algorithm='auto', p=1, leaf_size=20).fit(XT)    \n    _ , indices_T = nbrs.kneighbors(XT)\n    nbrs = NearestNeighbors(n_neighbors=3, algorithm='auto', p=1, leaf_size=20).fit(XN)    \n    _ , indices_N = nbrs.kneighbors(XN)\n    tt = (1.13 * XT[indices_T[-1][0]][0]) + (-0.16 * XT[indices_T[-1][1]][0]) + (0.03 * XT[indices_T[-1][2]][0])\n    T.append(tt) \n    nn = (1.269 * XN[indices_N[-1][0]][0]) + (-0.259 * XN[indices_N[-1][1]][0]) + (-0.01 * XN[indices_N[-1][2]][0])\n    N.append(nn) ","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-20T13:48:31.550789Z","iopub.execute_input":"2022-07-20T13:48:31.551530Z","iopub.status.idle":"2022-07-20T13:49:23.574919Z","shell.execute_reply.started":"2022-07-20T13:48:31.551486Z","shell.execute_reply":"2022-07-20T13:49:23.573932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating the new submission\nsub = SAMPLE.copy()\nsub['LatitudeDegrees']  = T\nsub['LongitudeDegrees'] = N\nsub","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-07-20T13:49:23.576620Z","iopub.execute_input":"2022-07-20T13:49:23.577071Z","iopub.status.idle":"2022-07-20T13:49:23.622252Z","shell.execute_reply.started":"2022-07-20T13:49:23.577026Z","shell.execute_reply":"2022-07-20T13:49:23.621351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submitting it to the competition\nsub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:49:23.623661Z","iopub.execute_input":"2022-07-20T13:49:23.624104Z","iopub.status.idle":"2022-07-20T13:49:24.102855Z","shell.execute_reply.started":"2022-07-20T13:49:23.624056Z","shell.execute_reply":"2022-07-20T13:49:24.101959Z"},"trusted":true},"execution_count":null,"outputs":[]}]}