{
  "id": 116761,
  "title": "Trim the leading zeroes",
  "url": "/competitions/ashrae-energy-prediction/discussion/116761",
  "author_name": "Konrad Banachewicz",
  "post_date": "2019-11-11T09:34:08.672000",
  "votes": 20,
  "comment_count": 12,
  "views": 0,
  "content": "<p>Cursory EDA suggests - which is confirmed in a few places - that several buildings, especially those from site_id 0 and 1, have a long string of zeroes at the end (which according to the \"partial leak\" info do not repeat later). Some people have reported that trimming those improves the model performance, so I wrote a small R kernel creating such a trimmed version of the dataset:</p>\n\n<p><a href=\"https://www.kaggle.com/konradb/trim-leading-zeroes\">https://www.kaggle.com/konradb/trim-leading-zeroes</a></p>\n\n<p>Comments / suggestions / upvotes are most welcome.</p>\n\n<p>Edit: for some weird reason I am unable to make the notebook public (toggling access has no effect), so here's the code</p>\n\n<p>```</p>\n\n<h2>load the necessary packages</h2>\n\n<p>library(data.table)\nlibrary(dplyr)\nrequire(lubridate)\nrequire(stringr)\nrequire(Metrics)\nrequire(zoo)</p>\n\n<h2>load data</h2>\n\n<p>xtrain &lt;- fread('../input/ashrae-energy-prediction/train.csv')</p>\n\n<h1>the column we need to know what to drop</h1>\n\n<p>xtrain$tokeep &lt;- 0</p>\n\n<h2>detect the strings of zeroes ----</h2>\n\n<h1>list of unique buildings</h1>\n\n<p>building_list &lt;- unique(xtrain$building_id)</p>\n\n<h1>loop over buildings - done in a loop for convenience (I wanted to check results as they came without waiting for a complete job</h1>\n\n<h1>to finish. also, parallelization in data.table on Mac does not work out of the box)</h1>\n\n<p>for (bb in building_list[1:3])\n{\n  index0 &lt;- which(xtrain$building_id == bb)\n  x0 &lt;- xtrain[index0,]\n  x0[, <code>:=</code>(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]\n  # catch non-negative indices\n  idx &lt;- which(x0$rmx &gt; 0)\n  # start of a first contiguous block\n  cutoff &lt;- which(diff(idx) == 1)\n  start_here &lt;- idx[cutoff[1]]\n  # mark rows to drop\n  x0$tokeep[start_here:nrow(x0)] &lt;- 1\n  xtrain$tokeep[index0][start_here:nrow(x0)] &lt;- 1</p>\n\n<p>prop_retained = round(1 - start_here/nrow(x0),2)\n  print(paste(bb, ': ', prop_retained, ' kept', sep = ''))\n}</p>\n\n<h2>save trimmed version ----</h2>\n\n<p>xtr &lt;- xtrain[xtrain$tokeep == 1,]\nxtr$tokeep &lt;- NULL\nwrite.csv(xtr, 'train_trimmed.csv', row.names = F, quote = F)\n```</p>",
  "messages": [
    {
      "id": 670294,
      "postDate": "2019-11-11T09:34:08.673Z",
      "content": "<p>Cursory EDA suggests - which is confirmed in a few places - that several buildings, especially those from site_id 0 and 1, have a long string of zeroes at the end (which according to the \"partial leak\" info do not repeat later). Some people have reported that trimming those improves the model performance, so I wrote a small R kernel creating such a trimmed version of the dataset:</p>\n\n<p><a href=\"https://www.kaggle.com/konradb/trim-leading-zeroes\">https://www.kaggle.com/konradb/trim-leading-zeroes</a></p>\n\n<p>Comments / suggestions / upvotes are most welcome.</p>\n\n<p>Edit: for some weird reason I am unable to make the notebook public (toggling access has no effect), so here's the code</p>\n\n<p>```</p>\n\n<h2>load the necessary packages</h2>\n\n<p>library(data.table)\nlibrary(dplyr)\nrequire(lubridate)\nrequire(stringr)\nrequire(Metrics)\nrequire(zoo)</p>\n\n<h2>load data</h2>\n\n<p>xtrain &lt;- fread('../input/ashrae-energy-prediction/train.csv')</p>\n\n<h1>the column we need to know what to drop</h1>\n\n<p>xtrain$tokeep &lt;- 0</p>\n\n<h2>detect the strings of zeroes ----</h2>\n\n<h1>list of unique buildings</h1>\n\n<p>building_list &lt;- unique(xtrain$building_id)</p>\n\n<h1>loop over buildings - done in a loop for convenience (I wanted to check results as they came without waiting for a complete job</h1>\n\n<h1>to finish. also, parallelization in data.table on Mac does not work out of the box)</h1>\n\n<p>for (bb in building_list[1:3])\n{\n  index0 &lt;- which(xtrain$building_id == bb)\n  x0 &lt;- xtrain[index0,]\n  x0[, <code>:=</code>(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]\n  # catch non-negative indices\n  idx &lt;- which(x0$rmx &gt; 0)\n  # start of a first contiguous block\n  cutoff &lt;- which(diff(idx) == 1)\n  start_here &lt;- idx[cutoff[1]]\n  # mark rows to drop\n  x0$tokeep[start_here:nrow(x0)] &lt;- 1\n  xtrain$tokeep[index0][start_here:nrow(x0)] &lt;- 1</p>\n\n<p>prop_retained = round(1 - start_here/nrow(x0),2)\n  print(paste(bb, ': ', prop_retained, ' kept', sep = ''))\n}</p>\n\n<h2>save trimmed version ----</h2>\n\n<p>xtr &lt;- xtrain[xtrain$tokeep == 1,]\nxtr$tokeep &lt;- NULL\nwrite.csv(xtr, 'train_trimmed.csv', row.names = F, quote = F)\n```</p>",
      "rawMarkdown": "Cursory EDA suggests - which is confirmed in a few places - that several buildings, especially those from site_id 0 and 1, have a long string of zeroes at the end (which according to the \"partial leak\" info do not repeat later). Some people have reported that trimming those improves the model performance, so I wrote a small R kernel creating such a trimmed version of the dataset:\n\nhttps://www.kaggle.com/konradb/trim-leading-zeroes\n\nComments / suggestions / upvotes are most welcome.\n\n\nEdit: for some weird reason I am unable to make the notebook public (toggling access has no effect), so here's the code\n\n```\n## load the necessary packages\nlibrary(data.table)\nlibrary(dplyr)\nrequire(lubridate)\nrequire(stringr)\nrequire(Metrics)\nrequire(zoo)\n\n## load data\nxtrain &lt;- fread('../input/ashrae-energy-prediction/train.csv')\n\n# the column we need to know what to drop \nxtrain$tokeep &lt;- 0\n\n## detect the strings of zeroes ----\n\n# list of unique buildings \nbuilding_list &lt;- unique(xtrain$building_id)\n\n# loop over buildings - done in a loop for convenience (I wanted to check results as they came without waiting for a complete job \n# to finish. also, parallelization in data.table on Mac does not work out of the box)\n\nfor (bb in building_list[1:3])\n{\n  index0 &lt;- which(xtrain$building_id == bb)\n  x0 &lt;- xtrain[index0,]\n  x0[, `:=`(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]\n  # catch non-negative indices\n  idx &lt;- which(x0$rmx &gt; 0)\n  # start of a first contiguous block\n  cutoff &lt;- which(diff(idx) == 1)\n  start_here &lt;- idx[cutoff[1]]\n  # mark rows to drop\n  x0$tokeep[start_here:nrow(x0)] &lt;- 1\n  xtrain$tokeep[index0][start_here:nrow(x0)] &lt;- 1\n  \n  prop_retained = round(1 - start_here/nrow(x0),2)\n  print(paste(bb, ': ', prop_retained, ' kept', sep = ''))\n}\n\n## save trimmed version ----\nxtr &lt;- xtrain[xtrain$tokeep == 1,]\nxtr$tokeep &lt;- NULL\nwrite.csv(xtr, 'train_trimmed.csv', row.names = F, quote = F)\n```",
      "votes": 20
    },
    {
      "id": 677584,
      "postDate": "2019-11-20T10:54:35.473Z",
      "content": "<p><code>x0[,</code>:=<code>(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]</code>\nwhy aggregate by timestamp? in this way you have as output meter_reading (cause every timestamp is different) and not a rollmax.</p>",
      "rawMarkdown": "`x0[, `:=`(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]`\nwhy aggregate by timestamp? in this way you have as output meter_reading (cause every timestamp is different) and not a rollmax."
    },
    {
      "id": 677383,
      "postDate": "2019-11-20T04:44:49.707Z",
      "content": "<p>For python,\ndata cleaning can be done by slightly modifying @ marshi's code.\n<a href=\"https://www.kaggle.com/c/ashrae-energy-prediction/discussion/113774#latest-655011\">https://www.kaggle.com/c/ashrae-energy-prediction/discussion/113774#latest-655011</a></p>",
      "rawMarkdown": "For python,\ndata cleaning can be done by slightly modifying @ marshi's code.\nhttps://www.kaggle.com/c/ashrae-energy-prediction/discussion/113774#latest-655011"
    },
    {
      "id": 671611,
      "postDate": "2019-11-13T00:22:58.677Z",
      "content": "<p>Just wondering \n+ Why would trimming zeros after decimal may increase the performance of the model?</p>",
      "rawMarkdown": "Just wondering \n+ Why would trimming zeros after decimal may increase the performance of the model?",
      "replies": [
        {
          "id": 672840,
          "postDate": "2019-11-14T08:15:30.317Z",
          "content": "<p>Because they don't represent the test set <code>meter_reading</code> distribution. They exist only in training set.</p>",
          "rawMarkdown": "Because they don't represent the test set `meter_reading` distribution. They exist only in training set."
        },
        {
          "id": 673206,
          "postDate": "2019-11-14T17:04:01.640Z",
          "content": "<p>Leading zeroes refers to the zeroes after the decimal? Shouldn't that be trailing zeroes instead? I don't read the R code, so not too sure what Konrad is doing.</p>\n\n<p>How do you know the zeroes after decimal don't represent the test set meter_reading distribution?</p>",
          "rawMarkdown": "Leading zeroes refers to the zeroes after the decimal? Shouldn't that be trailing zeroes instead? I don't read the R code, so not too sure what Konrad is doing.\n\nHow do you know the zeroes after decimal don't represent the test set meter_reading distribution?"
        },
        {
          "id": 673250,
          "postDate": "2019-11-14T18:05:52.477Z",
          "content": "<p>It was imprecise language on my part: i mean blocks of zero observations at the start of the series.</p>",
          "rawMarkdown": "It was imprecise language on my part: i mean blocks of zero observations at the start of the series."
        },
        {
          "id": 674781,
          "postDate": "2019-11-17T04:15:28.067Z",
          "content": "<p>Hi Konrad, thanks for explaining. What's the reason that removing zero observations at the start (for siteid=0 presumably) lead to improved model?</p>",
          "rawMarkdown": "Hi Konrad, thanks for explaining. What's the reason that removing zero observations at the start (for siteid=0 presumably) lead to improved model?"
        }
      ]
    },
    {
      "id": 670300,
      "postDate": "2019-11-11T09:39:30.080Z",
      "content": "<p>The notebook seems to be private.</p>",
      "rawMarkdown": "The notebook seems to be private.",
      "replies": [
        {
          "id": 670308,
          "postDate": "2019-11-11T09:52:25.300Z",
          "content": "<p>Yeah, I noticed - sorry about that. For some reason the switch to public does not seem to work atm, perhaps because kernel is running? I am trying to fix it.</p>",
          "rawMarkdown": "Yeah, I noticed - sorry about that. For some reason the switch to public does not seem to work atm, perhaps because kernel is running? I am trying to fix it."
        },
        {
          "id": 670313,
          "postDate": "2019-11-11T10:02:20.913Z",
          "content": "<p>Set sharing to public while editing kernel</p>",
          "rawMarkdown": "Set sharing to public while editing kernel"
        },
        {
          "id": 670319,
          "postDate": "2019-11-11T10:06:53.473Z",
          "content": "<p>Ok, worked when done that way - should be fine now. Thanks.</p>",
          "rawMarkdown": "Ok, worked when done that way - should be fine now. Thanks."
        }
      ]
    },
    {
      "id": 675413,
      "postDate": "2019-11-18T03:48:55.337Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 677584,
      "author_name": "Davide Stenner",
      "author_url": "",
      "post_date": "2019-11-20T10:54:35.473000",
      "content": "<p><code>x0[,</code>:=<code>(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]</code>\nwhy aggregate by timestamp? in this way you have as output meter_reading (cause every timestamp is different) and not a rollmax.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 677383,
      "author_name": "cocoa",
      "author_url": "",
      "post_date": "2019-11-20T04:44:49.707000",
      "content": "<p>For python,\ndata cleaning can be done by slightly modifying @ marshi's code.\n<a href=\"https://www.kaggle.com/c/ashrae-energy-prediction/discussion/113774#latest-655011\">https://www.kaggle.com/c/ashrae-energy-prediction/discussion/113774#latest-655011</a></p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 671611,
      "author_name": "Vishal Sharma",
      "author_url": "",
      "post_date": "2019-11-13T00:22:58.677000",
      "content": "<p>Just wondering \n+ Why would trimming zeros after decimal may increase the performance of the model?</p>",
      "votes": 0,
      "replies": [
        {
          "id": 672840,
          "author_name": "Gunes Evitan",
          "author_url": "",
          "post_date": "2019-11-14T08:15:30.317000",
          "content": "<p>Because they don't represent the test set <code>meter_reading</code> distribution. They exist only in training set.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 673206,
          "author_name": "Lawrence Toh",
          "author_url": "",
          "post_date": "2019-11-14T17:04:01.640000",
          "content": "<p>Leading zeroes refers to the zeroes after the decimal? Shouldn't that be trailing zeroes instead? I don't read the R code, so not too sure what Konrad is doing.</p>\n\n<p>How do you know the zeroes after decimal don't represent the test set meter_reading distribution?</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 673250,
          "author_name": "Konrad Banachewicz",
          "author_url": "",
          "post_date": "2019-11-14T18:05:52.477000",
          "content": "<p>It was imprecise language on my part: i mean blocks of zero observations at the start of the series.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 674781,
          "author_name": "Lawrence Toh",
          "author_url": "",
          "post_date": "2019-11-17T04:15:28.067000",
          "content": "<p>Hi Konrad, thanks for explaining. What's the reason that removing zero observations at the start (for siteid=0 presumably) lead to improved model?</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 670300,
      "author_name": "Fredrik Jonsson",
      "author_url": "",
      "post_date": "2019-11-11T09:39:30.080000",
      "content": "<p>The notebook seems to be private.</p>",
      "votes": 0,
      "replies": [
        {
          "id": 670308,
          "author_name": "Konrad Banachewicz",
          "author_url": "",
          "post_date": "2019-11-11T09:52:25.300000",
          "content": "<p>Yeah, I noticed - sorry about that. For some reason the switch to public does not seem to work atm, perhaps because kernel is running? I am trying to fix it.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 670313,
          "author_name": "Gunes Evitan",
          "author_url": "",
          "post_date": "2019-11-11T10:02:20.913000",
          "content": "<p>Set sharing to public while editing kernel</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 670319,
          "author_name": "Konrad Banachewicz",
          "author_url": "",
          "post_date": "2019-11-11T10:06:53.473000",
          "content": "<p>Ok, worked when done that way - should be fine now. Thanks.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 675413,
      "author_name": "",
      "author_url": "",
      "post_date": "2019-11-18T03:48:55.337000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "670294": "Cursory EDA suggests - which is confirmed in a few places - that several buildings, especially those from site_id 0 and 1, have a long string of zeroes at the end (which according to the \"partial leak\" info do not repeat later). Some people have reported that trimming those improves the model performance, so I wrote a small R kernel creating such a trimmed version of the dataset:\n\nhttps://www.kaggle.com/konradb/trim-leading-zeroes\n\nComments / suggestions / upvotes are most welcome.\n\n\nEdit: for some weird reason I am unable to make the notebook public (toggling access has no effect), so here's the code\n\n```\n## load the necessary packages\nlibrary(data.table)\nlibrary(dplyr)\nrequire(lubridate)\nrequire(stringr)\nrequire(Metrics)\nrequire(zoo)\n\n## load data\nxtrain &lt;- fread('../input/ashrae-energy-prediction/train.csv')\n\n# the column we need to know what to drop \nxtrain$tokeep &lt;- 0\n\n## detect the strings of zeroes ----\n\n# list of unique buildings \nbuilding_list &lt;- unique(xtrain$building_id)\n\n# loop over buildings - done in a loop for convenience (I wanted to check results as they came without waiting for a complete job \n# to finish. also, parallelization in data.table on Mac does not work out of the box)\n\nfor (bb in building_list[1:3])\n{\n  index0 &lt;- which(xtrain$building_id == bb)\n  x0 &lt;- xtrain[index0,]\n  x0[, `:=`(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]\n  # catch non-negative indices\n  idx &lt;- which(x0$rmx &gt; 0)\n  # start of a first contiguous block\n  cutoff &lt;- which(diff(idx) == 1)\n  start_here &lt;- idx[cutoff[1]]\n  # mark rows to drop\n  x0$tokeep[start_here:nrow(x0)] &lt;- 1\n  xtrain$tokeep[index0][start_here:nrow(x0)] &lt;- 1\n  \n  prop_retained = round(1 - start_here/nrow(x0),2)\n  print(paste(bb, ': ', prop_retained, ' kept', sep = ''))\n}\n\n## save trimmed version ----\nxtr &lt;- xtrain[xtrain$tokeep == 1,]\nxtr$tokeep &lt;- NULL\nwrite.csv(xtr, 'train_trimmed.csv', row.names = F, quote = F)\n```",
    "677584": "`x0[, `:=`(rmx = rollmax(meter_reading, k = 50, partial = TRUE, fill = 0, align = 'right')),\n         by= list(building_id, timestamp)]`\nwhy aggregate by timestamp? in this way you have as output meter_reading (cause every timestamp is different) and not a rollmax.",
    "677383": "For python,\ndata cleaning can be done by slightly modifying @ marshi's code.\nhttps://www.kaggle.com/c/ashrae-energy-prediction/discussion/113774#latest-655011",
    "671611": "Just wondering \n+ Why would trimming zeros after decimal may increase the performance of the model?",
    "670300": "The notebook seems to be private.",
    "675413": ""
  }
}