{
  "id": 215438,
  "title": "Competition metric with numpy",
  "url": "/competitions/indoor-location-navigation/discussion/215438",
  "author_name": "Tolga",
  "post_date": "2021-01-29T19:42:37.335000",
  "votes": 7,
  "comment_count": 3,
  "views": 0,
  "content": "<p>The competition metric, mean position error, can be calculated with numpy as follows:</p>\n<pre><code>import numpy as np\n\n# Hypothetical data for demonstration\ny_true = [[1, 100, 105], [2, 120, 112], [3, 180, 170]]\ny_pred = [[1, 110, 104], [1, 110, 100], [3, 178, 168]]\n\ndef mpe(y_true, y_pred):\n    y_true = np.asarray(y_true)\n    y_pred = np.asarray(y_pred)\n    diff = y_true - y_pred\n    return np.mean(np.sqrt(np.sum(diff[:, 1:3]**2., axis=1)) + np.abs(diff[:, 0]) * 15)\n\nmpe(y_true, y_pred)\n</code></pre>\n<p>y_true and y_pred are in the form of [[floor, x, y], [floor, x, y], …] as in the sample_submission.csv file.</p>",
  "messages": [
    {
      "id": 1176803,
      "postDate": "2021-01-29T19:42:37.337Z",
      "content": "<p>The competition metric, mean position error, can be calculated with numpy as follows:</p>\n<pre><code>import numpy as np\n\n# Hypothetical data for demonstration\ny_true = [[1, 100, 105], [2, 120, 112], [3, 180, 170]]\ny_pred = [[1, 110, 104], [1, 110, 100], [3, 178, 168]]\n\ndef mpe(y_true, y_pred):\n    y_true = np.asarray(y_true)\n    y_pred = np.asarray(y_pred)\n    diff = y_true - y_pred\n    return np.mean(np.sqrt(np.sum(diff[:, 1:3]**2., axis=1)) + np.abs(diff[:, 0]) * 15)\n\nmpe(y_true, y_pred)\n</code></pre>\n<p>y_true and y_pred are in the form of [[floor, x, y], [floor, x, y], …] as in the sample_submission.csv file.</p>",
      "rawMarkdown": "The competition metric, mean position error, can be calculated with numpy as follows:\n\n```python\nimport numpy as np\n\n# Hypothetical data for demonstration\ny_true = [[1, 100, 105], [2, 120, 112], [3, 180, 170]]\ny_pred = [[1, 110, 104], [1, 110, 100], [3, 178, 168]]\n\ndef mpe(y_true, y_pred):\n    y_true = np.asarray(y_true)\n    y_pred = np.asarray(y_pred)\n    diff = y_true - y_pred\n    return np.mean(np.sqrt(np.sum(diff[:, 1:3]**2., axis=1)) + np.abs(diff[:, 0]) * 15)\n\nmpe(y_true, y_pred)\n```\n\ny_true and y_pred are in the form of [[floor, x, y], [floor, x, y], ...] as in the sample_submission.csv file.",
      "votes": 7
    },
    {
      "id": 1176867,
      "postDate": "2021-01-29T21:08:06.043Z",
      "content": "<p>Cool! Here's a version to use if you already have dataframes for your submission and the ground truth. If you don't use the first column as index, just slice/drop it from the dataframes.</p>\n<pre><code>diff = sub_df.to_numpy() - truth_df.to_numpy()\nspace_error = np.linalg.norm(diff[:,-2:], axis=1)\nfloor_error = np.abs(diff[:,-3])\nscore = np.mean(space_error + 15*floor_error)\n</code></pre>\n<p><code>np.einsum()</code> is another option that is very fast and clean, but I find it less intuitive. This post compares various methods: <a href=\"https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy\" target=\"_blank\">https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy</a></p>",
      "rawMarkdown": "Cool! Here's a version to use if you already have dataframes for your submission and the ground truth. If you don't use the first column as index, just slice/drop it from the dataframes.\n\n```\ndiff = sub_df.to_numpy() - truth_df.to_numpy()\nspace_error = np.linalg.norm(diff[:,-2:], axis=1)\nfloor_error = np.abs(diff[:,-3])\nscore = np.mean(space_error + 15*floor_error)\n```\n\n`np.einsum()` is another option that is very fast and clean, but I find it less intuitive. This post compares various methods: https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy",
      "votes": 2,
      "replies": [
        {
          "id": 1176885,
          "postDate": "2021-01-29T21:32:18.857Z",
          "content": "<p>I was wondering comparison of different methods. Thank you for the link!</p>\n<p>Indeed, <code>np.einsum</code> looks pretty fast. I think all methods are fine, except <code>scipy.spatial.distance</code>.</p>",
          "rawMarkdown": "I was wondering comparison of different methods. Thank you for the link!\n\nIndeed, `np.einsum` looks pretty fast. I think all methods are fine, except `scipy.spatial.distance`."
        }
      ]
    },
    {
      "id": 1244213,
      "postDate": "2021-03-18T19:15:16.497Z",
      "rawMarkdown": "",
      "votes": 1,
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 1176867,
      "author_name": "JohnM",
      "author_url": "",
      "post_date": "2021-01-29T21:08:06.043000",
      "content": "<p>Cool! Here's a version to use if you already have dataframes for your submission and the ground truth. If you don't use the first column as index, just slice/drop it from the dataframes.</p>\n<pre><code>diff = sub_df.to_numpy() - truth_df.to_numpy()\nspace_error = np.linalg.norm(diff[:,-2:], axis=1)\nfloor_error = np.abs(diff[:,-3])\nscore = np.mean(space_error + 15*floor_error)\n</code></pre>\n<p><code>np.einsum()</code> is another option that is very fast and clean, but I find it less intuitive. This post compares various methods: <a href=\"https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy\" target=\"_blank\">https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy</a></p>",
      "votes": 2,
      "replies": [
        {
          "id": 1176885,
          "author_name": "Tolga",
          "author_url": "",
          "post_date": "2021-01-29T21:32:18.857000",
          "content": "<p>I was wondering comparison of different methods. Thank you for the link!</p>\n<p>Indeed, <code>np.einsum</code> looks pretty fast. I think all methods are fine, except <code>scipy.spatial.distance</code>.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1244213,
      "author_name": "",
      "author_url": "",
      "post_date": "2021-03-18T19:15:16.497000",
      "content": "",
      "votes": 1,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1176803": "The competition metric, mean position error, can be calculated with numpy as follows:\n\n```python\nimport numpy as np\n\n# Hypothetical data for demonstration\ny_true = [[1, 100, 105], [2, 120, 112], [3, 180, 170]]\ny_pred = [[1, 110, 104], [1, 110, 100], [3, 178, 168]]\n\ndef mpe(y_true, y_pred):\n    y_true = np.asarray(y_true)\n    y_pred = np.asarray(y_pred)\n    diff = y_true - y_pred\n    return np.mean(np.sqrt(np.sum(diff[:, 1:3]**2., axis=1)) + np.abs(diff[:, 0]) * 15)\n\nmpe(y_true, y_pred)\n```\n\ny_true and y_pred are in the form of [[floor, x, y], [floor, x, y], ...] as in the sample_submission.csv file.",
    "1176867": "Cool! Here's a version to use if you already have dataframes for your submission and the ground truth. If you don't use the first column as index, just slice/drop it from the dataframes.\n\n```\ndiff = sub_df.to_numpy() - truth_df.to_numpy()\nspace_error = np.linalg.norm(diff[:,-2:], axis=1)\nfloor_error = np.abs(diff[:,-3])\nscore = np.mean(space_error + 15*floor_error)\n```\n\n`np.einsum()` is another option that is very fast and clean, but I find it less intuitive. This post compares various methods: https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy",
    "1244213": ""
  }
}