{"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":"# Leakage Submission v3\n\nAs it seems like different `id`s are assigned to train/test dataset, I defined as \"leaks\" as such:\n- the triplet (`name`, `lat`, `lon`) are exactly the same","metadata":{}},{"cell_type":"code","source":"from math import nan, isnan\n\nimport pandas as pd\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-09T03:53:31.699882Z","iopub.status.idle":"2022-07-09T03:53:31.701259Z","shell.execute_reply.started":"2022-07-09T03:53:31.700914Z","shell.execute_reply":"2022-07-09T03:53:31.700952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\n    \"../input/4sq-leakage-train-data/train.csv/train.csv\",\n    usecols=[\"name\", \"latitude\", \"longitude\", \"point_of_interest\"]\n)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:57:51.228544Z","iopub.execute_input":"2022-07-09T03:57:51.229783Z","iopub.status.idle":"2022-07-09T03:57:55.415772Z","shell.execute_reply.started":"2022-07-09T03:57:51.229733Z","shell.execute_reply":"2022-07-09T03:57:55.413536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop_duplicates(subset=[\"name\", \"latitude\", \"longitude\"], inplace=True)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:56:08.49024Z","iopub.execute_input":"2022-07-09T03:56:08.490681Z","iopub.status.idle":"2022-07-09T03:56:09.447705Z","shell.execute_reply.started":"2022-07-09T03:56:08.490646Z","shell.execute_reply":"2022-07-09T03:56:09.446486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/foursquare-location-matching/test.csv\", usecols=[\"name\", \"id\", \"latitude\", \"longitude\"])\ntest = test.merge(train, on=(\"name\", \"latitude\", \"longitude\"), how=\"left\")\ntest","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:56:15.573533Z","iopub.execute_input":"2022-07-09T03:56:15.574365Z","iopub.status.idle":"2022-07-09T03:56:23.951745Z","shell.execute_reply.started":"2022-07-09T03:56:15.574317Z","shell.execute_reply":"2022-07-09T03:56:23.950362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"poi2matches = test.groupby(\"point_of_interest\")[\"id\"].agg(lambda x: \" \".join(x)).to_dict()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:56:31.6227Z","iopub.execute_input":"2022-07-09T03:56:31.623152Z","iopub.status.idle":"2022-07-09T03:56:42.635614Z","shell.execute_reply.started":"2022-07-09T03:56:31.623119Z","shell.execute_reply":"2022-07-09T03:56:42.632599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_matches_if_exist(x):\n    if type(x) == float and isnan(x):\n        return nan\n    return poi2matches[x]\n\ntest[\"matches\"] = test[\"point_of_interest\"].apply(get_matches_if_exist)\ntest","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:56:44.270889Z","iopub.execute_input":"2022-07-09T03:56:44.27135Z","iopub.status.idle":"2022-07-09T03:56:45.567207Z","shell.execute_reply.started":"2022-07-09T03:56:44.271309Z","shell.execute_reply":"2022-07-09T03:56:45.566228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = test[[\"id\", \"matches\"]]\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:56:48.647495Z","iopub.execute_input":"2022-07-09T03:56:48.647984Z","iopub.status.idle":"2022-07-09T03:56:49.047879Z","shell.execute_reply.started":"2022-07-09T03:56:48.647939Z","shell.execute_reply":"2022-07-09T03:56:49.046591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"matches\"].fillna(submission[\"id\"], inplace=True)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:56:52.112516Z","iopub.execute_input":"2022-07-09T03:56:52.113669Z","iopub.status.idle":"2022-07-09T03:56:52.369124Z","shell.execute_reply.started":"2022-07-09T03:56:52.113618Z","shell.execute_reply":"2022-07-09T03:56:52.367911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\npd.read_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-09T03:56:55.499926Z","iopub.execute_input":"2022-07-09T03:56:55.500395Z","iopub.status.idle":"2022-07-09T03:57:02.857016Z","shell.execute_reply.started":"2022-07-09T03:56:55.500355Z","shell.execute_reply":"2022-07-09T03:57:02.855791Z"},"trusted":true},"execution_count":null,"outputs":[]}]}