{"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":"# Average top public notebooks\n\nWeighted mean in cartesian coordinates at equal altitude.\n\n**All credit to the authors of those notebooks; please look at their work:**\n* [GSDC phones mean prediction](https://www.kaggle.com/t88take/gsdc-phones-mean-prediction)\n* [device EDA & Interpolate by removing device[en,ja]](https://www.kaggle.com/columbia2131/device-eda-interpolate-by-removing-device-en-ja)\n* [GSDC: Position shift](https://www.kaggle.com/wrrosa/gsdc-position-shift)","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy\nimport pandas\nimport wgs_ecef","metadata":{"_uuid":"9955fe5e-58da-4178-8680-c0acec0bbd93","_cell_guid":"50aef91c-6606-489e-9b1f-a1d28f6c2659","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-06-08T07:52:53.739227Z","iopub.execute_input":"2021-06-08T07:52:53.739714Z","iopub.status.idle":"2021-06-08T07:52:53.743690Z","shell.execute_reply.started":"2021-06-08T07:52:53.739682Z","shell.execute_reply":"2021-06-08T07:52:53.742700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def blend(folder_to_weight):\n    \"\"\" Return a weighted mean of folders' submissions. \n    \n        Assumes equal ordering to submission files' rows.\n    \"\"\"\n    norm = sum(folder_to_weight.values())\n\n    def submissions():\n        for folder, weight in folder_to_weight.items():\n            frame = pandas.read_csv(\n                os.path.join(\"../input/\", folder, \"submission.csv\"),\n                dtype={\n                    \"phone\": str,\n                    \"millisSinceGpsEpoch\": numpy.uint64,\n                    \"latDeg\": numpy.float64,\n                    \"lngDeg\": numpy.float64,\n                })\n            yield frame, weight / norm\n\n    subs = submissions()\n\n    # add in ECEF coordinates at 0 altitude\n    example, weight = next(subs)\n    xyz = get_ecef(example) * weight\n\n    for frame, weight in subs:\n        xyz += get_ecef(frame) * weight\n\n    # convert back to WSG, update example in place\n    example.latDeg, example.lngDeg, _ = wgs_ecef.ecef_to_wgs(*xyz)\n\n    return example\n\n\ndef get_ecef(frame):\n    \"\"\" Return ECEF positions from frame WGS at sea level. \"\"\"\n    lat = frame.latDeg\n    lng = frame.lngDeg\n    alt = numpy.zeros_like(frame.latDeg)\n    return numpy.stack(wgs_ecef.wgs_to_ecef(lat, lng, alt))","metadata":{"execution":{"iopub.status.busy":"2021-06-08T07:52:53.744937Z","iopub.execute_input":"2021-06-08T07:52:53.745279Z","iopub.status.idle":"2021-06-08T07:52:53.759076Z","shell.execute_reply.started":"2021-06-08T07:52:53.745249Z","shell.execute_reply":"2021-06-08T07:52:53.757894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = blend({\n    \"gsdc-phones-mean-prediction\": 5,\n    \"device-eda-interpolate-by-removing-device-en-ja\": 1,\n    \"gsdc-position-shift\": 1,\n})\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-08T07:52:53.760820Z","iopub.execute_input":"2021-06-08T07:52:53.761364Z","iopub.status.idle":"2021-06-08T07:52:54.169527Z","shell.execute_reply.started":"2021-06-08T07:52:53.761325Z","shell.execute_reply":"2021-06-08T07:52:54.168805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T07:52:54.170638Z","iopub.execute_input":"2021-06-08T07:52:54.171037Z","iopub.status.idle":"2021-06-08T07:52:54.803108Z","shell.execute_reply.started":"2021-06-08T07:52:54.171007Z","shell.execute_reply":"2021-06-08T07:52:54.802342Z"},"trusted":true},"execution_count":null,"outputs":[]}]}