{
  "id": 43768,
  "title": "xgboost custome objective function",
  "url": "/competitions/favorita-grocery-sales-forecasting/discussion/43768",
  "author_name": "",
  "post_date": "2017-11-19T08:46:54.608642900Z",
  "votes": 1,
  "comment_count": 2,
  "views": 0,
  "content": "<p>hey\ni am trying to build the right objective function for that case\ni have no idea how to do that\nthat my code</p>\n\n<p>import xgboost as xgb</p>\n\n<p>def NWRMSLE(y, pred, w):\n    return metrics.mean_squared_error(y, pred, sample_weight=w)**0.5</p>\n\n<p>param = {'max_depth': 2, 'eta': 1, 'silent': 1}\ndtrain=xgb.DMatrix(train_x, label=train_y)\nxgb.train(param, dtrain,NWRMSLE)</p>\n\n<p>i also tried using XGBRegressor\nand doing :\nXGBRegressor(objective=NWRMSLE)</p>\n\n<p>what should i do?\nthanks</p>",
  "messages": [
    {
      "id": "245682",
      "postDate": "11/19/2017 08:46:54",
      "content": "<p>hey\ni am trying to build the right objective function for that case\ni have no idea how to do that\nthat my code</p>\n\n<p>import xgboost as xgb</p>\n\n<p>def NWRMSLE(y, pred, w):\n    return metrics.mean_squared_error(y, pred, sample_weight=w)**0.5</p>\n\n<p>param = {'max_depth': 2, 'eta': 1, 'silent': 1}\ndtrain=xgb.DMatrix(train_x, label=train_y)\nxgb.train(param, dtrain,NWRMSLE)</p>\n\n<p>i also tried using XGBRegressor\nand doing :\nXGBRegressor(objective=NWRMSLE)</p>\n\n<p>what should i do?\nthanks</p>",
      "rawMarkdown": "hey\ni am trying to build the right objective function for that case\ni have no idea how to do that\nthat my code\n\nimport xgboost as xgb\n\ndef NWRMSLE(y, pred, w):\n    return metrics.mean_squared_error(y, pred, sample_weight=w)**0.5\n\nparam = {'max_depth': 2, 'eta': 1, 'silent': 1}\ndtrain=xgb.DMatrix(train_x, label=train_y)\nxgb.train(param, dtrain,NWRMSLE)\n\n i also tried using XGBRegressor\nand doing :\nXGBRegressor(objective=NWRMSLE)\n\nwhat should i do?\nthanks",
      "votes": null
    },
    {
      "id": "245845",
      "postDate": "11/19/2017 21:26:50",
      "content": "<p>In xgb model.fit() , try parameter eval_metric= NWRMSLE</p>",
      "rawMarkdown": "In xgb model.fit() , try parameter eval_metric= NWRMSLE",
      "votes": null
    },
    {
      "id": "246825",
      "postDate": "11/21/2017 20:17:16",
      "content": "<p>First of all, there's a common misconception that eval_metric is the objective function.  It's not.. eval_metric is just reporting the score of the model.  It can also be used for early stopping rounds (how many rounds of boosting is optimal?) and in this sense it can affect the construction of the model.  But otherwise, eval_metric is just a reporting tool.</p>\n\n<p>The actual model construction is dictated by the<code>obj</code> parameter (at least in R).  You can see some of these <a href=\"https://github.com/dmlc/xgboost/blob/master/doc/parameter.md#learning-task-parameters\">here</a>.  There is no NWRMSLE.  However, it's rarely the case that a ML model is going to have the exact objective you're looking for.  Rather than trying to define your own objective, it's usually easier to transform your data, y' := some_transformation(y) so that you can optimize one of the pre-packaged objectives on y' and it will behave as if you're optimizing y subject to some other objective function.</p>\n\n<p>In this case, take a look at the <a href=\"https://www.kaggle.com/c/favorita-grocery-sales-forecasting#evaluation\">formula for NWRMSLE</a>.  Notice how, if you set zhat = ln(yhat + 1) and z = ln(y + 1), get rid of the square root (which fine because it's a monotonic function.  argmin f(x) is the same as argmin sqrt(f(x)) for any function f) then you're left with Loss = Weighted Mean Squared Error of (z, zhat).  In other words, you can just create columns in your data: z := ln(y + 1) and zhat := ln(yhat + 1) and then use xgb with <code>obj = \"reg:linear\"</code>.  You'll also need to include weights in the xgb.DMatrix for perishable/non-perishable items.</p>",
      "rawMarkdown": "First of all, there's a common misconception that eval_metric is the objective function.  It's not.. eval_metric is just reporting the score of the model.  It can also be used for early stopping rounds (how many rounds of boosting is optimal?) and in this sense it can affect the construction of the model.  But otherwise, eval_metric is just a reporting tool.\n\nThe actual model construction is dictated by the`obj` parameter (at least in R).  You can see some of these [here][1].  There is no NWRMSLE.  However, it's rarely the case that a ML model is going to have the exact objective you're looking for.  Rather than trying to define your own objective, it's usually easier to transform your data, y' := some_transformation(y) so that you can optimize one of the pre-packaged objectives on y' and it will behave as if you're optimizing y subject to some other objective function.\n\nIn this case, take a look at the [formula for NWRMSLE][2].  Notice how, if you set zhat = ln(yhat + 1) and z = ln(y + 1), get rid of the square root (which fine because it's a monotonic function.  argmin f(x) is the same as argmin sqrt(f(x)) for any function f) then you're left with Loss = Weighted Mean Squared Error of (z, zhat).  In other words, you can just create columns in your data: z := ln(y + 1) and zhat := ln(yhat + 1) and then use xgb with `obj = \"reg:linear\"`.  You'll also need to include weights in the xgb.DMatrix for perishable/non-perishable items.\n\n  [1]: https://github.com/dmlc/xgboost/blob/master/doc/parameter.md#learning-task-parameters\n  [2]: https://www.kaggle.com/c/favorita-grocery-sales-forecasting#evaluation",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 245845,
      "author_name": "rashmibanthia",
      "author_url": "",
      "post_date": "11/19/2017 21:26:50",
      "content": "<p>In xgb model.fit() , try parameter eval_metric= NWRMSLE</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 246825,
      "author_name": "ben519",
      "author_url": "",
      "post_date": "11/21/2017 20:17:16",
      "content": "<p>First of all, there's a common misconception that eval_metric is the objective function.  It's not.. eval_metric is just reporting the score of the model.  It can also be used for early stopping rounds (how many rounds of boosting is optimal?) and in this sense it can affect the construction of the model.  But otherwise, eval_metric is just a reporting tool.</p>\n\n<p>The actual model construction is dictated by the<code>obj</code> parameter (at least in R).  You can see some of these <a href=\"https://github.com/dmlc/xgboost/blob/master/doc/parameter.md#learning-task-parameters\">here</a>.  There is no NWRMSLE.  However, it's rarely the case that a ML model is going to have the exact objective you're looking for.  Rather than trying to define your own objective, it's usually easier to transform your data, y' := some_transformation(y) so that you can optimize one of the pre-packaged objectives on y' and it will behave as if you're optimizing y subject to some other objective function.</p>\n\n<p>In this case, take a look at the <a href=\"https://www.kaggle.com/c/favorita-grocery-sales-forecasting#evaluation\">formula for NWRMSLE</a>.  Notice how, if you set zhat = ln(yhat + 1) and z = ln(y + 1), get rid of the square root (which fine because it's a monotonic function.  argmin f(x) is the same as argmin sqrt(f(x)) for any function f) then you're left with Loss = Weighted Mean Squared Error of (z, zhat).  In other words, you can just create columns in your data: z := ln(y + 1) and zhat := ln(yhat + 1) and then use xgb with <code>obj = \"reg:linear\"</code>.  You'll also need to include weights in the xgb.DMatrix for perishable/non-perishable items.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "245682": "hey\ni am trying to build the right objective function for that case\ni have no idea how to do that\nthat my code\n\nimport xgboost as xgb\n\ndef NWRMSLE(y, pred, w):\n    return metrics.mean_squared_error(y, pred, sample_weight=w)**0.5\n\nparam = {'max_depth': 2, 'eta': 1, 'silent': 1}\ndtrain=xgb.DMatrix(train_x, label=train_y)\nxgb.train(param, dtrain,NWRMSLE)\n\n i also tried using XGBRegressor\nand doing :\nXGBRegressor(objective=NWRMSLE)\n\nwhat should i do?\nthanks",
    "245845": "In xgb model.fit() , try parameter eval_metric= NWRMSLE",
    "246825": "First of all, there's a common misconception that eval_metric is the objective function.  It's not.. eval_metric is just reporting the score of the model.  It can also be used for early stopping rounds (how many rounds of boosting is optimal?) and in this sense it can affect the construction of the model.  But otherwise, eval_metric is just a reporting tool.\n\nThe actual model construction is dictated by the`obj` parameter (at least in R).  You can see some of these [here][1].  There is no NWRMSLE.  However, it's rarely the case that a ML model is going to have the exact objective you're looking for.  Rather than trying to define your own objective, it's usually easier to transform your data, y' := some_transformation(y) so that you can optimize one of the pre-packaged objectives on y' and it will behave as if you're optimizing y subject to some other objective function.\n\nIn this case, take a look at the [formula for NWRMSLE][2].  Notice how, if you set zhat = ln(yhat + 1) and z = ln(y + 1), get rid of the square root (which fine because it's a monotonic function.  argmin f(x) is the same as argmin sqrt(f(x)) for any function f) then you're left with Loss = Weighted Mean Squared Error of (z, zhat).  In other words, you can just create columns in your data: z := ln(y + 1) and zhat := ln(yhat + 1) and then use xgb with `obj = \"reg:linear\"`.  You'll also need to include weights in the xgb.DMatrix for perishable/non-perishable items.\n\n  [1]: https://github.com/dmlc/xgboost/blob/master/doc/parameter.md#learning-task-parameters\n  [2]: https://www.kaggle.com/c/favorita-grocery-sales-forecasting#evaluation"
  },
  "source": "meta"
}