{
  "id": 454962,
  "title": "The elegant way to generate the Gaussian targets with vectorization",
  "url": "/competitions/child-mind-institute-detect-sleep-states/discussion/454962",
  "author_name": "",
  "post_date": "2023-11-12T19:26:29.677121400Z",
  "votes": 13,
  "comment_count": 1,
  "views": 0,
  "content": "<pre><code> ():\n    df = df.copy()\n    pivotted_df = df.pivot(index=[, ], columns=, values=).reset_index()\n\n     pivotted_df\n\n ():\n    gaussian =  x, sigma=: torch.exp(-((x / (sigma * )) ** ))\n\n    event_group = pivotted_events[pivotted_events[] == group.iloc[][]]\n    event_group = event_group.replace(np.nan, np.inf)\n\n    timesteps = torch.Tensor(group.step).view(-, )\n    centroids_onset = torch.Tensor(event_group[].values)\n    centroids_wakeup = torch.Tensor(event_group[].values)\n\n    target = torch.dstack([gaussian(timesteps - centroids_onset), gaussian(timesteps - centroids_wakeup)]).()\n     target\n</code></pre>\n<p>This is definitely not the fastest solution but it's mistake free.</p>",
  "messages": [
    {
      "id": "2522554",
      "postDate": "11/12/2023 19:26:29",
      "content": "<pre><code> ():\n    df = df.copy()\n    pivotted_df = df.pivot(index=[, ], columns=, values=).reset_index()\n\n     pivotted_df\n\n ():\n    gaussian =  x, sigma=: torch.exp(-((x / (sigma * )) ** ))\n\n    event_group = pivotted_events[pivotted_events[] == group.iloc[][]]\n    event_group = event_group.replace(np.nan, np.inf)\n\n    timesteps = torch.Tensor(group.step).view(-, )\n    centroids_onset = torch.Tensor(event_group[].values)\n    centroids_wakeup = torch.Tensor(event_group[].values)\n\n    target = torch.dstack([gaussian(timesteps - centroids_onset), gaussian(timesteps - centroids_wakeup)]).()\n     target\n</code></pre>\n<p>This is definitely not the fastest solution but it's mistake free.</p>",
      "rawMarkdown": "```python\ndef pivot_events(df=train_events):\n    df = df.copy()\n    pivotted_df = df.pivot(index=[\"series_id\", \"night\"], columns=\"event\", values=\"step\").reset_index()\n\n    return pivotted_df\n\ndef add_gaussian_labels(group, pivotted_events):\n    gaussian = lambda x, sigma=120: torch.exp(-((x / (sigma * 2.0)) ** 2.0))\n\n    event_group = pivotted_events[pivotted_events[\"series_id\"] == group.iloc[0][\"series_id\"]]\n    event_group = event_group.replace(np.nan, np.inf)\n\n    timesteps = torch.Tensor(group.step).view(-1, 1)\n    centroids_onset = torch.Tensor(event_group[\"onset\"].values)\n    centroids_wakeup = torch.Tensor(event_group[\"wakeup\"].values)\n\n    target = torch.dstack([gaussian(timesteps - centroids_onset), gaussian(timesteps - centroids_wakeup)]).sum(1)\n    return target\n```\n\nThis is definitely not the fastest solution but it's mistake free.",
      "votes": null
    },
    {
      "id": "2522562",
      "postDate": "11/12/2023 19:34:04",
      "content": "<p>It can be optimized with one more line, though. No need to create 0 arrays for np.inf rows (both onset and wakeup) in the pivotted_events.</p>",
      "rawMarkdown": "It can be optimized with one more line, though. No need to create 0 arrays for np.inf rows (both onset and wakeup) in the pivotted_events.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2522562,
      "author_name": "tolgadincer",
      "author_url": "",
      "post_date": "11/12/2023 19:34:04",
      "content": "<p>It can be optimized with one more line, though. No need to create 0 arrays for np.inf rows (both onset and wakeup) in the pivotted_events.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2522554": "```python\ndef pivot_events(df=train_events):\n    df = df.copy()\n    pivotted_df = df.pivot(index=[\"series_id\", \"night\"], columns=\"event\", values=\"step\").reset_index()\n\n    return pivotted_df\n\ndef add_gaussian_labels(group, pivotted_events):\n    gaussian = lambda x, sigma=120: torch.exp(-((x / (sigma * 2.0)) ** 2.0))\n\n    event_group = pivotted_events[pivotted_events[\"series_id\"] == group.iloc[0][\"series_id\"]]\n    event_group = event_group.replace(np.nan, np.inf)\n\n    timesteps = torch.Tensor(group.step).view(-1, 1)\n    centroids_onset = torch.Tensor(event_group[\"onset\"].values)\n    centroids_wakeup = torch.Tensor(event_group[\"wakeup\"].values)\n\n    target = torch.dstack([gaussian(timesteps - centroids_onset), gaussian(timesteps - centroids_wakeup)]).sum(1)\n    return target\n```\n\nThis is definitely not the fastest solution but it's mistake free.",
    "2522562": "It can be optimized with one more line, though. No need to create 0 arrays for np.inf rows (both onset and wakeup) in the pivotted_events."
  },
  "source": "meta"
}