{"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","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:23:26.718596Z","iopub.execute_input":"2022-07-09T18:23:26.718965Z","iopub.status.idle":"2022-07-09T18:23:26.723495Z","shell.execute_reply.started":"2022-07-09T18:23:26.718924Z","shell.execute_reply":"2022-07-09T18:23:26.722801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read data\ntrain = pd.read_csv('/kaggle/input/foursquare-location-matching/train.csv')\n#train = pd.read_csv('../input/the-real-train-for-foursquare/train.csv')\ntest = pd.read_csv('/kaggle/input/foursquare-location-matching/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:25:56.937347Z","iopub.execute_input":"2022-07-09T18:25:56.938535Z","iopub.status.idle":"2022-07-09T18:26:03.3116Z","shell.execute_reply.started":"2022-07-09T18:25:56.938481Z","shell.execute_reply":"2022-07-09T18:26:03.310977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use name/address/longitude/latitude as key to match \"point of interest\" column to test.csv\nexact_match = train.groupby(['name','longitude','latitude'],as_index=False)['point_of_interest'].first()\ntest = test.merge(exact_match,how='left')\ntest.loc[:,['id','point_of_interest']]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:26:03.3128Z","iopub.execute_input":"2022-07-09T18:26:03.313389Z","iopub.status.idle":"2022-07-09T18:26:10.183322Z","shell.execute_reply.started":"2022-07-09T18:26:03.313356Z","shell.execute_reply":"2022-07-09T18:26:10.182274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we generate answer based on following principle\n# if two entries have same \"point of interest\" value as indicated by train.csv, they are put into the same group\ntest['point_of_interest'] = test['point_of_interest'].fillna(test['id'])\npoi_2_match = test.groupby(['point_of_interest'])['id'].apply(lambda x:' '.join(x)).to_dict()\ntest['matches'] = test['point_of_interest'].apply(lambda x:poi_2_match[x])\ntest.loc[:,['id','point_of_interest','matches']]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:26:16.427444Z","iopub.execute_input":"2022-07-09T18:26:16.42779Z","iopub.status.idle":"2022-07-09T18:26:16.446471Z","shell.execute_reply.started":"2022-07-09T18:26:16.427753Z","shell.execute_reply":"2022-07-09T18:26:16.445632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ssub = pd.read_csv(\"../input/foursquare-location-matching/sample_submission.csv\")\nssub = ssub.drop(columns=\"matches\")\nssub = ssub.merge(test.loc[:,[\"id\",\"matches\"]])\nssub.to_csv(\"submission.csv\", index=False)\n\nssub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:26:19.913732Z","iopub.execute_input":"2022-07-09T18:26:19.91405Z","iopub.status.idle":"2022-07-09T18:26:19.934411Z","shell.execute_reply.started":"2022-07-09T18:26:19.914018Z","shell.execute_reply":"2022-07-09T18:26:19.933505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}