{
  "id": 11270,
  "title": "Is the FeatureHasher function available in R?",
  "url": "/competitions/avazu-ctr-prediction/discussion/11270",
  "author_name": "",
  "post_date": "2014-12-16T17:57:14.657Z",
  "votes": 1,
  "comment_count": 19,
  "views": 7631,
  "content": "<p>Hi folks,</p>\n<p>Is any of you know if there exist something similar (package) to the <strong>FeatureHasher</strong>&nbsp;function from <strong>sci-kit learn</strong> in <strong>R</strong>? I implemented one following the algo in Wikipedia, but I'm not pleased with the result.&nbsp;</p>\n<p>Thanks,</p>",
  "messages": [
    {
      "id": "60380",
      "postDate": "12/16/2014 17:57:14",
      "content": "<p>Hi folks,</p>\n<p>Is any of you know if there exist something similar (package) to the <strong>FeatureHasher</strong>&nbsp;function from <strong>sci-kit learn</strong> in <strong>R</strong>? I implemented one following the algo in Wikipedia, but I'm not pleased with the result.&nbsp;</p>\n<p>Thanks,</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "60384",
      "postDate": "12/16/2014 19:17:43",
      "content": "<p>I guess No. The closest to hash that I could get is the hash library that gives us a dictionary data structure. But, feature hashing is a No in R. If there is one, please let us know. Happy to learn.</p>\n<p>EDIT: There's a hash generator in the digest package. But I'm afraid this doesn't answer your question though.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "60447",
      "postDate": "12/17/2014 08:57:49",
      "content": "<p>you can use digest package.... for example</p>\n\n<p>library(digest)</p>\n<p>D &lt;- 2^20</p>\n<p>#feature hashing<br>hash &lt;- function(s) {</p>\n<p>&nbsp; &nbsp; hstr &lt;- digest(paste0(s[1], '_', s[2]), algo='xxhash32')<br>&nbsp; &nbsp; as.numeric(paste0('0x', hstr)) %% D<br>}</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "60510",
      "postDate": "12/17/2014 19:19:39",
      "content": "<p>Hi Xin,</p>\n<p>In your formula, what is the variable s? How do you make sure there's a match in the number of rows in the hashed test data set and the ID's number? I actually implemented something more elegant than that, but the rows in my test set ended being way more than the original test data. And the problem was how do I match the test ID variables to the new hashed test data set.</p>\n<p>Thank you,</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "61754",
      "postDate": "12/23/2014 14:23:18",
      "content": "<p>Have you considered encoding without the hash trick in R? &nbsp;You can one-hot encode all of the features into a sparse matrix automatically using the Matrix package or model.matrix.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "61759",
      "postDate": "12/23/2014 16:54:42",
      "content": "<p><a href=\"http://cran.at.r-project.org/web/packages/FeatureHashing/index.html\">http://cran.at.r-project.org/web/packages/FeatureHashing/index.html</a></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "61770",
      "postDate": "12/23/2014 19:19:23",
      "content": "<p>Thanks a lot piotrek. That looks like what I was looking for. I'll mess around with it and see how it compares to the python implementation.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "61881",
      "postDate": "12/25/2014 23:18:01",
      "content": "<p>for what is feature hashing good for - just compression or is calc also faster?</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62664",
      "postDate": "01/12/2015 18:37:12",
      "content": "<p>@Piotrek - Did you figure out how to use FeatureHashing package on test data for submission? I mean after you hashed the training data and you've built the model&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62934",
      "postDate": "01/20/2015 19:12:03",
      "content": "<p>[quote=Filip Floegel;61881]</p>\n<p>for what is feature hashing good for - just compression or is calc also faster?</p>\n<p>[/quote]</p>\n<p>This post is a good introduction to some of the reasons for feature hashing:&nbsp;http://webcache.googleusercontent.com/search?q=cache:PRrGZpsAUJAJ:metaoptimize.com/qa/questions/6943/what-is-the-hashing-trick+&amp;cd=2&amp;hl=en&amp;ct=clnk&amp;gl=us</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63049",
      "postDate": "01/23/2015 09:18:33",
      "content": "<p>Dear all,</p>\n<p>I am the author of FeatureHashing .</p>\n<p>Here is a&nbsp;<a href=\"https://gist.github.com/wush978/1d8c18b57b6844dd0084\">demo code</a> to train the logistic regression with&nbsp;<a href=\"http://cran.r-project.org/web/packages/glmnet/index.html\">glmnet</a> and gradient boosted decision tree with&nbsp;<a href=\"http://cran.r-project.org/web/packages/xgboost/index.html\">gxboost</a> on the <a href=\"http://data.computational-advertising.org/\">ipinyou dataset</a></p>\n<p>Hope that helps.These example will be included in the documentation of the next patch.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63118",
      "postDate": "01/24/2015 22:18:07",
      "content": "<p>[quote=Wush Wu;63049]</p>\n<p>Dear all,</p>\n<p>I am the author of FeatureHashing .</p>\n<p>Here is a&nbsp;<a href=\"https://gist.github.com/anonymous/a54b0a30f467aa31f90a\">demo code</a> to train the logistic regression with&nbsp;<a href=\"http://cran.r-project.org/web/packages/glmnet/index.html\">glmnet</a> and gradient boosted decision tree with&nbsp;<a href=\"http://cran.r-project.org/web/packages/xgboost/index.html\">gxboost</a> on the <a href=\"http://data.computational-advertising.org/\">ipinyou dataset</a></p>\n<p>Hope that helps.These example will be included in the documentation of the next patch.</p>\n<p>[/quote]</p>\n\n<p>There seems to be some formatting issue in the sample code. I'm getting following errors when trying it out. Can you please into it? Let me know if I'm missing something obvious here.</p>\n\n<p>Thanks.</p>\n<p><code></code><code>&gt; suppressPackageStartupMessages({<br>+ library(FeatureHashing)<br>+ library(dplyr)<br>+ })<br>&gt; <br>&gt; f &lt;- ~=&quot;&quot; ip=&quot;&quot; +=&quot;&quot; region=&quot;&quot; +=&quot;&quot; city=&quot;&quot; +=&quot;&quot; adexchange=&quot;&quot; +=&quot;&quot; domain=&quot;&quot;&gt;<br>Error: unexpected '=' in &quot;f &lt;- ~=&quot;<br>&gt; URL + AdSlotId + AdSlotWidth + AdSlotHeight +<br>+ AdSlotVisibility + AdSlotFormat + CreativeID +<br>+ weekday + hour + tag(usertag, split = &quot;,&quot;)<br>Error: object 'URL' not found<br>&gt; m.train &lt;- hashed.model.matrix(f,=&quot;&quot; imp.train,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.train &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default<br>&gt; m.test &lt;- hashed.model.matrix(f,=&quot;&quot; imp.test,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.test &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default</code></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63119",
      "postDate": "01/24/2015 23:48:02",
      "content": "<p>A few additional questions. Please pardon my ignorance as I'm a novice.</p>\n<p>1) It seems that the feature hasher by default returns a transposed matrix. Shouldn't it be transposed again before training so that the rows represent the samples and the columns the new features.</p>\n<p>2) From the sample I'm trying to understand how the features are hashed and mapped.</p>\n<p><code>&gt; m &lt;- hashed.model.matrix(~ ., CO2, 2^6, keep.hashing_mapping = TRUE)<br>&gt; mapping &lt;- as.list(attr(m, &quot;mapping&quot;))<br></code><code>&gt; mapping &lt;- unlist(as.list(attr(m, &quot;mapping&quot;)))<br>&gt; <br>&gt; mapping %% 2^6<br> PlantQn1 PlantQn2 PlantQn3 uptake TypeMississippi Treatmentchilled PlantMn1 PlantMn2 PlantMn3 <br> 33 37 6 48 16 18 22 1 20 <br> PlantQc1 PlantQc2 PlantQc3 Treatmentnonchilled PlantMc1 PlantMc2 PlantMc3 conc TypeQuebec <br> 5 26 45 31 54 41 41 30 6 <br></code></p>\n<p>From the above my understanding is that value of Qn1 for Plant will be 33, Qn2 be 37, etc. This implies that in the one hot encoded vector of 64 features, the 33rd and 37th values will be set to 1.</p>\n<p><code>Let's look at the CO2 data.</code><code></code><code>&gt; head(CO2)<br>Grouped Data: uptake ~ conc | Plant<br> Plant Type Treatment conc uptake<br>1 Qn1 Quebec nonchilled 95 16.0<br>2 Qn1 Quebec nonchilled 175 30.4<br>3 Qn1 Quebec nonchilled 250 34.8<br>4 Qn1 Quebec nonchilled 350 37.2<br>5 Qn1 Quebec nonchilled 500 35.3<br>6 Qn1 Quebec nonchilled 675 39.2<br></code></p>\n<p>Comparing the above with the first column of the feature hash, I don't see the 33rd element set as 1 (this is what QN1 above was mapped to).</p>\n<p><code>&gt; dgm = as(m, &quot;dgCMatrix&quot;)<br></code></p>\n<p><code>&gt; dgm[,1]<br> [1] 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 95 -1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br>[49] -16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br></code></p>\n<p>And lastly why are there elements like -16 and 95.</p>\n<p>It's possible that I'm not just understanding how feature hashing works. So please pardon my ignorance and the questions asked.</p>\n<p>Thanks.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63127",
      "postDate": "01/25/2015 05:23:29",
      "content": "<p>Thanks for your asking.</p>\n\n<p>I correct the link above. But please do not use them directly. Replace the formula and the variable name of the data.frame to adapt to your dataset.</p>\n\n<p>Your question indicates some incomplete explanation of the document.</p>\n<p>[quote=Hitesh Sharma;63119]</p>\n<p>A few additional questions. Please pardon my ignorance as I'm a novice.</p>\n<p>1) It seems that the feature hasher by default returns a transposed matrix. Shouldn't it be transposed again before training so that the rows represent the samples and the columns the new features.</p>\n\n<p>There is a parameter `transpose` to decide whether to transpose the returned matrix or not. The default value is `TRUE` and I am considering whether to change it in the next patch or not.</p>\n\n<p>2) From the sample I'm trying to understand how the features are hashed and mapped.</p>\n<p><code>&gt; m &lt;- hashed.model.matrix(~ ., CO2, 2^6, keep.hashing_mapping = TRUE)<br>&gt; mapping &lt;- as.list(attr(m, &quot;mapping&quot;))<br></code><code>&gt; mapping &lt;- unlist(as.list(attr(m, &quot;mapping&quot;)))<br>&gt; <br>&gt; mapping %% 2^6<br> PlantQn1 PlantQn2 PlantQn3 uptake TypeMississippi Treatmentchilled PlantMn1 PlantMn2 PlantMn3 <br> 33 37 6 48 16 18 22 1 20 <br> PlantQc1 PlantQc2 PlantQc3 Treatmentnonchilled PlantMc1 PlantMc2 PlantMc3 conc TypeQuebec <br> 5 26 45 31 54 41 41 30 6 <br></code></p>\n<p>From the above my understanding is that value of Qn1 for Plant will be 33, Qn2 be 37, etc. This implies that in the one hot encoded vector of 64 features, the 33rd and 37th values will be set to 1.</p>\n<p><code>Let's look at the CO2 data.</code><code></code><code>&gt; head(CO2)<br>Grouped Data: uptake ~ conc | Plant<br> Plant Type Treatment conc uptake<br>1 Qn1 Quebec nonchilled 95 16.0<br>2 Qn1 Quebec nonchilled 175 30.4<br>3 Qn1 Quebec nonchilled 250 34.8<br>4 Qn1 Quebec nonchilled 350 37.2<br>5 Qn1 Quebec nonchilled 500 35.3<br>6 Qn1 Quebec nonchilled 675 39.2<br></code></p>\n<p>Comparing the above with the first column of the feature hash, I don't see the 33rd element set as 1 (this is what QN1 above was mapped to).</p>\n<p><code>&gt; dgm = as(m, &quot;dgCMatrix&quot;)<br></code></p>\n<p><code>&gt; dgm[,1]<br> [1] 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 95 -1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br>[49] -16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br></code></p>\n<p>And lastly why are there elements like -16 and 95.</p>\n<p>It's possible that I'm not just understanding how feature hashing works. So please pardon my ignorance and the questions asked.</p>\n<p>Thanks.</p>\n<p>[/quote]</p>\n\n<p>At first, I need to address that the implementation uses a binary hashed function to correct the sign of the result. Therefore,&nbsp;there are 1 and -1 in the returned matrix. You can read the second formula in here:&nbsp;<a href=\"http://en.wikipedia.org/wiki/Feature_hashing#Feature_vectorization_using_the_hashing_trick\">feature vectorization using the hashing trick</a></p>\n<p>Second,&nbsp;the range of `%% 2^6` is 0 ~ 2^6 - 1, so we need to shift the index by 1 Because the starting index in R is 1</p>\n<p>The first row of CO2 is:</p>\n<p>Plant Type Treatment conc uptake<br>1 Qn1 Quebec nonchilled 95 16</p>\n<p>The mapping of them are:</p>\n<p>&gt; mapping[&quot;Treatmentnonchilled&quot;] %% 2^6 + 1<br>Treatmentnonchilled<br> 32<br>&gt; mapping[&quot;TypeQuebec&quot;] %% 2^6 + 1<br>TypeQuebec<br> 7<br>&gt; mapping[&quot;PlantQn1&quot;] %% 2^6 + 1<br>PlantQn1<br> 34<br>&gt; mapping[&quot;conc&quot;] %% 2^6 + 1<br>conc<br> 31<br>&gt; mapping[&quot;uptake&quot;] %% 2^6 + 1<br>uptake<br> 49</p>\n\n<p>Therefore,&nbsp;</p>\n<p>&gt; dgm[c(7,31,32,34,49),1]<br>&lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;<br> 1 95 -1 -1 -16</p>\n<p>Note that the variable conc and uptake are numeric, so the value is 95 and -16.</p>\n<p>Hope that helps.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63169",
      "postDate": "01/26/2015 02:53:33",
      "content": "<p>Hi Wush,</p>\n<p>Thanks for the explanation and response. I tried to use the FeatureHasher with glmnet on the CO2 data but getting this error:</p>\n<p><code>&gt; <br>&gt; co = CO2<br>&gt; <br>&gt; co$Treatment = NULL<br>&gt; co = hashed.model.matrix(~ ., co, 2^6, keep.hashing_mapping = TRUE)<br>&gt; co = as(co, &quot;dgCMatrix&quot;)<br>&gt; co = t(co)<br>&gt; <br>&gt; <br>&gt; glmnetmod &lt;- cv.glmnet(y = as.factor(CO2$Treatment), x = co, family=&quot;binomial&quot;, nfolds=3, type.measure=&quot;auc&quot;)<br>&gt; result = predict(glmnetmod, co, s = &quot;lambda.min&quot;)<br>Error in `dimnames&lt;-`(`*tmp*`, value = list(&quot;(Intercept)&quot;, &quot;1&quot;)) : <br>&nbsp; invalid dimnames given for &#8220;dgCMatrix&#8221; object<br>&gt; <br></code></p>\n<p>I'm not sure why it's happening but have a hunch that it might be something to do with the transpose that I'm taking. I noted that in your sample glmnet snippet you are not taking a transpose. Is the input data in that sample already transposed?</p>\n<p>Appreciate the pointers in getting past this error. I have tried to search a lot on the web but had no luck.</p>\n<p>Thanks.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63173",
      "postDate": "01/26/2015 04:42:50",
      "content": "<p>[quote=Hitesh Sharma;63169]</p>\n<p>Hi Wush,</p>\n<p>Thanks for the explanation and response. I tried to use the FeatureHasher with glmnet on the CO2 data but getting this error:</p>\n<p><code>&gt; <br>&gt; co = CO2<br>&gt; <br>&gt; co$Treatment = NULL<br>&gt; co = hashed.model.matrix(~ ., co, 2^6, keep.hashing_mapping = TRUE)<br>&gt; co = as(co, &quot;dgCMatrix&quot;)<br>&gt; co = t(co)<br>&gt; <br>&gt; <br>&gt; glmnetmod &lt;- cv.glmnet(y = as.factor(CO2$Treatment), x = co, family=&quot;binomial&quot;, nfolds=3, type.measure=&quot;auc&quot;)<br>&gt; result = predict(glmnetmod, co, s = &quot;lambda.min&quot;)<br>Error in `dimnames&lt;-`(`*tmp*`, value = list(&quot;(Intercept)&quot;, &quot;1&quot;)) : <br>&nbsp; invalid dimnames given for &#8220;dgCMatrix&#8221; object<br>&gt; <br></code></p>\n<p>I'm not sure why it's happening but have a hunch that it might be something to do with the transpose that I'm taking. I noted that in your sample glmnet snippet you are not taking a transpose. Is the input data in that sample already transposed?</p>\n<p>Appreciate the pointers in getting past this error. I have tried to search a lot on the web but had no luck.</p>\n<p>Thanks.</p>\n<p>[/quote]</p>\n<p>As suspected something was going wrong when a transpose was being done. I set transpose = FALSE in FeatureHasher and everything worked. Thanks for creating this package!</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63216",
      "postDate": "01/26/2015 22:05:06",
      "content": "<p>[quote=jwjohnson314;62934]</p>\n<p>[quote=Filip Floegel;61881]</p>\n<p>for what is feature hashing good for - just compression or is calc also faster?</p>\n<p>[/quote]</p>\n<p>This post is a good introduction to some of the reasons for feature hashing:&nbsp;http://webcache.googleusercontent.com/search?q=cache:PRrGZpsAUJAJ:metaoptimize.com/qa/questions/6943/what-is-the-hashing-trick+&amp;cd=2&amp;hl=en&amp;ct=clnk&amp;gl=us</p>\n<p>[/quote]</p>\n\n<p>oh :-) thank you very much for the web link</p>\n\n<p>multitask + interaction are very good reasonsa apart from online learning + feature space</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63385",
      "postDate": "01/31/2015 11:03:20",
      "content": "<p>[quote=Hitesh Sharma;63118]</p>\n<p>[quote=Wush Wu;63049]</p>\n<p>Dear all,</p>\n<p>I am the author of FeatureHashing .</p>\n<p>Here is a&nbsp;<a href=\"https://gist.github.com/anonymous/a54b0a30f467aa31f90a\">demo code</a> to train the logistic regression with&nbsp;<a href=\"http://cran.r-project.org/web/packages/glmnet/index.html\">glmnet</a> and gradient boosted decision tree with&nbsp;<a href=\"http://cran.r-project.org/web/packages/xgboost/index.html\">gxboost</a> on the <a href=\"http://data.computational-advertising.org/\">ipinyou dataset</a></p>\n<p>Hope that helps.These example will be included in the documentation of the next patch.</p>\n<p>[/quote]</p>\n<p>There seems to be some formatting issue in the sample code. I'm getting following errors when trying it out. Can you please into it? Let me know if I'm missing something obvious here.</p>\n<p>Thanks.</p>\n<p><code></code><code>&gt; suppressPackageStartupMessages({<br>+ library(FeatureHashing)<br>+ library(dplyr)<br>+ })<br>&gt; <br>&gt; f &lt;- ~=&quot;&quot; ip=&quot;&quot; +=&quot;&quot; region=&quot;&quot; +=&quot;&quot; city=&quot;&quot; +=&quot;&quot; adexchange=&quot;&quot; +=&quot;&quot; domain=&quot;&quot;&gt;<br>Error: unexpected '=' in &quot;f &lt;- ~=&quot;<br>&gt; URL + AdSlotId + AdSlotWidth + AdSlotHeight +<br>+ AdSlotVisibility + AdSlotFormat + CreativeID +<br>+ weekday + hour + tag(usertag, split = &quot;,&quot;)<br>Error: object 'URL' not found<br>&gt; m.train &lt;- hashed.model.matrix(f,=&quot;&quot; imp.train,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.train &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default<br>&gt; m.test &lt;- hashed.model.matrix(f,=&quot;&quot; imp.test,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.test &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default</code></p>\n<p>[/quote]</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63387",
      "postDate": "01/31/2015 11:04:28",
      "content": "<p>in my case it doesnt work out too with those formating problems</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "63543",
      "postDate": "02/04/2015 03:01:57",
      "content": "<p>You could use sparse.model.matrix() in R. I'm not sure if it's worse/beter than the hash trick, but it works. Here is an example script.</p>\n<p><code>rm(list =ls(all=T)); gc()</code></p>\n<p><code>library(FeatureHashing)<br>library(dplyr)<br>library(data.table)<br>library(Matrix)<br>library(glmnet)<br>library(xgboost)</code></p>\n<p><code>test &lt;- fread(&quot;test.txt&quot;, colClasses='character', header=T)<br>id &lt;- test$id<br>test &lt;- sparse.model.matrix(object= ~ ., data=test)<br>test$id &lt;- NULL</code></p>\n\n<p><code>LogLoss&lt;-function(actual, predicted){<br> predicted&lt;-(pmax(predicted, 0.00001))<br> predicted&lt;-(pmin(predicted, 0.99999))<br> result&lt;- -1/length(actual)*(sum((actual*log(predicted)+(1-actual)*log(1-predicted))))<br> return(result)</code></p>\n<p><code>}</code></p>\n<p><code>set.seed(123)</code></p>\n\n<p><code>train = fread(&quot;train.txt&quot;, nrows=1e6)<br>y = train[['click']]</code><code>gc()</code><code>x_all &lt;- sparse.model.matrix(object=click ~ ., data=train[, -1, with=F])<br>inx &lt;- sample(1e6, 1e6 * 0.7)<br>x_train &lt;- x_all[inx, ]<br>x_test &lt;- x_all[-inx, ]</code></p>\n<p><code>rm(train); gc()</code></p>\n\n<p># Parameters</p>\n<p><code>max.depth = 10<br>gamma = 1.2<br>eta = 0.8<br>nround = 10<br>min_child_weight = 3</code></p>\n\n<p><code>bst &lt;- xgboost(data = x_train, label = y[inx], max.depth=max.depth, gamma = gamma,<br> eta = eta, nround = nround, min_child_weight = min_child_weight,<br> objective = &quot;binary:logistic&quot;, booster = 'gbtree')<br>pred &lt;- predict(bst, x_test)<br>print(LogLoss(y[-inx], pred))</code></p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 60384,
      "author_name": "phanisrikanth",
      "author_url": "",
      "post_date": "12/16/2014 19:17:43",
      "content": "<p>I guess No. The closest to hash that I could get is the hash library that gives us a dictionary data structure. But, feature hashing is a No in R. If there is one, please let us know. Happy to learn.</p>\n<p>EDIT: There's a hash generator in the digest package. But I'm afraid this doesn't answer your question though.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 60447,
      "author_name": "xinliu",
      "author_url": "",
      "post_date": "12/17/2014 08:57:49",
      "content": "<p>you can use digest package.... for example</p>\n\n<p>library(digest)</p>\n<p>D &lt;- 2^20</p>\n<p>#feature hashing<br>hash &lt;- function(s) {</p>\n<p>&nbsp; &nbsp; hstr &lt;- digest(paste0(s[1], '_', s[2]), algo='xxhash32')<br>&nbsp; &nbsp; as.numeric(paste0('0x', hstr)) %% D<br>}</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 60510,
      "author_name": "maxsop",
      "author_url": "",
      "post_date": "12/17/2014 19:19:39",
      "content": "<p>Hi Xin,</p>\n<p>In your formula, what is the variable s? How do you make sure there's a match in the number of rows in the hashed test data set and the ID's number? I actually implemented something more elegant than that, but the rows in my test set ended being way more than the original test data. And the problem was how do I match the test ID variables to the new hashed test data set.</p>\n<p>Thank you,</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 61754,
      "author_name": "cvarrichio",
      "author_url": "",
      "post_date": "12/23/2014 14:23:18",
      "content": "<p>Have you considered encoding without the hash trick in R? &nbsp;You can one-hot encode all of the features into a sparse matrix automatically using the Matrix package or model.matrix.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 61759,
      "author_name": "piotrw",
      "author_url": "",
      "post_date": "12/23/2014 16:54:42",
      "content": "<p><a href=\"http://cran.at.r-project.org/web/packages/FeatureHashing/index.html\">http://cran.at.r-project.org/web/packages/FeatureHashing/index.html</a></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 61770,
      "author_name": "maxsop",
      "author_url": "",
      "post_date": "12/23/2014 19:19:23",
      "content": "<p>Thanks a lot piotrek. That looks like what I was looking for. I'll mess around with it and see how it compares to the python implementation.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 61881,
      "author_name": "filipus",
      "author_url": "",
      "post_date": "12/25/2014 23:18:01",
      "content": "<p>for what is feature hashing good for - just compression or is calc also faster?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62664,
      "author_name": "maxsop",
      "author_url": "",
      "post_date": "01/12/2015 18:37:12",
      "content": "<p>@Piotrek - Did you figure out how to use FeatureHashing package on test data for submission? I mean after you hashed the training data and you've built the model&nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62934,
      "author_name": "jwjohnson314",
      "author_url": "",
      "post_date": "01/20/2015 19:12:03",
      "content": "<p>[quote=Filip Floegel;61881]</p>\n<p>for what is feature hashing good for - just compression or is calc also faster?</p>\n<p>[/quote]</p>\n<p>This post is a good introduction to some of the reasons for feature hashing:&nbsp;http://webcache.googleusercontent.com/search?q=cache:PRrGZpsAUJAJ:metaoptimize.com/qa/questions/6943/what-is-the-hashing-trick+&amp;cd=2&amp;hl=en&amp;ct=clnk&amp;gl=us</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63049,
      "author_name": "wush978",
      "author_url": "",
      "post_date": "01/23/2015 09:18:33",
      "content": "<p>Dear all,</p>\n<p>I am the author of FeatureHashing .</p>\n<p>Here is a&nbsp;<a href=\"https://gist.github.com/wush978/1d8c18b57b6844dd0084\">demo code</a> to train the logistic regression with&nbsp;<a href=\"http://cran.r-project.org/web/packages/glmnet/index.html\">glmnet</a> and gradient boosted decision tree with&nbsp;<a href=\"http://cran.r-project.org/web/packages/xgboost/index.html\">gxboost</a> on the <a href=\"http://data.computational-advertising.org/\">ipinyou dataset</a></p>\n<p>Hope that helps.These example will be included in the documentation of the next patch.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63118,
      "author_name": "hitesh",
      "author_url": "",
      "post_date": "01/24/2015 22:18:07",
      "content": "<p>[quote=Wush Wu;63049]</p>\n<p>Dear all,</p>\n<p>I am the author of FeatureHashing .</p>\n<p>Here is a&nbsp;<a href=\"https://gist.github.com/anonymous/a54b0a30f467aa31f90a\">demo code</a> to train the logistic regression with&nbsp;<a href=\"http://cran.r-project.org/web/packages/glmnet/index.html\">glmnet</a> and gradient boosted decision tree with&nbsp;<a href=\"http://cran.r-project.org/web/packages/xgboost/index.html\">gxboost</a> on the <a href=\"http://data.computational-advertising.org/\">ipinyou dataset</a></p>\n<p>Hope that helps.These example will be included in the documentation of the next patch.</p>\n<p>[/quote]</p>\n\n<p>There seems to be some formatting issue in the sample code. I'm getting following errors when trying it out. Can you please into it? Let me know if I'm missing something obvious here.</p>\n\n<p>Thanks.</p>\n<p><code></code><code>&gt; suppressPackageStartupMessages({<br>+ library(FeatureHashing)<br>+ library(dplyr)<br>+ })<br>&gt; <br>&gt; f &lt;- ~=&quot;&quot; ip=&quot;&quot; +=&quot;&quot; region=&quot;&quot; +=&quot;&quot; city=&quot;&quot; +=&quot;&quot; adexchange=&quot;&quot; +=&quot;&quot; domain=&quot;&quot;&gt;<br>Error: unexpected '=' in &quot;f &lt;- ~=&quot;<br>&gt; URL + AdSlotId + AdSlotWidth + AdSlotHeight +<br>+ AdSlotVisibility + AdSlotFormat + CreativeID +<br>+ weekday + hour + tag(usertag, split = &quot;,&quot;)<br>Error: object 'URL' not found<br>&gt; m.train &lt;- hashed.model.matrix(f,=&quot;&quot; imp.train,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.train &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default<br>&gt; m.test &lt;- hashed.model.matrix(f,=&quot;&quot; imp.test,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.test &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default</code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63119,
      "author_name": "hitesh",
      "author_url": "",
      "post_date": "01/24/2015 23:48:02",
      "content": "<p>A few additional questions. Please pardon my ignorance as I'm a novice.</p>\n<p>1) It seems that the feature hasher by default returns a transposed matrix. Shouldn't it be transposed again before training so that the rows represent the samples and the columns the new features.</p>\n<p>2) From the sample I'm trying to understand how the features are hashed and mapped.</p>\n<p><code>&gt; m &lt;- hashed.model.matrix(~ ., CO2, 2^6, keep.hashing_mapping = TRUE)<br>&gt; mapping &lt;- as.list(attr(m, &quot;mapping&quot;))<br></code><code>&gt; mapping &lt;- unlist(as.list(attr(m, &quot;mapping&quot;)))<br>&gt; <br>&gt; mapping %% 2^6<br> PlantQn1 PlantQn2 PlantQn3 uptake TypeMississippi Treatmentchilled PlantMn1 PlantMn2 PlantMn3 <br> 33 37 6 48 16 18 22 1 20 <br> PlantQc1 PlantQc2 PlantQc3 Treatmentnonchilled PlantMc1 PlantMc2 PlantMc3 conc TypeQuebec <br> 5 26 45 31 54 41 41 30 6 <br></code></p>\n<p>From the above my understanding is that value of Qn1 for Plant will be 33, Qn2 be 37, etc. This implies that in the one hot encoded vector of 64 features, the 33rd and 37th values will be set to 1.</p>\n<p><code>Let's look at the CO2 data.</code><code></code><code>&gt; head(CO2)<br>Grouped Data: uptake ~ conc | Plant<br> Plant Type Treatment conc uptake<br>1 Qn1 Quebec nonchilled 95 16.0<br>2 Qn1 Quebec nonchilled 175 30.4<br>3 Qn1 Quebec nonchilled 250 34.8<br>4 Qn1 Quebec nonchilled 350 37.2<br>5 Qn1 Quebec nonchilled 500 35.3<br>6 Qn1 Quebec nonchilled 675 39.2<br></code></p>\n<p>Comparing the above with the first column of the feature hash, I don't see the 33rd element set as 1 (this is what QN1 above was mapped to).</p>\n<p><code>&gt; dgm = as(m, &quot;dgCMatrix&quot;)<br></code></p>\n<p><code>&gt; dgm[,1]<br> [1] 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 95 -1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br>[49] -16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br></code></p>\n<p>And lastly why are there elements like -16 and 95.</p>\n<p>It's possible that I'm not just understanding how feature hashing works. So please pardon my ignorance and the questions asked.</p>\n<p>Thanks.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63127,
      "author_name": "wush978",
      "author_url": "",
      "post_date": "01/25/2015 05:23:29",
      "content": "<p>Thanks for your asking.</p>\n\n<p>I correct the link above. But please do not use them directly. Replace the formula and the variable name of the data.frame to adapt to your dataset.</p>\n\n<p>Your question indicates some incomplete explanation of the document.</p>\n<p>[quote=Hitesh Sharma;63119]</p>\n<p>A few additional questions. Please pardon my ignorance as I'm a novice.</p>\n<p>1) It seems that the feature hasher by default returns a transposed matrix. Shouldn't it be transposed again before training so that the rows represent the samples and the columns the new features.</p>\n\n<p>There is a parameter `transpose` to decide whether to transpose the returned matrix or not. The default value is `TRUE` and I am considering whether to change it in the next patch or not.</p>\n\n<p>2) From the sample I'm trying to understand how the features are hashed and mapped.</p>\n<p><code>&gt; m &lt;- hashed.model.matrix(~ ., CO2, 2^6, keep.hashing_mapping = TRUE)<br>&gt; mapping &lt;- as.list(attr(m, &quot;mapping&quot;))<br></code><code>&gt; mapping &lt;- unlist(as.list(attr(m, &quot;mapping&quot;)))<br>&gt; <br>&gt; mapping %% 2^6<br> PlantQn1 PlantQn2 PlantQn3 uptake TypeMississippi Treatmentchilled PlantMn1 PlantMn2 PlantMn3 <br> 33 37 6 48 16 18 22 1 20 <br> PlantQc1 PlantQc2 PlantQc3 Treatmentnonchilled PlantMc1 PlantMc2 PlantMc3 conc TypeQuebec <br> 5 26 45 31 54 41 41 30 6 <br></code></p>\n<p>From the above my understanding is that value of Qn1 for Plant will be 33, Qn2 be 37, etc. This implies that in the one hot encoded vector of 64 features, the 33rd and 37th values will be set to 1.</p>\n<p><code>Let's look at the CO2 data.</code><code></code><code>&gt; head(CO2)<br>Grouped Data: uptake ~ conc | Plant<br> Plant Type Treatment conc uptake<br>1 Qn1 Quebec nonchilled 95 16.0<br>2 Qn1 Quebec nonchilled 175 30.4<br>3 Qn1 Quebec nonchilled 250 34.8<br>4 Qn1 Quebec nonchilled 350 37.2<br>5 Qn1 Quebec nonchilled 500 35.3<br>6 Qn1 Quebec nonchilled 675 39.2<br></code></p>\n<p>Comparing the above with the first column of the feature hash, I don't see the 33rd element set as 1 (this is what QN1 above was mapped to).</p>\n<p><code>&gt; dgm = as(m, &quot;dgCMatrix&quot;)<br></code></p>\n<p><code>&gt; dgm[,1]<br> [1] 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 95 -1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br>[49] -16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0<br></code></p>\n<p>And lastly why are there elements like -16 and 95.</p>\n<p>It's possible that I'm not just understanding how feature hashing works. So please pardon my ignorance and the questions asked.</p>\n<p>Thanks.</p>\n<p>[/quote]</p>\n\n<p>At first, I need to address that the implementation uses a binary hashed function to correct the sign of the result. Therefore,&nbsp;there are 1 and -1 in the returned matrix. You can read the second formula in here:&nbsp;<a href=\"http://en.wikipedia.org/wiki/Feature_hashing#Feature_vectorization_using_the_hashing_trick\">feature vectorization using the hashing trick</a></p>\n<p>Second,&nbsp;the range of `%% 2^6` is 0 ~ 2^6 - 1, so we need to shift the index by 1 Because the starting index in R is 1</p>\n<p>The first row of CO2 is:</p>\n<p>Plant Type Treatment conc uptake<br>1 Qn1 Quebec nonchilled 95 16</p>\n<p>The mapping of them are:</p>\n<p>&gt; mapping[&quot;Treatmentnonchilled&quot;] %% 2^6 + 1<br>Treatmentnonchilled<br> 32<br>&gt; mapping[&quot;TypeQuebec&quot;] %% 2^6 + 1<br>TypeQuebec<br> 7<br>&gt; mapping[&quot;PlantQn1&quot;] %% 2^6 + 1<br>PlantQn1<br> 34<br>&gt; mapping[&quot;conc&quot;] %% 2^6 + 1<br>conc<br> 31<br>&gt; mapping[&quot;uptake&quot;] %% 2^6 + 1<br>uptake<br> 49</p>\n\n<p>Therefore,&nbsp;</p>\n<p>&gt; dgm[c(7,31,32,34,49),1]<br>&lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;<br> 1 95 -1 -1 -16</p>\n<p>Note that the variable conc and uptake are numeric, so the value is 95 and -16.</p>\n<p>Hope that helps.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63169,
      "author_name": "hitesh",
      "author_url": "",
      "post_date": "01/26/2015 02:53:33",
      "content": "<p>Hi Wush,</p>\n<p>Thanks for the explanation and response. I tried to use the FeatureHasher with glmnet on the CO2 data but getting this error:</p>\n<p><code>&gt; <br>&gt; co = CO2<br>&gt; <br>&gt; co$Treatment = NULL<br>&gt; co = hashed.model.matrix(~ ., co, 2^6, keep.hashing_mapping = TRUE)<br>&gt; co = as(co, &quot;dgCMatrix&quot;)<br>&gt; co = t(co)<br>&gt; <br>&gt; <br>&gt; glmnetmod &lt;- cv.glmnet(y = as.factor(CO2$Treatment), x = co, family=&quot;binomial&quot;, nfolds=3, type.measure=&quot;auc&quot;)<br>&gt; result = predict(glmnetmod, co, s = &quot;lambda.min&quot;)<br>Error in `dimnames&lt;-`(`*tmp*`, value = list(&quot;(Intercept)&quot;, &quot;1&quot;)) : <br>&nbsp; invalid dimnames given for &#8220;dgCMatrix&#8221; object<br>&gt; <br></code></p>\n<p>I'm not sure why it's happening but have a hunch that it might be something to do with the transpose that I'm taking. I noted that in your sample glmnet snippet you are not taking a transpose. Is the input data in that sample already transposed?</p>\n<p>Appreciate the pointers in getting past this error. I have tried to search a lot on the web but had no luck.</p>\n<p>Thanks.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63173,
      "author_name": "hitesh",
      "author_url": "",
      "post_date": "01/26/2015 04:42:50",
      "content": "<p>[quote=Hitesh Sharma;63169]</p>\n<p>Hi Wush,</p>\n<p>Thanks for the explanation and response. I tried to use the FeatureHasher with glmnet on the CO2 data but getting this error:</p>\n<p><code>&gt; <br>&gt; co = CO2<br>&gt; <br>&gt; co$Treatment = NULL<br>&gt; co = hashed.model.matrix(~ ., co, 2^6, keep.hashing_mapping = TRUE)<br>&gt; co = as(co, &quot;dgCMatrix&quot;)<br>&gt; co = t(co)<br>&gt; <br>&gt; <br>&gt; glmnetmod &lt;- cv.glmnet(y = as.factor(CO2$Treatment), x = co, family=&quot;binomial&quot;, nfolds=3, type.measure=&quot;auc&quot;)<br>&gt; result = predict(glmnetmod, co, s = &quot;lambda.min&quot;)<br>Error in `dimnames&lt;-`(`*tmp*`, value = list(&quot;(Intercept)&quot;, &quot;1&quot;)) : <br>&nbsp; invalid dimnames given for &#8220;dgCMatrix&#8221; object<br>&gt; <br></code></p>\n<p>I'm not sure why it's happening but have a hunch that it might be something to do with the transpose that I'm taking. I noted that in your sample glmnet snippet you are not taking a transpose. Is the input data in that sample already transposed?</p>\n<p>Appreciate the pointers in getting past this error. I have tried to search a lot on the web but had no luck.</p>\n<p>Thanks.</p>\n<p>[/quote]</p>\n<p>As suspected something was going wrong when a transpose was being done. I set transpose = FALSE in FeatureHasher and everything worked. Thanks for creating this package!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63216,
      "author_name": "filipus",
      "author_url": "",
      "post_date": "01/26/2015 22:05:06",
      "content": "<p>[quote=jwjohnson314;62934]</p>\n<p>[quote=Filip Floegel;61881]</p>\n<p>for what is feature hashing good for - just compression or is calc also faster?</p>\n<p>[/quote]</p>\n<p>This post is a good introduction to some of the reasons for feature hashing:&nbsp;http://webcache.googleusercontent.com/search?q=cache:PRrGZpsAUJAJ:metaoptimize.com/qa/questions/6943/what-is-the-hashing-trick+&amp;cd=2&amp;hl=en&amp;ct=clnk&amp;gl=us</p>\n<p>[/quote]</p>\n\n<p>oh :-) thank you very much for the web link</p>\n\n<p>multitask + interaction are very good reasonsa apart from online learning + feature space</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63385,
      "author_name": "filipus",
      "author_url": "",
      "post_date": "01/31/2015 11:03:20",
      "content": "<p>[quote=Hitesh Sharma;63118]</p>\n<p>[quote=Wush Wu;63049]</p>\n<p>Dear all,</p>\n<p>I am the author of FeatureHashing .</p>\n<p>Here is a&nbsp;<a href=\"https://gist.github.com/anonymous/a54b0a30f467aa31f90a\">demo code</a> to train the logistic regression with&nbsp;<a href=\"http://cran.r-project.org/web/packages/glmnet/index.html\">glmnet</a> and gradient boosted decision tree with&nbsp;<a href=\"http://cran.r-project.org/web/packages/xgboost/index.html\">gxboost</a> on the <a href=\"http://data.computational-advertising.org/\">ipinyou dataset</a></p>\n<p>Hope that helps.These example will be included in the documentation of the next patch.</p>\n<p>[/quote]</p>\n<p>There seems to be some formatting issue in the sample code. I'm getting following errors when trying it out. Can you please into it? Let me know if I'm missing something obvious here.</p>\n<p>Thanks.</p>\n<p><code></code><code>&gt; suppressPackageStartupMessages({<br>+ library(FeatureHashing)<br>+ library(dplyr)<br>+ })<br>&gt; <br>&gt; f &lt;- ~=&quot;&quot; ip=&quot;&quot; +=&quot;&quot; region=&quot;&quot; +=&quot;&quot; city=&quot;&quot; +=&quot;&quot; adexchange=&quot;&quot; +=&quot;&quot; domain=&quot;&quot;&gt;<br>Error: unexpected '=' in &quot;f &lt;- ~=&quot;<br>&gt; URL + AdSlotId + AdSlotWidth + AdSlotHeight +<br>+ AdSlotVisibility + AdSlotFormat + CreativeID +<br>+ weekday + hour + tag(usertag, split = &quot;,&quot;)<br>Error: object 'URL' not found<br>&gt; m.train &lt;- hashed.model.matrix(f,=&quot;&quot; imp.train,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.train &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default<br>&gt; m.test &lt;- hashed.model.matrix(f,=&quot;&quot; imp.test,=&quot;&quot; 2^20,=&quot;&quot; false)=&quot;&quot; %=&quot;&quot;&gt;%<br>Error: unexpected '=' in &quot;m.test &lt;- hashed.model.matrix(f,=&quot;<br>&gt; as(&quot;dgCMatrix&quot;)<br>Error in .identC(Class, &quot;double&quot;) : <br> argument &quot;Class&quot; is missing, with no default</code></p>\n<p>[/quote]</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63387,
      "author_name": "filipus",
      "author_url": "",
      "post_date": "01/31/2015 11:04:28",
      "content": "<p>in my case it doesnt work out too with those formating problems</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 63543,
      "author_name": "marbel",
      "author_url": "",
      "post_date": "02/04/2015 03:01:57",
      "content": "<p>You could use sparse.model.matrix() in R. I'm not sure if it's worse/beter than the hash trick, but it works. Here is an example script.</p>\n<p><code>rm(list =ls(all=T)); gc()</code></p>\n<p><code>library(FeatureHashing)<br>library(dplyr)<br>library(data.table)<br>library(Matrix)<br>library(glmnet)<br>library(xgboost)</code></p>\n<p><code>test &lt;- fread(&quot;test.txt&quot;, colClasses='character', header=T)<br>id &lt;- test$id<br>test &lt;- sparse.model.matrix(object= ~ ., data=test)<br>test$id &lt;- NULL</code></p>\n\n<p><code>LogLoss&lt;-function(actual, predicted){<br> predicted&lt;-(pmax(predicted, 0.00001))<br> predicted&lt;-(pmin(predicted, 0.99999))<br> result&lt;- -1/length(actual)*(sum((actual*log(predicted)+(1-actual)*log(1-predicted))))<br> return(result)</code></p>\n<p><code>}</code></p>\n<p><code>set.seed(123)</code></p>\n\n<p><code>train = fread(&quot;train.txt&quot;, nrows=1e6)<br>y = train[['click']]</code><code>gc()</code><code>x_all &lt;- sparse.model.matrix(object=click ~ ., data=train[, -1, with=F])<br>inx &lt;- sample(1e6, 1e6 * 0.7)<br>x_train &lt;- x_all[inx, ]<br>x_test &lt;- x_all[-inx, ]</code></p>\n<p><code>rm(train); gc()</code></p>\n\n<p># Parameters</p>\n<p><code>max.depth = 10<br>gamma = 1.2<br>eta = 0.8<br>nround = 10<br>min_child_weight = 3</code></p>\n\n<p><code>bst &lt;- xgboost(data = x_train, label = y[inx], max.depth=max.depth, gamma = gamma,<br> eta = eta, nround = nround, min_child_weight = min_child_weight,<br> objective = &quot;binary:logistic&quot;, booster = 'gbtree')<br>pred &lt;- predict(bst, x_test)<br>print(LogLoss(y[-inx], pred))</code></p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "60380": "",
    "60384": "",
    "60447": "",
    "60510": "",
    "61754": "",
    "61759": "",
    "61770": "",
    "61881": "",
    "62664": "",
    "62934": "",
    "63049": "",
    "63118": "",
    "63119": "",
    "63127": "",
    "63169": "",
    "63173": "",
    "63216": "",
    "63385": "",
    "63387": "",
    "63543": ""
  },
  "source": "meta"
}