{
  "id": 452315,
  "title": " Assigning `score` values using ML",
  "url": "/competitions/child-mind-institute-detect-sleep-states/discussion/452315",
  "author_name": "Daniel Dewey",
  "post_date": "2023-11-01T17:40:14.413000",
  "votes": 3,
  "comment_count": 0,
  "views": 0,
  "content": "<p>I took a simple transition-fitting approach to finding the onset and wakeup event locations in the <a href=\"https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times\" target=\"_blank\">CMI 2023 - Sleepy Times</a> notebook; see some images below and some details in the \"Summary of the current algorithm\" at the notebook's beginning. This method got me an LB of 0.434 with the event <code>score</code> values all the same (1.0) because the fitting doesn't provide an appropriate measure of \"confidence\".</p>\n<p>By adding a <code>score</code> value to each event, the LB for the same event predictions increased to 0.503. To set the scores I used a simple random forest model and trained it to predict the scaled error (0 to 1) of an event based on some feature values from the event's fitting; then setting score = 1 - level-of-error. This simple ML is done in the <a href=\"https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times-phat-ml\" target=\"_blank\">CMI 2023 - Sleepy Times - phat ML</a> notebook using data in \"phat_frame.csv\" --&nbsp; a submission-like file with many extra columns, including the true step values and fit feature values. The ML models are dill'ed and can be used by the main notebook to assign scores to the test events when run on test data.</p>\n<p>Assigned scores vs the errors:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fb50436c9118b0c9117e6c9bc6ebc404e%2Fassigned_scores_with_ML.png?generation=1698860103746733&amp;alt=media\" alt=\"\"></p>\n<p>_ _ _ Example of score helping AP</p>\n<p>Including a score that is higher for more precise predictions can improve the AP value. For example, my recall for wakeups in the +/-12 step tolerance range is 0.172 ( =  # of predicted wakeups within +/-12 steps / # of all ground-truth wakeups.) Without a score, the precision is also about 0.170 ( =  # of predicted wakeups within +/-12 steps / # of all predicted wakeups) since there are similar numbers of predicted and ground-truth wakeups. So the recall-averaged AP will be 0.172 * 0.170 ~ <strong>0.0292</strong> .</p>\n<p>However when scores are assigned, a score threshold is used and only predicted events with scores above the threshold are considered in calculating the recall and precision. The precision with the score selection can be larger than overall, and the recall will be smaller. As the score threshold goes from high to low values the recall increases. Each time the threshold is lowered, the precision in the new selection is multiplied by the <em>increase in the recall</em> and this product is added onto the Total AP.</p>\n<p>As an example this table shows how the AP for wakeups with tol=12 steps is calculated using a few score thresholds. In this case the Total AP comes out to <strong>0.0461</strong> which is larger than the without-score value of 0.0292. This happens because the precision of high score events, ~ 0.332, is higher than the precision overall, 0.172.</p>\n<pre><code>     #Gnd   #Above\n   Truth   Thresh   #TP      Prec. * dRecall =  dAP    Total AP\n .:                 . * .  = .   .\n .:                . * .  = .   .\n .:                . * .  = .   .\n .:                . * .  = .   .\n</code></pre>\n<p>_ _ _ Some images from the fitting method:</p>\n<p>A sleep/awake feature is fit with a cosine to find approx middle-of-sleep locations for each night of a series:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fbc4a4bdf14756f952cf00448f5a87d22%2Ffit_to_find_events.png?generation=1698860035760187&amp;alt=media\" alt=\"\"></p>\n<p>Zooming in on an expected onset location, a step-down ramp is fit to get the time of onset. The plots here show that the predicted time for Night 9 (blue vertical line) is very close to the ground-truth time (green vertical line.) On the other hand, for Night 12 the predicted time is too early by about 50 minutes.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2F778d10aa034cc71fd887a572760e1c1f%2Fgood_fit_example_onset9.png?generation=1698860055664561&amp;alt=media\" alt=\"\"></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fe220866b0f06a42d0c78fd1ee5e2ee22%2Fpoor_fit_example_onset12.png?generation=1698860071004311&amp;alt=media\" alt=\"\"></p>",
  "messages": [
    {
      "id": 2508418,
      "postDate": "2023-11-01T17:40:14.413Z",
      "content": "<p>I took a simple transition-fitting approach to finding the onset and wakeup event locations in the <a href=\"https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times\" target=\"_blank\">CMI 2023 - Sleepy Times</a> notebook; see some images below and some details in the \"Summary of the current algorithm\" at the notebook's beginning. This method got me an LB of 0.434 with the event <code>score</code> values all the same (1.0) because the fitting doesn't provide an appropriate measure of \"confidence\".</p>\n<p>By adding a <code>score</code> value to each event, the LB for the same event predictions increased to 0.503. To set the scores I used a simple random forest model and trained it to predict the scaled error (0 to 1) of an event based on some feature values from the event's fitting; then setting score = 1 - level-of-error. This simple ML is done in the <a href=\"https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times-phat-ml\" target=\"_blank\">CMI 2023 - Sleepy Times - phat ML</a> notebook using data in \"phat_frame.csv\" --&nbsp; a submission-like file with many extra columns, including the true step values and fit feature values. The ML models are dill'ed and can be used by the main notebook to assign scores to the test events when run on test data.</p>\n<p>Assigned scores vs the errors:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fb50436c9118b0c9117e6c9bc6ebc404e%2Fassigned_scores_with_ML.png?generation=1698860103746733&amp;alt=media\" alt=\"\"></p>\n<p>_ _ _ Example of score helping AP</p>\n<p>Including a score that is higher for more precise predictions can improve the AP value. For example, my recall for wakeups in the +/-12 step tolerance range is 0.172 ( =  # of predicted wakeups within +/-12 steps / # of all ground-truth wakeups.) Without a score, the precision is also about 0.170 ( =  # of predicted wakeups within +/-12 steps / # of all predicted wakeups) since there are similar numbers of predicted and ground-truth wakeups. So the recall-averaged AP will be 0.172 * 0.170 ~ <strong>0.0292</strong> .</p>\n<p>However when scores are assigned, a score threshold is used and only predicted events with scores above the threshold are considered in calculating the recall and precision. The precision with the score selection can be larger than overall, and the recall will be smaller. As the score threshold goes from high to low values the recall increases. Each time the threshold is lowered, the precision in the new selection is multiplied by the <em>increase in the recall</em> and this product is added onto the Total AP.</p>\n<p>As an example this table shows how the AP for wakeups with tol=12 steps is calculated using a few score thresholds. In this case the Total AP comes out to <strong>0.0461</strong> which is larger than the without-score value of 0.0292. This happens because the precision of high score events, ~ 0.332, is higher than the precision overall, 0.172.</p>\n<pre><code>     #Gnd   #Above\n   Truth   Thresh   #TP      Prec. * dRecall =  dAP    Total AP\n .:                 . * .  = .   .\n .:                . * .  = .   .\n .:                . * .  = .   .\n .:                . * .  = .   .\n</code></pre>\n<p>_ _ _ Some images from the fitting method:</p>\n<p>A sleep/awake feature is fit with a cosine to find approx middle-of-sleep locations for each night of a series:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fbc4a4bdf14756f952cf00448f5a87d22%2Ffit_to_find_events.png?generation=1698860035760187&amp;alt=media\" alt=\"\"></p>\n<p>Zooming in on an expected onset location, a step-down ramp is fit to get the time of onset. The plots here show that the predicted time for Night 9 (blue vertical line) is very close to the ground-truth time (green vertical line.) On the other hand, for Night 12 the predicted time is too early by about 50 minutes.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2F778d10aa034cc71fd887a572760e1c1f%2Fgood_fit_example_onset9.png?generation=1698860055664561&amp;alt=media\" alt=\"\"></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fe220866b0f06a42d0c78fd1ee5e2ee22%2Fpoor_fit_example_onset12.png?generation=1698860071004311&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "I took a simple transition-fitting approach to finding the onset and wakeup event locations in the [CMI 2023 - Sleepy Times](https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times) notebook; see some images below and some details in the \"Summary of the current algorithm\" at the notebook's beginning. This method got me an LB of 0.434 with the event `score` values all the same (1.0) because the fitting doesn't provide an appropriate measure of \"confidence\".\n\nBy adding a `score` value to each event, the LB for the same event predictions increased to 0.503. To set the scores I used a simple random forest model and trained it to predict the scaled error (0 to 1) of an event based on some feature values from the event's fitting; then setting score = 1 - level-of-error. This simple ML is done in the [CMI 2023 - Sleepy Times - phat ML](https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times-phat-ml) notebook using data in \"phat_frame.csv\" --  a submission-like file with many extra columns, including the true step values and fit feature values. The ML models are dill'ed and can be used by the main notebook to assign scores to the test events when run on test data.\n\nAssigned scores vs the errors:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fb50436c9118b0c9117e6c9bc6ebc404e%2Fassigned_scores_with_ML.png?generation=1698860103746733&alt=media)\n\n_ _ _ Example of score helping AP\n\nIncluding a score that is higher for more precise predictions can improve the AP value. For example, my recall for wakeups in the +/-12 step tolerance range is 0.172 ( =  # of predicted wakeups within +/-12 steps / # of all ground-truth wakeups.) Without a score, the precision is also about 0.170 ( =  # of predicted wakeups within +/-12 steps / # of all predicted wakeups) since there are similar numbers of predicted and ground-truth wakeups. So the recall-averaged AP will be 0.172 * 0.170 ~ **0.0292** .\n\nHowever when scores are assigned, a score threshold is used and only predicted events with scores above the threshold are considered in calculating the recall and precision. The precision with the score selection can be larger than overall, and the recall will be smaller. As the score threshold goes from high to low values the recall increases. Each time the threshold is lowered, the precision in the new selection is multiplied by the _increase in the recall_ and this product is added onto the Total AP.\n\nAs an example this table shows how the AP for wakeups with tol=12 steps is calculated using a few score thresholds. In this case the Total AP comes out to **0.0461** which is larger than the without-score value of 0.0292. This happens because the precision of high score events, ~ 0.332, is higher than the precision overall, 0.172.\n```\n Score    #Gnd   #Above\nThresh   Truth   Thresh   #TP      Prec. * dRecall =  dAP    Total AP\n 0.844:   4794     933    310     0.3323 * 0.0647  = 0.0215   0.0215\n 0.809:   4794    1654    467     0.2823 * 0.0327  = 0.0092   0.0307\n 0.719:   4794    2845    676     0.2376 * 0.0436  = 0.0104   0.0411\n 0.045:   4794    4745    816     0.1720 * 0.0292  = 0.0050   0.0461\n```\n\n_ _ _ Some images from the fitting method:\n\nA sleep/awake feature is fit with a cosine to find approx middle-of-sleep locations for each night of a series:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fbc4a4bdf14756f952cf00448f5a87d22%2Ffit_to_find_events.png?generation=1698860035760187&alt=media)\n\nZooming in on an expected onset location, a step-down ramp is fit to get the time of onset. The plots here show that the predicted time for Night 9 (blue vertical line) is very close to the ground-truth time (green vertical line.) On the other hand, for Night 12 the predicted time is too early by about 50 minutes.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2F778d10aa034cc71fd887a572760e1c1f%2Fgood_fit_example_onset9.png?generation=1698860055664561&alt=media)\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fe220866b0f06a42d0c78fd1ee5e2ee22%2Fpoor_fit_example_onset12.png?generation=1698860071004311&alt=media)",
      "votes": 3
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2508418": "I took a simple transition-fitting approach to finding the onset and wakeup event locations in the [CMI 2023 - Sleepy Times](https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times) notebook; see some images below and some details in the \"Summary of the current algorithm\" at the notebook's beginning. This method got me an LB of 0.434 with the event `score` values all the same (1.0) because the fitting doesn't provide an appropriate measure of \"confidence\".\n\nBy adding a `score` value to each event, the LB for the same event predictions increased to 0.503. To set the scores I used a simple random forest model and trained it to predict the scaled error (0 to 1) of an event based on some feature values from the event's fitting; then setting score = 1 - level-of-error. This simple ML is done in the [CMI 2023 - Sleepy Times - phat ML](https://www.kaggle.com/code/dan3dewey/cmi-2023-sleepy-times-phat-ml) notebook using data in \"phat_frame.csv\" --  a submission-like file with many extra columns, including the true step values and fit feature values. The ML models are dill'ed and can be used by the main notebook to assign scores to the test events when run on test data.\n\nAssigned scores vs the errors:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fb50436c9118b0c9117e6c9bc6ebc404e%2Fassigned_scores_with_ML.png?generation=1698860103746733&alt=media)\n\n_ _ _ Example of score helping AP\n\nIncluding a score that is higher for more precise predictions can improve the AP value. For example, my recall for wakeups in the +/-12 step tolerance range is 0.172 ( =  # of predicted wakeups within +/-12 steps / # of all ground-truth wakeups.) Without a score, the precision is also about 0.170 ( =  # of predicted wakeups within +/-12 steps / # of all predicted wakeups) since there are similar numbers of predicted and ground-truth wakeups. So the recall-averaged AP will be 0.172 * 0.170 ~ **0.0292** .\n\nHowever when scores are assigned, a score threshold is used and only predicted events with scores above the threshold are considered in calculating the recall and precision. The precision with the score selection can be larger than overall, and the recall will be smaller. As the score threshold goes from high to low values the recall increases. Each time the threshold is lowered, the precision in the new selection is multiplied by the _increase in the recall_ and this product is added onto the Total AP.\n\nAs an example this table shows how the AP for wakeups with tol=12 steps is calculated using a few score thresholds. In this case the Total AP comes out to **0.0461** which is larger than the without-score value of 0.0292. This happens because the precision of high score events, ~ 0.332, is higher than the precision overall, 0.172.\n```\n Score    #Gnd   #Above\nThresh   Truth   Thresh   #TP      Prec. * dRecall =  dAP    Total AP\n 0.844:   4794     933    310     0.3323 * 0.0647  = 0.0215   0.0215\n 0.809:   4794    1654    467     0.2823 * 0.0327  = 0.0092   0.0307\n 0.719:   4794    2845    676     0.2376 * 0.0436  = 0.0104   0.0411\n 0.045:   4794    4745    816     0.1720 * 0.0292  = 0.0050   0.0461\n```\n\n_ _ _ Some images from the fitting method:\n\nA sleep/awake feature is fit with a cosine to find approx middle-of-sleep locations for each night of a series:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fbc4a4bdf14756f952cf00448f5a87d22%2Ffit_to_find_events.png?generation=1698860035760187&alt=media)\n\nZooming in on an expected onset location, a step-down ramp is fit to get the time of onset. The plots here show that the predicted time for Night 9 (blue vertical line) is very close to the ground-truth time (green vertical line.) On the other hand, for Night 12 the predicted time is too early by about 50 minutes.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2F778d10aa034cc71fd887a572760e1c1f%2Fgood_fit_example_onset9.png?generation=1698860055664561&alt=media)\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1496178%2Fe220866b0f06a42d0c78fd1ee5e2ee22%2Fpoor_fit_example_onset12.png?generation=1698860071004311&alt=media)"
  }
}