{"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":"# What is this?\n\nThe following is a short script aimed to implement this competition's evaluation function.\nIf you have ideas to make this function better - **awesome**! \n\nIf you found this helpful please consider upvoting this notebook :)\n","metadata":{}},{"cell_type":"code","source":"# (x1, y1), (x2, y2) --> distance in km \n!pip install vincenty","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:45:51.98131Z","iopub.execute_input":"2021-05-25T12:45:51.981735Z","iopub.status.idle":"2021-05-25T12:45:58.743979Z","shell.execute_reply.started":"2021-05-25T12:45:51.9817Z","shell.execute_reply":"2021-05-25T12:45:58.7428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn import preprocessing\nimport lightgbm as lgbm\nfrom vincenty import vincenty\n\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:45:58.746022Z","iopub.execute_input":"2021-05-25T12:45:58.746424Z","iopub.status.idle":"2021-05-25T12:45:58.755081Z","shell.execute_reply.started":"2021-05-25T12:45:58.746376Z","shell.execute_reply":"2021-05-25T12:45:58.753855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nSubmissions are scored on the mean of the 50th and 95th percentile distance errors.\nFor every phone and at every millisSinceGpsEpoch,\nthe horizontal distance (in meters) is computed between the predicted lat/lng and the ground truth lat/lng.\nThese distance errors form a distribution from which the 50th and 95th percentile errors are calculated \n(i.e. the 95th percentile error is the value, in meters, for which 95% of the distance errors are smaller).\nThe 50th and 95th percentile errors are then averaged for each phone.\nLastly, the mean of these averaged values is calculated across all phones in the test set.\n\"\"\"\npred_cols = [\"predsLatDeg\", \"predsLngDeg\"]\nreal_cols = [\"realLatDeg\", \"realLngDeg\"]\n\ndef apply_vincenty(row: pd.DataFrame):\n    return vincenty(row[real_cols].values, row[pred_cols].values)\n\ndef distance(group: pd.DataFrame):\n    errors = group.apply(apply_vincenty, axis = 1) * 1000 #(km to meters)\n    \n    p_50 = np.percentile(errors, 50)\n    p_95 = np.percentile(errors, 95)\n    return (p_50 + p_95)/2\n\n    \ndef evaluation(phones: pd.Series, predsLat: pd.Series, predsLan: pd.Series, realLat: pd.Series, realLan: pd.Series):\n    preds_df = pd.DataFrame({\"latDeg\": predsLat, \"lngDeg\": predsLan})\n    real_df = pd.DataFrame({\"latDeg\": realLat, \"lngDeg\": realLan})\n    all_df = pd.DataFrame({\"realLatDeg\": realLat, \"realLngDeg\": realLan, \"predsLatDeg\": predsLat, \"predsLngDeg\": predsLan,\"phone\": phones})\n    cols=['latDeg','lngDeg']\n    \n    errors_per_phone = all_df.groupby(\"phone\").apply(distance)\n    return errors_per_phone.mean()\n    \n    \n\n    ","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:45:58.757097Z","iopub.execute_input":"2021-05-25T12:45:58.757452Z","iopub.status.idle":"2021-05-25T12:45:58.771185Z","shell.execute_reply.started":"2021-05-25T12:45:58.757418Z","shell.execute_reply":"2021-05-25T12:45:58.769946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/google-smartphone-decimeter-challenge/baseline_locations_train.csv\")\nbl_test =  pd.read_csv(\"../input/google-smartphone-decimeter-challenge/baseline_locations_test.csv\")\nsample_submission = pd.read_csv(\"../input/google-smartphone-decimeter-challenge/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:45:58.773352Z","iopub.execute_input":"2021-05-25T12:45:58.773934Z","iopub.status.idle":"2021-05-25T12:45:59.180716Z","shell.execute_reply.started":"2021-05-25T12:45:58.773887Z","shell.execute_reply":"2021-05-25T12:45:59.179559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make ground_truth_df from all ground_truth files:\n#  collectionName*phoneName, latDeg, lngDeg, millisSinceGpsEpoch\n# merge to train on collectionName*phoneName (phone) and millisSinceGpsEpoch\ni = 0\nground_truths_df = pd.DataFrame()\nfor path,dir_names,_ in  os.walk(\"../input/google-smartphone-decimeter-challenge/train\"):\n    if i != 0 and 'supplemental' not in dir_names:\n        for phone in dir_names:\n            temp_df =  pd.read_csv(\"{}/{}/ground_truth.csv\".format(path, phone))\n            temp_df[\"phone\"] = temp_df[\"collectionName\"] + \"_\" + temp_df[\"phoneName\"]\n            temp_df = temp_df[[ \"phone\", \"latDeg\", \"lngDeg\", \"millisSinceGpsEpoch\"]]\n            ground_truths_df = ground_truths_df.append(temp_df)\n    \n    i = i + 1\nground_truths_df = ground_truths_df.reset_index().drop(\"index\", axis = 1)\ntrain = train.merge(ground_truths_df, on =[\"phone\",\"millisSinceGpsEpoch\"],suffixes = (\"\", \"_y\"))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:45:59.182533Z","iopub.execute_input":"2021-05-25T12:45:59.182951Z","iopub.status.idle":"2021-05-25T12:46:00.440686Z","shell.execute_reply.started":"2021-05-25T12:45:59.182919Z","shell.execute_reply":"2021-05-25T12:46:00.439514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# suppose to be ~7 (compare ground truth of train to the small antena's latDeg, lngDeg)\nevaluation(train[\"phoneName\"],\n           train[\"latDeg_y\"],train[\"lngDeg_y\"],\n           train[\"latDeg\"], train[\"lngDeg\"])\n","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:46:00.444581Z","iopub.execute_input":"2021-05-25T12:46:00.444902Z","iopub.status.idle":"2021-05-25T12:47:55.758904Z","shell.execute_reply.started":"2021-05-25T12:46:00.444874Z","shell.execute_reply":"2021-05-25T12:47:55.75778Z"},"trusted":true},"execution_count":null,"outputs":[]}]}