{
  "id": 459885,
  "title": "39th Place Solution",
  "url": "/competitions/child-mind-institute-detect-sleep-states/writeups/avengers-39th-place-solution",
  "author_name": "",
  "post_date": "2023-12-07T04:38:56.860Z",
  "votes": 18,
  "comment_count": 3,
  "views": 0,
  "content": "<h1>Here is 39th Detail Solution  Child Mind Institute - Detect Sleep States</h1>\n<p>Thanks to Kaggle for hosting this meaningful competition. Thanks to all the Kagglers for your discussions and shared perspectives. This was our first time participating in a formal tabular competition, and we've learned a lot from the experience.</p>\n<p><strong>Team Avengers will always continue the journey on Kaggle.</strong></p>\n<p>Main GitHub Repo: <a href=\"https://github.com/lullabies777/kaggle-detect-sleep\" target=\"_blank\">Here</a></p>\n<p>PrecTime GitHub Repo: <a href=\"https://github.com/Lizhecheng02/Kaggle-Detect_Sleep_States\" target=\"_blank\">Here</a></p>\n<h2>Here is the Detail Solution</h2>\n<h3>Baseline Code</h3>\n<p>Here, we need to thank <a href=\"https://www.kaggle.com/tubotubo\" target=\"_blank\">tubotubo</a> for providing the baseline code.  We didn't join the competition from the very beginning, this baseline code provided us with some ideas and basic model structures.</p>\n<h3>Dataset Preparation</h3>\n<ul>\n<li><p>We didn't use any methods to handle the dirty data, which might be one reason why we couldn't improve our scores anymore.</p></li>\n<li><p>On the evening before the competition ended, my teammate found this <a href=\"https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/456177\" target=\"_blank\">discussion</a>. Consequently, we attempted to clean the data by removing the data for the days where the event was empty. However, due to the time limitation, we didn't make significant progress.  </p></li>\n<li><p>We believe data cleaning should be helpful because the model using this method showed a smaller difference in scores on the private leaderboard.</p></li>\n</ul>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F10671485%2Fec42c865fae37d8815cc81fff8521184%2F_20231205195307.png?generation=1701903784520525&amp;alt=media\" alt=\"Comparison\"></p>\n<h3>Feature Engineering</h3>\n<p>We generated new features using shift, different and rolling window functions. </p>\n<p>The final set we utilized 24 rolling features in addition to the original 4, making a total of 28 features. The use of new features did not significantly improve the model's score, which was somewhat unexpected for us.</p>\n<p>Code: <a href=\"https://github.com/lullabies777/kaggle-detect-sleep/blob/main/run/prepare_data.py\" target=\"_blank\">Here</a></p>\n<pre><code>*[pl.().(i).(f)  i  (diff_start, diff_end, diff_step)],\n*[pl.().shift(i).(f)\n   i  (shift_start, shift_end, shift_step)  i != ],\n*[pl.().rolling_mean(window_size).(\n    f)  window_size  window_steps],\n*[pl.().rolling_min(window_size).(\n    f)  window_size  window_steps],\n*[pl.().rolling_max(window_size).(\n    f)  window_size  window_steps],\n*[pl.().rolling_std(window_size).(\n    f)  window_size  window_steps]\n</code></pre>\n<h3>Wandb sweep</h3>\n<p>Wandb sweep is a hyperparameter optimization tool provided by the Wandb machine learning platform. It allows automatic exploration of different hyperparameter combinations to enhance a model's performance.</p>\n<p>Implementation Code: <a href=\"https://github.com/lullabies777/kaggle-detect-sleep/tree/main/run/sweep\" target=\"_blank\">Here</a></p>\n<h3>Models</h3>\n<ul>\n<li>Used overlap - To enhance accuracy in predicting sequence edges, we utilized overlap by using a 10000 length sequence to predict an 8000 length sequence.</li>\n<li>Implementation of PrecTime Model -  You can find details in this <a href=\"https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/459616\" target=\"_blank\">discussion</a>. We also made modifications to its structure, including the addition of transformer architecture and residual connection structures. The experiments had shown that these modifications contribute to a certain improvement in the model's performance.</li>\n</ul>\n<h3>Post-preprocessing Trick</h3>\n<p>We used dynamic programming algorithm to deal with overlap problem.  </p>\n<p>Principle behind this method: To achieve a high AP (Average Precision), three main criteria need to be met: Firstly, the predicted label should be sufficiently close to the actual label. Secondly, within a positive or negative tolerance range around the actual label, there should only be one predicted point. Thirdly, the score of other predicted points outside the actual label range should be lower than those within the range.</p>\n<pre><code>def get_results_slide_window(pred, gap):\n    scores = (pred)\n    stack = []\n     = [-] * (scores)\n    [] = \n     i in (,(scores)):\n         i - stack[-] &lt; gap:\n             scores[i] &gt;= scores[stack[-]]:\n                stack.()\n                 i - gap &gt;= :\n                     stack:\n                         [i - gap] != stack[-]:\n                             stack  [i - gap] - stack[-] &lt; gap:\n                                stack.()\n                            stack.([i - gap])\n                    :\n                        stack.([i - gap])\n                stack.(i)\n        :\n            stack.(i)\n        [i] = stack[-]\n     stack\n</code></pre>\n<h3>Ensemble</h3>\n<p>Our final ensemble method essentially involved averaging different outputs. With post-processing and this ensemble method combined, our results generally follow the pattern that the more models we use or the greater the variety of models, the higher the score.</p>\n<p>Submission Code1: <a href=\"https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-761-pb-0-804\" target=\"_blank\">Here</a><br>\nSubmission Code2: <a href=\"https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-75-pb-0-8\" target=\"_blank\">Here</a></p>\n<p>Our submissions:</p>\n<table>\n<thead>\n<tr>\n<th>Models</th>\n<th>LB Score</th>\n<th>PB Score</th>\n<th>Selected</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>2 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet</code></td>\n<td><code>0.75</code></td>\n<td><code>0.8</code></td>\n<td><strong>Yes</strong></td>\n</tr>\n<tr>\n<td><code>2 * 5 folds PrecTime + 2 * 5 folds LSTM-Unet + 10 single models</code></td>\n<td><code>0.759</code></td>\n<td><code>0.803</code></td>\n<td><strong>Yes</strong></td>\n</tr>\n<tr>\n<td><code>1 * 5 folds PrecTime + 1 fold LSTM-Unet + 10 single models</code></td>\n<td><code>0.761</code></td>\n<td><code>0.804</code></td>\n<td><strong>No</strong></td>\n</tr>\n<tr>\n<td><code>1 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet + 10 single models</code></td>\n<td><code>0.759</code></td>\n<td><code>0.803</code></td>\n<td><strong>No</strong></td>\n</tr>\n</tbody>\n</table>\n<h3>Other</h3>\n<p>Welcome everyone to check our GitHub code, looking forward to any discussions.</p>\n<h3>Conclusion</h3>\n<ul>\n<li>Data Cleaning.</li>\n<li>Generate New Features. </li>\n<li>Use Architecture Like Conv1d, RNN, GRU, LSTM or Transformer.</li>\n<li>Write Post-preprocessing for Special Metrics.</li>\n</ul>\n<h2>Thanks to all of my teammates for working together to gain this Silver Medal.</h2>",
  "messages": [
    {
      "id": "2551723",
      "postDate": "12/06/2023 23:05:56",
      "content": "<h1>Here is 39th Detail Solution  Child Mind Institute - Detect Sleep States</h1>\n<p>Thanks to Kaggle for hosting this meaningful competition. Thanks to all the Kagglers for your discussions and shared perspectives. This was our first time participating in a formal tabular competition, and we've learned a lot from the experience.</p>\n<p><strong>Team Avengers will always continue the journey on Kaggle.</strong></p>\n<p>Main GitHub Repo: <a href=\"https://github.com/lullabies777/kaggle-detect-sleep\" target=\"_blank\">Here</a></p>\n<p>PrecTime GitHub Repo: <a href=\"https://github.com/Lizhecheng02/Kaggle-Detect_Sleep_States\" target=\"_blank\">Here</a></p>\n<h2>Here is the Detail Solution</h2>\n<h3>Baseline Code</h3>\n<p>Here, we need to thank <a href=\"https://www.kaggle.com/tubotubo\" target=\"_blank\">tubotubo</a> for providing the baseline code.  We didn't join the competition from the very beginning, this baseline code provided us with some ideas and basic model structures.</p>\n<h3>Dataset Preparation</h3>\n<ul>\n<li><p>We didn't use any methods to handle the dirty data, which might be one reason why we couldn't improve our scores anymore.</p></li>\n<li><p>On the evening before the competition ended, my teammate found this <a href=\"https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/456177\" target=\"_blank\">discussion</a>. Consequently, we attempted to clean the data by removing the data for the days where the event was empty. However, due to the time limitation, we didn't make significant progress.  </p></li>\n<li><p>We believe data cleaning should be helpful because the model using this method showed a smaller difference in scores on the private leaderboard.</p></li>\n</ul>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F10671485%2Fec42c865fae37d8815cc81fff8521184%2F_20231205195307.png?generation=1701903784520525&amp;alt=media\" alt=\"Comparison\"></p>\n<h3>Feature Engineering</h3>\n<p>We generated new features using shift, different and rolling window functions. </p>\n<p>The final set we utilized 24 rolling features in addition to the original 4, making a total of 28 features. The use of new features did not significantly improve the model's score, which was somewhat unexpected for us.</p>\n<p>Code: <a href=\"https://github.com/lullabies777/kaggle-detect-sleep/blob/main/run/prepare_data.py\" target=\"_blank\">Here</a></p>\n<pre><code>*[pl.().(i).(f)  i  (diff_start, diff_end, diff_step)],\n*[pl.().shift(i).(f)\n   i  (shift_start, shift_end, shift_step)  i != ],\n*[pl.().rolling_mean(window_size).(\n    f)  window_size  window_steps],\n*[pl.().rolling_min(window_size).(\n    f)  window_size  window_steps],\n*[pl.().rolling_max(window_size).(\n    f)  window_size  window_steps],\n*[pl.().rolling_std(window_size).(\n    f)  window_size  window_steps]\n</code></pre>\n<h3>Wandb sweep</h3>\n<p>Wandb sweep is a hyperparameter optimization tool provided by the Wandb machine learning platform. It allows automatic exploration of different hyperparameter combinations to enhance a model's performance.</p>\n<p>Implementation Code: <a href=\"https://github.com/lullabies777/kaggle-detect-sleep/tree/main/run/sweep\" target=\"_blank\">Here</a></p>\n<h3>Models</h3>\n<ul>\n<li>Used overlap - To enhance accuracy in predicting sequence edges, we utilized overlap by using a 10000 length sequence to predict an 8000 length sequence.</li>\n<li>Implementation of PrecTime Model -  You can find details in this <a href=\"https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/459616\" target=\"_blank\">discussion</a>. We also made modifications to its structure, including the addition of transformer architecture and residual connection structures. The experiments had shown that these modifications contribute to a certain improvement in the model's performance.</li>\n</ul>\n<h3>Post-preprocessing Trick</h3>\n<p>We used dynamic programming algorithm to deal with overlap problem.  </p>\n<p>Principle behind this method: To achieve a high AP (Average Precision), three main criteria need to be met: Firstly, the predicted label should be sufficiently close to the actual label. Secondly, within a positive or negative tolerance range around the actual label, there should only be one predicted point. Thirdly, the score of other predicted points outside the actual label range should be lower than those within the range.</p>\n<pre><code>def get_results_slide_window(pred, gap):\n    scores = (pred)\n    stack = []\n     = [-] * (scores)\n    [] = \n     i in (,(scores)):\n         i - stack[-] &lt; gap:\n             scores[i] &gt;= scores[stack[-]]:\n                stack.()\n                 i - gap &gt;= :\n                     stack:\n                         [i - gap] != stack[-]:\n                             stack  [i - gap] - stack[-] &lt; gap:\n                                stack.()\n                            stack.([i - gap])\n                    :\n                        stack.([i - gap])\n                stack.(i)\n        :\n            stack.(i)\n        [i] = stack[-]\n     stack\n</code></pre>\n<h3>Ensemble</h3>\n<p>Our final ensemble method essentially involved averaging different outputs. With post-processing and this ensemble method combined, our results generally follow the pattern that the more models we use or the greater the variety of models, the higher the score.</p>\n<p>Submission Code1: <a href=\"https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-761-pb-0-804\" target=\"_blank\">Here</a><br>\nSubmission Code2: <a href=\"https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-75-pb-0-8\" target=\"_blank\">Here</a></p>\n<p>Our submissions:</p>\n<table>\n<thead>\n<tr>\n<th>Models</th>\n<th>LB Score</th>\n<th>PB Score</th>\n<th>Selected</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>2 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet</code></td>\n<td><code>0.75</code></td>\n<td><code>0.8</code></td>\n<td><strong>Yes</strong></td>\n</tr>\n<tr>\n<td><code>2 * 5 folds PrecTime + 2 * 5 folds LSTM-Unet + 10 single models</code></td>\n<td><code>0.759</code></td>\n<td><code>0.803</code></td>\n<td><strong>Yes</strong></td>\n</tr>\n<tr>\n<td><code>1 * 5 folds PrecTime + 1 fold LSTM-Unet + 10 single models</code></td>\n<td><code>0.761</code></td>\n<td><code>0.804</code></td>\n<td><strong>No</strong></td>\n</tr>\n<tr>\n<td><code>1 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet + 10 single models</code></td>\n<td><code>0.759</code></td>\n<td><code>0.803</code></td>\n<td><strong>No</strong></td>\n</tr>\n</tbody>\n</table>\n<h3>Other</h3>\n<p>Welcome everyone to check our GitHub code, looking forward to any discussions.</p>\n<h3>Conclusion</h3>\n<ul>\n<li>Data Cleaning.</li>\n<li>Generate New Features. </li>\n<li>Use Architecture Like Conv1d, RNN, GRU, LSTM or Transformer.</li>\n<li>Write Post-preprocessing for Special Metrics.</li>\n</ul>\n<h2>Thanks to all of my teammates for working together to gain this Silver Medal.</h2>",
      "rawMarkdown": "# Here is 39th Detail Solution  Child Mind Institute - Detect Sleep States\n\nThanks to Kaggle for hosting this meaningful competition. Thanks to all the Kagglers for your discussions and shared perspectives. This was our first time participating in a formal tabular competition, and we've learned a lot from the experience.\n\n**Team Avengers will always continue the journey on Kaggle.**\n\nMain GitHub Repo: [Here](https://github.com/lullabies777/kaggle-detect-sleep)\n\nPrecTime GitHub Repo: [Here](https://github.com/Lizhecheng02/Kaggle-Detect_Sleep_States)\n\n## Here is the Detail Solution\n\n### Baseline Code\n\nHere, we need to thank [tubotubo](https://www.kaggle.com/tubotubo) for providing the baseline code.  We didn't join the competition from the very beginning, this baseline code provided us with some ideas and basic model structures.\n\n### Dataset Preparation\n\n- We didn't use any methods to handle the dirty data, which might be one reason why we couldn't improve our scores anymore.\n\n- On the evening before the competition ended, my teammate found this [discussion](https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/456177). Consequently, we attempted to clean the data by removing the data for the days where the event was empty. However, due to the time limitation, we didn't make significant progress.  \n\n- We believe data cleaning should be helpful because the model using this method showed a smaller difference in scores on the private leaderboard.\n\n![Comparison](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F10671485%2Fec42c865fae37d8815cc81fff8521184%2F_20231205195307.png?generation=1701903784520525&alt=media)\n\n### Feature Engineering\n\nWe generated new features using shift, different and rolling window functions. \n\nThe final set we utilized 24 rolling features in addition to the original 4, making a total of 28 features. The use of new features did not significantly improve the model's score, which was somewhat unexpected for us.\n\nCode: [Here](https://github.com/lullabies777/kaggle-detect-sleep/blob/main/run/prepare_data.py)\n\n```\n*[pl.col(\"anglez\").diff(i).alias(f\"anglez_diff_{i}\") for i in range(diff_start, diff_end, diff_step)],\n*[pl.col(\"anglez\").shift(i).alias(f\"anglez_lag_{i}\")\n  for i in range(shift_start, shift_end, shift_step) if i != 0],\n*[pl.col(\"anglez\").rolling_mean(window_size).alias(\n    f\"anglez_mean_{window_size}\") for window_size in window_steps],\n*[pl.col(\"anglez\").rolling_min(window_size).alias(\n    f\"anglez_min_{window_size}\") for window_size in window_steps],\n*[pl.col(\"anglez\").rolling_max(window_size).alias(\n    f\"anglez_max_{window_size}\") for window_size in window_steps],\n*[pl.col(\"anglez\").rolling_std(window_size).alias(\n    f\"anglez_std_{window_size}\") for window_size in window_steps]\n```\n\n### Wandb sweep\n\nWandb sweep is a hyperparameter optimization tool provided by the Wandb machine learning platform. It allows automatic exploration of different hyperparameter combinations to enhance a model's performance.\n\nImplementation Code: [Here](https://github.com/lullabies777/kaggle-detect-sleep/tree/main/run/sweep)\n\n### Models\n\n- Used overlap - To enhance accuracy in predicting sequence edges, we utilized overlap by using a 10000 length sequence to predict an 8000 length sequence.\n- Implementation of PrecTime Model -  You can find details in this [discussion](https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/459616). We also made modifications to its structure, including the addition of transformer architecture and residual connection structures. The experiments had shown that these modifications contribute to a certain improvement in the model's performance.\n\n### Post-preprocessing Trick\n\nWe used dynamic programming algorithm to deal with overlap problem.  \n\nPrinciple behind this method: To achieve a high AP (Average Precision), three main criteria need to be met: Firstly, the predicted label should be sufficiently close to the actual label. Secondly, within a positive or negative tolerance range around the actual label, there should only be one predicted point. Thirdly, the score of other predicted points outside the actual label range should be lower than those within the range.\n\n```\ndef get_results_slide_window(pred, gap):\n    scores = list(pred)\n    stack = [0]\n    dp = [-1] * len(scores)\n    dp[0] = 0\n    for i in range(1,len(scores)):\n        if i - stack[-1] < gap:\n            if scores[i] >= scores[stack[-1]]:\n                stack.pop()\n                if i - gap >= 0:\n                    if stack:\n                        if dp[i - gap] != stack[-1]:\n                            while stack and dp[i - gap] - stack[-1] < gap:\n                                stack.pop()\n                            stack.append(dp[i - gap])\n                    else:\n                        stack.append(dp[i - gap])\n                stack.append(i)\n        else:\n            stack.append(i)\n        dp[i] = stack[-1]\n    return stack\n```\n\n### Ensemble\n\nOur final ensemble method essentially involved averaging different outputs. With post-processing and this ensemble method combined, our results generally follow the pattern that the more models we use or the greater the variety of models, the higher the score.\n\nSubmission Code1: [Here](https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-761-pb-0-804)\nSubmission Code2: [Here](https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-75-pb-0-8)\n\nOur submissions:\n\n| Models                                                       | LB Score  | PB Score  | Selected |\n| ------------------------------------------------------------ | --------- | --------- | -------- |\n| ``2 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet``             | ``0.75``  | ``0.8``   | **Yes**  |\n| ``2 * 5 folds PrecTime + 2 * 5 folds LSTM-Unet + 10 single models`` | ``0.759`` | ``0.803`` | **Yes**  |\n| ``1 * 5 folds PrecTime + 1 fold LSTM-Unet + 10 single models`` | ``0.761`` | ``0.804`` | **No**   |\n| ``1 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet + 10 single models`` | ``0.759`` | ``0.803`` | **No**   |\n\n\n### Other\n\nWelcome everyone to check our GitHub code, looking forward to any discussions.\n\n### Conclusion\n\n- Data Cleaning.\n- Generate New Features. \n- Use Architecture Like Conv1d, RNN, GRU, LSTM or Transformer.\n- Write Post-preprocessing for Special Metrics.\n\n## Thanks to all of my teammates for working together to gain this Silver Medal.",
      "votes": null
    },
    {
      "id": "2551906",
      "postDate": "12/07/2023 03:56:12",
      "content": "<p>excellent work</p>",
      "rawMarkdown": "excellent work",
      "votes": null
    },
    {
      "id": "2551916",
      "postDate": "12/07/2023 04:10:37",
      "content": "<p>excellent teammates</p>",
      "rawMarkdown": "excellent teammates",
      "votes": null
    },
    {
      "id": "2552974",
      "postDate": "12/07/2023 23:41:42",
      "content": "<p>Data Structures and Algorithms implementation in play!</p>",
      "rawMarkdown": "Data Structures and Algorithms implementation in play!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2551906,
      "author_name": "rdxsun",
      "author_url": "",
      "post_date": "12/07/2023 03:56:12",
      "content": "<p>excellent work</p>",
      "votes": null,
      "replies": [
        {
          "id": 2551916,
          "author_name": "lizhecheng",
          "author_url": "",
          "post_date": "12/07/2023 04:10:37",
          "content": "<p>excellent teammates</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 2552974,
      "author_name": "yeoyunsianggeremie",
      "author_url": "",
      "post_date": "12/07/2023 23:41:42",
      "content": "<p>Data Structures and Algorithms implementation in play!</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2551723": "# Here is 39th Detail Solution  Child Mind Institute - Detect Sleep States\n\nThanks to Kaggle for hosting this meaningful competition. Thanks to all the Kagglers for your discussions and shared perspectives. This was our first time participating in a formal tabular competition, and we've learned a lot from the experience.\n\n**Team Avengers will always continue the journey on Kaggle.**\n\nMain GitHub Repo: [Here](https://github.com/lullabies777/kaggle-detect-sleep)\n\nPrecTime GitHub Repo: [Here](https://github.com/Lizhecheng02/Kaggle-Detect_Sleep_States)\n\n## Here is the Detail Solution\n\n### Baseline Code\n\nHere, we need to thank [tubotubo](https://www.kaggle.com/tubotubo) for providing the baseline code.  We didn't join the competition from the very beginning, this baseline code provided us with some ideas and basic model structures.\n\n### Dataset Preparation\n\n- We didn't use any methods to handle the dirty data, which might be one reason why we couldn't improve our scores anymore.\n\n- On the evening before the competition ended, my teammate found this [discussion](https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/456177). Consequently, we attempted to clean the data by removing the data for the days where the event was empty. However, due to the time limitation, we didn't make significant progress.  \n\n- We believe data cleaning should be helpful because the model using this method showed a smaller difference in scores on the private leaderboard.\n\n![Comparison](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F10671485%2Fec42c865fae37d8815cc81fff8521184%2F_20231205195307.png?generation=1701903784520525&alt=media)\n\n### Feature Engineering\n\nWe generated new features using shift, different and rolling window functions. \n\nThe final set we utilized 24 rolling features in addition to the original 4, making a total of 28 features. The use of new features did not significantly improve the model's score, which was somewhat unexpected for us.\n\nCode: [Here](https://github.com/lullabies777/kaggle-detect-sleep/blob/main/run/prepare_data.py)\n\n```\n*[pl.col(\"anglez\").diff(i).alias(f\"anglez_diff_{i}\") for i in range(diff_start, diff_end, diff_step)],\n*[pl.col(\"anglez\").shift(i).alias(f\"anglez_lag_{i}\")\n  for i in range(shift_start, shift_end, shift_step) if i != 0],\n*[pl.col(\"anglez\").rolling_mean(window_size).alias(\n    f\"anglez_mean_{window_size}\") for window_size in window_steps],\n*[pl.col(\"anglez\").rolling_min(window_size).alias(\n    f\"anglez_min_{window_size}\") for window_size in window_steps],\n*[pl.col(\"anglez\").rolling_max(window_size).alias(\n    f\"anglez_max_{window_size}\") for window_size in window_steps],\n*[pl.col(\"anglez\").rolling_std(window_size).alias(\n    f\"anglez_std_{window_size}\") for window_size in window_steps]\n```\n\n### Wandb sweep\n\nWandb sweep is a hyperparameter optimization tool provided by the Wandb machine learning platform. It allows automatic exploration of different hyperparameter combinations to enhance a model's performance.\n\nImplementation Code: [Here](https://github.com/lullabies777/kaggle-detect-sleep/tree/main/run/sweep)\n\n### Models\n\n- Used overlap - To enhance accuracy in predicting sequence edges, we utilized overlap by using a 10000 length sequence to predict an 8000 length sequence.\n- Implementation of PrecTime Model -  You can find details in this [discussion](https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/459616). We also made modifications to its structure, including the addition of transformer architecture and residual connection structures. The experiments had shown that these modifications contribute to a certain improvement in the model's performance.\n\n### Post-preprocessing Trick\n\nWe used dynamic programming algorithm to deal with overlap problem.  \n\nPrinciple behind this method: To achieve a high AP (Average Precision), three main criteria need to be met: Firstly, the predicted label should be sufficiently close to the actual label. Secondly, within a positive or negative tolerance range around the actual label, there should only be one predicted point. Thirdly, the score of other predicted points outside the actual label range should be lower than those within the range.\n\n```\ndef get_results_slide_window(pred, gap):\n    scores = list(pred)\n    stack = [0]\n    dp = [-1] * len(scores)\n    dp[0] = 0\n    for i in range(1,len(scores)):\n        if i - stack[-1] < gap:\n            if scores[i] >= scores[stack[-1]]:\n                stack.pop()\n                if i - gap >= 0:\n                    if stack:\n                        if dp[i - gap] != stack[-1]:\n                            while stack and dp[i - gap] - stack[-1] < gap:\n                                stack.pop()\n                            stack.append(dp[i - gap])\n                    else:\n                        stack.append(dp[i - gap])\n                stack.append(i)\n        else:\n            stack.append(i)\n        dp[i] = stack[-1]\n    return stack\n```\n\n### Ensemble\n\nOur final ensemble method essentially involved averaging different outputs. With post-processing and this ensemble method combined, our results generally follow the pattern that the more models we use or the greater the variety of models, the higher the score.\n\nSubmission Code1: [Here](https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-761-pb-0-804)\nSubmission Code2: [Here](https://www.kaggle.com/code/lizhecheng/detect-sleep-states-ensemble-lb-0-75-pb-0-8)\n\nOur submissions:\n\n| Models                                                       | LB Score  | PB Score  | Selected |\n| ------------------------------------------------------------ | --------- | --------- | -------- |\n| ``2 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet``             | ``0.75``  | ``0.8``   | **Yes**  |\n| ``2 * 5 folds PrecTime + 2 * 5 folds LSTM-Unet + 10 single models`` | ``0.759`` | ``0.803`` | **Yes**  |\n| ``1 * 5 folds PrecTime + 1 fold LSTM-Unet + 10 single models`` | ``0.761`` | ``0.804`` | **No**   |\n| ``1 * 5 folds PrecTime + 1 * 5 folds LSTM-Unet + 10 single models`` | ``0.759`` | ``0.803`` | **No**   |\n\n\n### Other\n\nWelcome everyone to check our GitHub code, looking forward to any discussions.\n\n### Conclusion\n\n- Data Cleaning.\n- Generate New Features. \n- Use Architecture Like Conv1d, RNN, GRU, LSTM or Transformer.\n- Write Post-preprocessing for Special Metrics.\n\n## Thanks to all of my teammates for working together to gain this Silver Medal.",
    "2551906": "excellent work",
    "2551916": "excellent teammates",
    "2552974": "Data Structures and Algorithms implementation in play!"
  },
  "source": "existing_discussion",
  "complete": true,
  "competition_id": 53666,
  "competition_slug": "child-mind-institute-detect-sleep-states",
  "team_id": 11061227,
  "solution_url": "/competitions/child-mind-institute-detect-sleep-states/writeups/avengers-39th-place-solution"
}