{
  "id": 47124,
  "title": "log1p transformation? why?",
  "url": "/competitions/favorita-grocery-sales-forecasting/discussion/47124",
  "author_name": "",
  "post_date": "2018-01-09T02:08:20.527723600Z",
  "votes": 9,
  "comment_count": 8,
  "views": 0,
  "content": "<p>Hi,</p>\n\n<p>I saw people used numpy.log1p to transform their data before modeling. I am wondering why people do that (instead of log) and what the mathematical explanation is? Thanks!</p>",
  "messages": [
    {
      "id": "266518",
      "postDate": "01/09/2018 02:08:20",
      "content": "<p>Hi,</p>\n\n<p>I saw people used numpy.log1p to transform their data before modeling. I am wondering why people do that (instead of log) and what the mathematical explanation is? Thanks!</p>",
      "rawMarkdown": "Hi,\n\nI saw people used numpy.log1p to transform their data before modeling. I am wondering why people do that (instead of log) and what the mathematical explanation is? Thanks!",
      "votes": null
    },
    {
      "id": "266531",
      "postDate": "01/09/2018 02:51:56",
      "content": "<p>The short answer is logarithm of zero is undefined.</p>",
      "rawMarkdown": "The short answer is logarithm of zero is undefined.",
      "votes": null
    },
    {
      "id": "266551",
      "postDate": "01/09/2018 03:52:17",
      "content": "<p>If you have data that is not normally distributed, you're limited in how you can approach/analyze/model it. Normal distributions are convenient and the tools we have for dealing with them are powerful. Thus, many techniques involve making things look like normal distributions.</p>\n\n<p>For example, if the data is bi-modal, then we can ask if it looks like the sum of two different normal distributions. If the data is skewed, then we can ask if it looks log-normal. Etc., etc. In our case, a lot of the data appears to be log-normal.</p>\n\n<p>See: <a href=\"https://en.wikipedia.org/wiki/Log-normal_distribution\">https://en.wikipedia.org/wiki/Log-normal_distribution</a></p>",
      "rawMarkdown": "If you have data that is not normally distributed, you're limited in how you can approach/analyze/model it. Normal distributions are convenient and the tools we have for dealing with them are powerful. Thus, many techniques involve making things look like normal distributions.\n\nFor example, if the data is bi-modal, then we can ask if it looks like the sum of two different normal distributions. If the data is skewed, then we can ask if it looks log-normal. Etc., etc. In our case, a lot of the data appears to be log-normal.\n\nSee: https://en.wikipedia.org/wiki/Log-normal_distribution",
      "votes": null
    },
    {
      "id": "266553",
      "postDate": "01/09/2018 03:59:28",
      "content": "<p>I should add, there are many techniques for transforming data. It's more of an art, than a science. Statisticians always have pretty fancy arsenals. But you can get by with a few simple tricks when you know how to use them correctly. But remember the goal is to get something that is easier to model, i.e. gets a better fit. So you can always test something out and see how it performs if you're unsure about whether a certain transformation is appropriate or not.</p>",
      "rawMarkdown": "I should add, there are many techniques for transforming data. It's more of an art, than a science. Statisticians always have pretty fancy arsenals. But you can get by with a few simple tricks when you know how to use them correctly. But remember the goal is to get something that is easier to model, i.e. gets a better fit. So you can always test something out and see how it performs if you're unsure about whether a certain transformation is appropriate or not.",
      "votes": null
    },
    {
      "id": "266862",
      "postDate": "01/10/2018 01:37:25",
      "content": "<p>0</p>",
      "rawMarkdown": "0",
      "votes": null
    },
    {
      "id": "266910",
      "postDate": "01/10/2018 05:49:09",
      "content": "<p>It is because of the evaluation metric.   If you transform the unit sales with log1p then the evaluation metric is the same as rmse with weights, and you can use built-in objective (loss) functions from your favorite algorithm.</p>\n\n<p>The next question is why did they select log1p?  2 reasons:</p>\n\n<ol>\n<li><p>log transform makes large sales look similar to small sales: a x % variation will cost the same, whatever the baseline is.</p></li>\n<li><p>As Ceshine Lee wrote, log transform cannot be applied directly given there can be 0 unit sales.  Log1p is one way to mitigate this, but it is not keeping the good property of variation cost being independent from baseline.  I would have used something like log(1 + a x) where a is significantly larger than 1 to get best of both.  That's what i used in the Web Traffic Forecasting competition, and it led to much better results than log1p.  But here we have to use log1p to match the evaluation metrics anyway...</p></li>\n</ol>",
      "rawMarkdown": "It is because of the evaluation metric.   If you transform the unit sales with log1p then the evaluation metric is the same as rmse with weights, and you can use built-in objective (loss) functions from your favorite algorithm.\n\nThe next question is why did they select log1p?  2 reasons:\n\n 1. log transform makes large sales look similar to small sales: a x % variation will cost the same, whatever the baseline is.\n\n 2. As Ceshine Lee wrote, log transform cannot be applied directly given there can be 0 unit sales.  Log1p is one way to mitigate this, but it is not keeping the good property of variation cost being independent from baseline.  I would have used something like log(1 + a x) where a is significantly larger than 1 to get best of both.  That's what i used in the Web Traffic Forecasting competition, and it led to much better results than log1p.  But here we have to use log1p to match the evaluation metrics anyway...",
      "votes": null
    },
    {
      "id": "266938",
      "postDate": "01/10/2018 07:01:14",
      "content": "<p>Thanks for the elegant explanation.</p>\n\n<p>Directly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales. I guess whether it is intentional or not can only be answered by Kaggle and the sponsor.</p>",
      "rawMarkdown": "Thanks for the elegant explanation.\n\nDirectly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales. I guess whether it is intentional or not can only be answered by Kaggle and the sponsor.",
      "votes": null
    },
    {
      "id": "267002",
      "postDate": "01/10/2018 10:47:18",
      "content": "<p>Thanks.</p>\n\n<p>&gt; Directly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales</p>\n\n<p>Exactly, small volume series do not matter much here. </p>",
      "rawMarkdown": "Thanks.\n\n&gt; Directly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales\n\nExactly, small volume series do not matter much here.",
      "votes": null
    },
    {
      "id": "1210512",
      "postDate": "02/19/2021 13:46:03",
      "content": "<p>applied log1p which make my predictions 1 and zeros but  i am doing a regresson task can please help me</p>",
      "rawMarkdown": "applied log1p which make my predictions 1 and zeros but  i am doing a regresson task can please help me",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1210512,
      "author_name": "subbugalam",
      "author_url": "",
      "post_date": "02/19/2021 13:46:03",
      "content": "<p>applied log1p which make my predictions 1 and zeros but  i am doing a regresson task can please help me</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 266531,
      "author_name": "ceshine",
      "author_url": "",
      "post_date": "01/09/2018 02:51:56",
      "content": "<p>The short answer is logarithm of zero is undefined.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 266551,
      "author_name": "puremath86",
      "author_url": "",
      "post_date": "01/09/2018 03:52:17",
      "content": "<p>If you have data that is not normally distributed, you're limited in how you can approach/analyze/model it. Normal distributions are convenient and the tools we have for dealing with them are powerful. Thus, many techniques involve making things look like normal distributions.</p>\n\n<p>For example, if the data is bi-modal, then we can ask if it looks like the sum of two different normal distributions. If the data is skewed, then we can ask if it looks log-normal. Etc., etc. In our case, a lot of the data appears to be log-normal.</p>\n\n<p>See: <a href=\"https://en.wikipedia.org/wiki/Log-normal_distribution\">https://en.wikipedia.org/wiki/Log-normal_distribution</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 266553,
          "author_name": "puremath86",
          "author_url": "",
          "post_date": "01/09/2018 03:59:28",
          "content": "<p>I should add, there are many techniques for transforming data. It's more of an art, than a science. Statisticians always have pretty fancy arsenals. But you can get by with a few simple tricks when you know how to use them correctly. But remember the goal is to get something that is easier to model, i.e. gets a better fit. So you can always test something out and see how it performs if you're unsure about whether a certain transformation is appropriate or not.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 266862,
      "author_name": "zhoukj",
      "author_url": "",
      "post_date": "01/10/2018 01:37:25",
      "content": "<p>0</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 266910,
      "author_name": "cpmpml",
      "author_url": "",
      "post_date": "01/10/2018 05:49:09",
      "content": "<p>It is because of the evaluation metric.   If you transform the unit sales with log1p then the evaluation metric is the same as rmse with weights, and you can use built-in objective (loss) functions from your favorite algorithm.</p>\n\n<p>The next question is why did they select log1p?  2 reasons:</p>\n\n<ol>\n<li><p>log transform makes large sales look similar to small sales: a x % variation will cost the same, whatever the baseline is.</p></li>\n<li><p>As Ceshine Lee wrote, log transform cannot be applied directly given there can be 0 unit sales.  Log1p is one way to mitigate this, but it is not keeping the good property of variation cost being independent from baseline.  I would have used something like log(1 + a x) where a is significantly larger than 1 to get best of both.  That's what i used in the Web Traffic Forecasting competition, and it led to much better results than log1p.  But here we have to use log1p to match the evaluation metrics anyway...</p></li>\n</ol>",
      "votes": null,
      "replies": [
        {
          "id": 266938,
          "author_name": "ceshine",
          "author_url": "",
          "post_date": "01/10/2018 07:01:14",
          "content": "<p>Thanks for the elegant explanation.</p>\n\n<p>Directly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales. I guess whether it is intentional or not can only be answered by Kaggle and the sponsor.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 267002,
          "author_name": "cpmpml",
          "author_url": "",
          "post_date": "01/10/2018 10:47:18",
          "content": "<p>Thanks.</p>\n\n<p>&gt; Directly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales</p>\n\n<p>Exactly, small volume series do not matter much here. </p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "266518": "Hi,\n\nI saw people used numpy.log1p to transform their data before modeling. I am wondering why people do that (instead of log) and what the mathematical explanation is? Thanks!",
    "266531": "The short answer is logarithm of zero is undefined.",
    "266551": "If you have data that is not normally distributed, you're limited in how you can approach/analyze/model it. Normal distributions are convenient and the tools we have for dealing with them are powerful. Thus, many techniques involve making things look like normal distributions.\n\nFor example, if the data is bi-modal, then we can ask if it looks like the sum of two different normal distributions. If the data is skewed, then we can ask if it looks log-normal. Etc., etc. In our case, a lot of the data appears to be log-normal.\n\nSee: https://en.wikipedia.org/wiki/Log-normal_distribution",
    "266553": "I should add, there are many techniques for transforming data. It's more of an art, than a science. Statisticians always have pretty fancy arsenals. But you can get by with a few simple tricks when you know how to use them correctly. But remember the goal is to get something that is easier to model, i.e. gets a better fit. So you can always test something out and see how it performs if you're unsure about whether a certain transformation is appropriate or not.",
    "266862": "0",
    "266910": "It is because of the evaluation metric.   If you transform the unit sales with log1p then the evaluation metric is the same as rmse with weights, and you can use built-in objective (loss) functions from your favorite algorithm.\n\nThe next question is why did they select log1p?  2 reasons:\n\n 1. log transform makes large sales look similar to small sales: a x % variation will cost the same, whatever the baseline is.\n\n 2. As Ceshine Lee wrote, log transform cannot be applied directly given there can be 0 unit sales.  Log1p is one way to mitigate this, but it is not keeping the good property of variation cost being independent from baseline.  I would have used something like log(1 + a x) where a is significantly larger than 1 to get best of both.  That's what i used in the Web Traffic Forecasting competition, and it led to much better results than log1p.  But here we have to use log1p to match the evaluation metrics anyway...",
    "266938": "Thanks for the elegant explanation.\n\nDirectly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales. I guess whether it is intentional or not can only be answered by Kaggle and the sponsor.",
    "267002": "Thanks.\n\n&gt; Directly using log(1 + x) does have a side effect of de-emphasizing the variation for smaller unit sales\n\nExactly, small volume series do not matter much here.",
    "1210512": "applied log1p which make my predictions 1 and zeros but  i am doing a regresson task can please help me"
  },
  "source": "meta"
}