{
  "id": 21996,
  "title": "XGB newbie stuck...",
  "url": "/competitions/grupo-bimbo-inventory-demand/discussion/21996",
  "author_name": "",
  "post_date": "2016-07-01T17:08:10.677Z",
  "votes": null,
  "comment_count": 2,
  "views": 933,
  "content": "<p>I am new to using XGBoost and trying to use a regression objective  (then want to do poisson if I can ever get it working).  </p>\n\n<p>I've attempted to use the following code and at the point of PREDICT I get the error:\n<strong>Error in xgb.DMatrix(newdata) : \n  REAL() can only be applied to a 'numeric', not a 'integer'</strong></p>\n\n<p>I've tried using lapply to the matrix, I've tried setting a variable to numeric, the training seems to move through, it's the prediction step that bombs.  </p>\n\n<p>train$id &lt;- NULL;\ntest$id &lt;- NULL;</p>\n\n<p>cat(&quot;Treating features...\\n&quot;);</p>\n\n<h2>impute train and test here</h2>\n\n<h1>one hot encoding</h1>\n\n<p>sparse_matrix &lt;- sparse.model.matrix(Demanda_uni_equil ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = train)\nsparse_matrix &lt;- sparse.model.matrix( ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = test)\ntrain$Cliente_ID&lt;-as.numeric(train$Cliente_ID)\ntrain.y = train$Demanda_uni_equil;</p>\n\n<p>cat(&quot;Dropping features...\\n&quot;);\ntrain$Demanda_uni_equil &lt;- NULL;</p>\n\n<p>cat(&quot;Creating data.matrix...\\n&quot;);\npreds &lt;- rep(0,nrow(test));\ntrainM&lt;-data.matrix(train, rownames.force = NA);\ncat(&quot;Creating DMatrix for xgboost...\\n&quot;);\ndtrain &lt;- xgb.DMatrix(data=trainM, label=train.y, missing = NaN);\nwatchlist &lt;- list(trainM=dtrain);</p>\n\n<p>set.seed(1969);</p>\n\n<p>param &lt;- list(  objective           = &quot;reg:linear&quot;, \n                booster             = &quot;gblinear&quot;,\n                #eval_metric         =&quot;mae&quot;,\n                eta                 = 0.035,\n                max_depth           = 6,\n                subsample           = 1.00,\n                colsample_bytree    = 0.40\n)</p>\n\n<p>clf &lt;- xgb.cv(  params              = param, \n                data                = dtrain, \n                nrounds             = 200, \n                verbose             = 1,\n                watchlist           = watchlist,\n                maximize            = FALSE,\n                nfold               = 3,\n                early.stop.round    = 10,\n                print.every.n       = 1\n);</p>\n\n<p>bestRound &lt;- which.min( as.matrix(clf)[,3] );\ncat(&quot;Best round:&quot;, bestRound,&quot;\\n&quot;);\ncat(&quot;Best result:&quot;,min(as.matrix(clf)[,3]),&quot;\\n&quot;);</p>\n\n<p>clf &lt;- xgb.train(   params              = param, \n                    data                = dtrain, \n                    nrounds             = bestRound, \n                    verbose             = 1,\n                    watchlist           = watchlist,\n                    maximize            = FALSE\n)\ntestM &lt;-data.matrix(test, rownames.force = NA);</p>\n\n<p><strong>preds &lt;- predict(clf, testM);</strong></p>",
  "messages": [
    {
      "id": "125701",
      "postDate": "07/01/2016 17:08:10",
      "content": "<p>I am new to using XGBoost and trying to use a regression objective  (then want to do poisson if I can ever get it working).  </p>\n\n<p>I've attempted to use the following code and at the point of PREDICT I get the error:\n<strong>Error in xgb.DMatrix(newdata) : \n  REAL() can only be applied to a 'numeric', not a 'integer'</strong></p>\n\n<p>I've tried using lapply to the matrix, I've tried setting a variable to numeric, the training seems to move through, it's the prediction step that bombs.  </p>\n\n<p>train$id &lt;- NULL;\ntest$id &lt;- NULL;</p>\n\n<p>cat(&quot;Treating features...\\n&quot;);</p>\n\n<h2>impute train and test here</h2>\n\n<h1>one hot encoding</h1>\n\n<p>sparse_matrix &lt;- sparse.model.matrix(Demanda_uni_equil ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = train)\nsparse_matrix &lt;- sparse.model.matrix( ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = test)\ntrain$Cliente_ID&lt;-as.numeric(train$Cliente_ID)\ntrain.y = train$Demanda_uni_equil;</p>\n\n<p>cat(&quot;Dropping features...\\n&quot;);\ntrain$Demanda_uni_equil &lt;- NULL;</p>\n\n<p>cat(&quot;Creating data.matrix...\\n&quot;);\npreds &lt;- rep(0,nrow(test));\ntrainM&lt;-data.matrix(train, rownames.force = NA);\ncat(&quot;Creating DMatrix for xgboost...\\n&quot;);\ndtrain &lt;- xgb.DMatrix(data=trainM, label=train.y, missing = NaN);\nwatchlist &lt;- list(trainM=dtrain);</p>\n\n<p>set.seed(1969);</p>\n\n<p>param &lt;- list(  objective           = &quot;reg:linear&quot;, \n                booster             = &quot;gblinear&quot;,\n                #eval_metric         =&quot;mae&quot;,\n                eta                 = 0.035,\n                max_depth           = 6,\n                subsample           = 1.00,\n                colsample_bytree    = 0.40\n)</p>\n\n<p>clf &lt;- xgb.cv(  params              = param, \n                data                = dtrain, \n                nrounds             = 200, \n                verbose             = 1,\n                watchlist           = watchlist,\n                maximize            = FALSE,\n                nfold               = 3,\n                early.stop.round    = 10,\n                print.every.n       = 1\n);</p>\n\n<p>bestRound &lt;- which.min( as.matrix(clf)[,3] );\ncat(&quot;Best round:&quot;, bestRound,&quot;\\n&quot;);\ncat(&quot;Best result:&quot;,min(as.matrix(clf)[,3]),&quot;\\n&quot;);</p>\n\n<p>clf &lt;- xgb.train(   params              = param, \n                    data                = dtrain, \n                    nrounds             = bestRound, \n                    verbose             = 1,\n                    watchlist           = watchlist,\n                    maximize            = FALSE\n)\ntestM &lt;-data.matrix(test, rownames.force = NA);</p>\n\n<p><strong>preds &lt;- predict(clf, testM);</strong></p>",
      "rawMarkdown": "I am new to using XGBoost and trying to use a regression objective  (then want to do poisson if I can ever get it working).  \r\n\r\nI've attempted to use the following code and at the point of PREDICT I get the error:\r\n**Error in xgb.DMatrix(newdata) : \r\n  REAL() can only be applied to a 'numeric', not a 'integer'**\r\n\r\nI've tried using lapply to the matrix, I've tried setting a variable to numeric, the training seems to move through, it's the prediction step that bombs.  \r\n\r\n\r\n\r\n\r\ntrain$id <- NULL;\r\ntest$id <- NULL;\r\n\r\ncat(\"Treating features...\\n\");\r\n## impute train and test here\r\n#one hot encoding\r\nsparse_matrix <- sparse.model.matrix(Demanda_uni_equil ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = train)\r\nsparse_matrix <- sparse.model.matrix( ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = test)\r\ntrain$Cliente_ID<-as.numeric(train$Cliente_ID)\r\ntrain.y = train$Demanda_uni_equil;\r\n\r\ncat(\"Dropping features...\\n\");\r\ntrain$Demanda_uni_equil <- NULL;\r\n\r\n\r\ncat(\"Creating data.matrix...\\n\");\r\npreds <- rep(0,nrow(test));\r\ntrainM<-data.matrix(train, rownames.force = NA);\r\ncat(\"Creating DMatrix for xgboost...\\n\");\r\ndtrain <- xgb.DMatrix(data=trainM, label=train.y, missing = NaN);\r\nwatchlist <- list(trainM=dtrain);\r\n\r\nset.seed(1969);\r\n\r\nparam <- list(  objective           = \"reg:linear\", \r\n                booster             = \"gblinear\",\r\n                #eval_metric         =\"mae\",\r\n                eta                 = 0.035,\r\n                max_depth           = 6,\r\n                subsample           = 1.00,\r\n                colsample_bytree    = 0.40\r\n)\r\n\r\nclf <- xgb.cv(  params              = param, \r\n                data                = dtrain, \r\n                nrounds             = 200, \r\n                verbose             = 1,\r\n                watchlist           = watchlist,\r\n                maximize            = FALSE,\r\n                nfold               = 3,\r\n                early.stop.round    = 10,\r\n                print.every.n       = 1\r\n);\r\n\r\nbestRound <- which.min( as.matrix(clf)[,3] );\r\ncat(\"Best round:\", bestRound,\"\\n\");\r\ncat(\"Best result:\",min(as.matrix(clf)[,3]),\"\\n\");\r\n\r\nclf <- xgb.train(   params              = param, \r\n                    data                = dtrain, \r\n                    nrounds             = bestRound, \r\n                    verbose             = 1,\r\n                    watchlist           = watchlist,\r\n                    maximize            = FALSE\r\n)\r\ntestM <-data.matrix(test, rownames.force = NA);\r\n\r\n**preds <- predict(clf, testM);**",
      "votes": null
    },
    {
      "id": "125702",
      "postDate": "07/01/2016 17:14:08",
      "content": "<p>I see that you've changed a column to numeric in your train set, but it doesn't look like you did it to your test set. Try that first and see if that helps.</p>",
      "rawMarkdown": "I see that you've changed a column to numeric in your train set, but it doesn't look like you did it to your test set. Try that first and see if that helps.",
      "votes": null
    },
    {
      "id": "125727",
      "postDate": "07/01/2016 22:42:51",
      "content": "<p>Thanks much Branden.  That allowed me to move through.  Doesn't seem to matter which column I select as long as there is one.  </p>",
      "rawMarkdown": "Thanks much Branden.  That allowed me to move through.  Doesn't seem to matter which column I select as long as there is one.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 125702,
      "author_name": "brandenkmurray",
      "author_url": "",
      "post_date": "07/01/2016 17:14:08",
      "content": "<p>I see that you've changed a column to numeric in your train set, but it doesn't look like you did it to your test set. Try that first and see if that helps.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 125727,
      "author_name": "curuos",
      "author_url": "",
      "post_date": "07/01/2016 22:42:51",
      "content": "<p>Thanks much Branden.  That allowed me to move through.  Doesn't seem to matter which column I select as long as there is one.  </p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "125701": "I am new to using XGBoost and trying to use a regression objective  (then want to do poisson if I can ever get it working).  \r\n\r\nI've attempted to use the following code and at the point of PREDICT I get the error:\r\n**Error in xgb.DMatrix(newdata) : \r\n  REAL() can only be applied to a 'numeric', not a 'integer'**\r\n\r\nI've tried using lapply to the matrix, I've tried setting a variable to numeric, the training seems to move through, it's the prediction step that bombs.  \r\n\r\n\r\n\r\n\r\ntrain$id <- NULL;\r\ntest$id <- NULL;\r\n\r\ncat(\"Treating features...\\n\");\r\n## impute train and test here\r\n#one hot encoding\r\nsparse_matrix <- sparse.model.matrix(Demanda_uni_equil ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = train)\r\nsparse_matrix <- sparse.model.matrix( ~ Agencia_ID+Canal_ID +Ruta_SAK +Producto_ID +Cliente_ID -1, data = test)\r\ntrain$Cliente_ID<-as.numeric(train$Cliente_ID)\r\ntrain.y = train$Demanda_uni_equil;\r\n\r\ncat(\"Dropping features...\\n\");\r\ntrain$Demanda_uni_equil <- NULL;\r\n\r\n\r\ncat(\"Creating data.matrix...\\n\");\r\npreds <- rep(0,nrow(test));\r\ntrainM<-data.matrix(train, rownames.force = NA);\r\ncat(\"Creating DMatrix for xgboost...\\n\");\r\ndtrain <- xgb.DMatrix(data=trainM, label=train.y, missing = NaN);\r\nwatchlist <- list(trainM=dtrain);\r\n\r\nset.seed(1969);\r\n\r\nparam <- list(  objective           = \"reg:linear\", \r\n                booster             = \"gblinear\",\r\n                #eval_metric         =\"mae\",\r\n                eta                 = 0.035,\r\n                max_depth           = 6,\r\n                subsample           = 1.00,\r\n                colsample_bytree    = 0.40\r\n)\r\n\r\nclf <- xgb.cv(  params              = param, \r\n                data                = dtrain, \r\n                nrounds             = 200, \r\n                verbose             = 1,\r\n                watchlist           = watchlist,\r\n                maximize            = FALSE,\r\n                nfold               = 3,\r\n                early.stop.round    = 10,\r\n                print.every.n       = 1\r\n);\r\n\r\nbestRound <- which.min( as.matrix(clf)[,3] );\r\ncat(\"Best round:\", bestRound,\"\\n\");\r\ncat(\"Best result:\",min(as.matrix(clf)[,3]),\"\\n\");\r\n\r\nclf <- xgb.train(   params              = param, \r\n                    data                = dtrain, \r\n                    nrounds             = bestRound, \r\n                    verbose             = 1,\r\n                    watchlist           = watchlist,\r\n                    maximize            = FALSE\r\n)\r\ntestM <-data.matrix(test, rownames.force = NA);\r\n\r\n**preds <- predict(clf, testM);**",
    "125702": "I see that you've changed a column to numeric in your train set, but it doesn't look like you did it to your test set. Try that first and see if that helps.",
    "125727": "Thanks much Branden.  That allowed me to move through.  Doesn't seem to matter which column I select as long as there is one."
  },
  "source": "meta"
}