{
  "id": 362852,
  "title": "📌 Advanced Feature Engineering 4 - Goal Direction",
  "url": "/competitions/tabular-playground-series-oct-2022/discussion/362852",
  "author_name": "",
  "post_date": "2022-10-29T12:34:01.554697Z",
  "votes": 12,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Hi all,</p>\n<p>I created four new features which are goal angle, ball touch positions, car number inside the triangle, and goal direction. Goal direction represents whether the ball goes goal or not.</p>\n<p>Feature 1, goal angle <a href=\"https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362691\" target=\"_blank\">link</a>.<br>\nFeature 2, ball touch positions <a href=\"https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362692\" target=\"_blank\">link</a>.<br>\nFeature 3, the car number in the triangle <a href=\"https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362839\" target=\"_blank\">link</a>.<br>\nFeature 4 is goal direction.</p>\n<pre><code>def add_goal_dir(df):\n    vec_a = [0, 100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_b = [0, -100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_ball = df[['ball_vel_x', 'ball_vel_y']].to_numpy() - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    df['ball_goal_dir_A'] = vec_a[:,0] * vec_ball[:,0] + vec_a[:,1] * vec_ball[:,1]\n    df['ball_goal_dir_B'] = vec_b[:,0] * vec_ball[:,0] + vec_b[:,1] * vec_ball[:,1]\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3381067%2F1e7f9d2e41f64655cec72155ea187eb7%2FPwXfZ.jpg?generation=1667046751539722&amp;alt=media\" alt=\"\"><br>\nimage <a href=\"https://math.stackexchange.com/questions/2505184/two-vectors-are-in-the-same-direction-if\" target=\"_blank\">link</a> </p>",
  "messages": [
    {
      "id": "2008897",
      "postDate": "10/29/2022 12:34:01",
      "content": "<p>Hi all,</p>\n<p>I created four new features which are goal angle, ball touch positions, car number inside the triangle, and goal direction. Goal direction represents whether the ball goes goal or not.</p>\n<p>Feature 1, goal angle <a href=\"https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362691\" target=\"_blank\">link</a>.<br>\nFeature 2, ball touch positions <a href=\"https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362692\" target=\"_blank\">link</a>.<br>\nFeature 3, the car number in the triangle <a href=\"https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362839\" target=\"_blank\">link</a>.<br>\nFeature 4 is goal direction.</p>\n<pre><code>def add_goal_dir(df):\n    vec_a = [0, 100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_b = [0, -100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_ball = df[['ball_vel_x', 'ball_vel_y']].to_numpy() - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    df['ball_goal_dir_A'] = vec_a[:,0] * vec_ball[:,0] + vec_a[:,1] * vec_ball[:,1]\n    df['ball_goal_dir_B'] = vec_b[:,0] * vec_ball[:,0] + vec_b[:,1] * vec_ball[:,1]\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3381067%2F1e7f9d2e41f64655cec72155ea187eb7%2FPwXfZ.jpg?generation=1667046751539722&amp;alt=media\" alt=\"\"><br>\nimage <a href=\"https://math.stackexchange.com/questions/2505184/two-vectors-are-in-the-same-direction-if\" target=\"_blank\">link</a> </p>",
      "rawMarkdown": "Hi all,\n\nI created four new features which are goal angle, ball touch positions, car number inside the triangle, and goal direction. Goal direction represents whether the ball goes goal or not.\n\nFeature 1, goal angle [link](https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362691).\nFeature 2, ball touch positions [link](https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362692).\nFeature 3, the car number in the triangle [link](https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362839).\nFeature 4 is goal direction.\n\n```\ndef add_goal_dir(df):\n    vec_a = [0, 100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_b = [0, -100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_ball = df[['ball_vel_x', 'ball_vel_y']].to_numpy() - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    df['ball_goal_dir_A'] = vec_a[:,0] * vec_ball[:,0] + vec_a[:,1] * vec_ball[:,1]\n    df['ball_goal_dir_B'] = vec_b[:,0] * vec_ball[:,0] + vec_b[:,1] * vec_ball[:,1]\n```\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3381067%2F1e7f9d2e41f64655cec72155ea187eb7%2FPwXfZ.jpg?generation=1667046751539722&alt=media)\nimage [link](https://math.stackexchange.com/questions/2505184/two-vectors-are-in-the-same-direction-if)",
      "votes": null
    },
    {
      "id": "2008990",
      "postDate": "10/29/2022 14:19:23",
      "content": "<p>I think you need to consider the position of the ball aswell and normalize the vectors.</p>\n<p>`</p>\n<pre><code>def euclidean_norm(X):\n     return np.linalg.norm(X, axis=1)\n\n###make sure not to divide by zero\ndef normalize(X):\n     return X/((euclidean_norm(X)+1.e-5)[:, None])\n\n def cosine_law(X,Y):\n     return np.sum(normalize(X)*normalize(Y),axis=1)\n\n df[f'ball_move_goal_{goal}']=cosine_law(GOAL-df[BALL_POS].values, df[BALL_VEL].values)\n</code></pre>\n<p>`</p>",
      "rawMarkdown": "I think you need to consider the position of the ball aswell and normalize the vectors.\n\n`\n\n    def euclidean_norm(X):\n         return np.linalg.norm(X, axis=1)\n\n    ###make sure not to divide by zero\n    def normalize(X):\n         return X/((euclidean_norm(X)+1.e-5)[:, None])\n\n     def cosine_law(X,Y):\n         return np.sum(normalize(X)*normalize(Y),axis=1)\n\n     df[f'ball_move_goal_{goal}']=cosine_law(GOAL-df[BALL_POS].values, df[BALL_VEL].values)\n\n`",
      "votes": null
    },
    {
      "id": "2009778",
      "postDate": "10/30/2022 10:01:39",
      "content": "<p>That is true, thank you. I fixed the beginning of the two vectors by subtracting the ball position</p>",
      "rawMarkdown": "That is true, thank you. I fixed the beginning of the two vectors by subtracting the ball position",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2008990,
      "author_name": "robinbarbarino",
      "author_url": "",
      "post_date": "10/29/2022 14:19:23",
      "content": "<p>I think you need to consider the position of the ball aswell and normalize the vectors.</p>\n<p>`</p>\n<pre><code>def euclidean_norm(X):\n     return np.linalg.norm(X, axis=1)\n\n###make sure not to divide by zero\ndef normalize(X):\n     return X/((euclidean_norm(X)+1.e-5)[:, None])\n\n def cosine_law(X,Y):\n     return np.sum(normalize(X)*normalize(Y),axis=1)\n\n df[f'ball_move_goal_{goal}']=cosine_law(GOAL-df[BALL_POS].values, df[BALL_VEL].values)\n</code></pre>\n<p>`</p>",
      "votes": null,
      "replies": [
        {
          "id": 2009778,
          "author_name": "hasanbasriakcay",
          "author_url": "",
          "post_date": "10/30/2022 10:01:39",
          "content": "<p>That is true, thank you. I fixed the beginning of the two vectors by subtracting the ball position</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2008897": "Hi all,\n\nI created four new features which are goal angle, ball touch positions, car number inside the triangle, and goal direction. Goal direction represents whether the ball goes goal or not.\n\nFeature 1, goal angle [link](https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362691).\nFeature 2, ball touch positions [link](https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362692).\nFeature 3, the car number in the triangle [link](https://www.kaggle.com/competitions/tabular-playground-series-oct-2022/discussion/362839).\nFeature 4 is goal direction.\n\n```\ndef add_goal_dir(df):\n    vec_a = [0, 100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_b = [0, -100] - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    vec_ball = df[['ball_vel_x', 'ball_vel_y']].to_numpy() - df[['ball_pos_x', 'ball_pos_y']].to_numpy()\n    df['ball_goal_dir_A'] = vec_a[:,0] * vec_ball[:,0] + vec_a[:,1] * vec_ball[:,1]\n    df['ball_goal_dir_B'] = vec_b[:,0] * vec_ball[:,0] + vec_b[:,1] * vec_ball[:,1]\n```\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3381067%2F1e7f9d2e41f64655cec72155ea187eb7%2FPwXfZ.jpg?generation=1667046751539722&alt=media)\nimage [link](https://math.stackexchange.com/questions/2505184/two-vectors-are-in-the-same-direction-if)",
    "2008990": "I think you need to consider the position of the ball aswell and normalize the vectors.\n\n`\n\n    def euclidean_norm(X):\n         return np.linalg.norm(X, axis=1)\n\n    ###make sure not to divide by zero\n    def normalize(X):\n         return X/((euclidean_norm(X)+1.e-5)[:, None])\n\n     def cosine_law(X,Y):\n         return np.sum(normalize(X)*normalize(Y),axis=1)\n\n     df[f'ball_move_goal_{goal}']=cosine_law(GOAL-df[BALL_POS].values, df[BALL_VEL].values)\n\n`",
    "2009778": "That is true, thank you. I fixed the beginning of the two vectors by subtracting the ball position"
  },
  "source": "meta"
}