title: Understanding FeatureHasher, collisions and vector size trade-off
body: <p>I'm preprocessing my data before implementing a machine learning model. Some of the features are with high cardinality, like country and language.</p>
  <p>Since encoding those features as one-hot-vector can produce sparse data, I've decided to look into <a href="https://en.wikipedia.org/wiki/Feature_hashing" rel="noreferrer">the hashing trick</a> and used python's category_encoders like so:</p>
  <pre><code>from category_encoders.hashing import HashingEncoder
  ce_hash = HashingEncoder(cols = ['country'])
  encoded = ce_hash.fit_transform(df.country)
  encoded['country'] = df.country
  encoded.head()
  </code></pre>
  <p>When looking at the result, I can see the collisions</p>
  <pre><code>    col_0  col_1  col_2  col_3  col_4  col_5  col_6  col_7 country
  0       0      0      1      0      0      0      0      0      US &lt;━┓
  1       0      1      0      0      0      0      0      0      CA.  ┃ US and SE collides 
  2       0      0      1      0      0      0      0      0      SE &lt;━┛
  3       0      0      0      0      0      0      1      0      JP
  </code></pre>
  <p>Further investigation lead me to <a href="https://www.kaggle.com/discdiver/category-encoders-examples#Hashing" rel="noreferrer">this Kaggle article</a>. The example of Hashing there include <strong>both X and y</strong>.</p>
  <ul>
  <li>What is the purpose of y, does it help to fight the collision problem?</li>
  <li>Should I add more columns to the encoder and encode more than one feature together (for example country and language)?</li>
  </ul>
  <p>Will appreciate an explanation of how to encode such categories using the hashing trick.</p>
  <p><strong>Update:</strong>
  Based on the comments I got from @CoMartel, Iv'e looked at <a href="https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.FeatureHasher.html" rel="noreferrer">Sklearn FeatureHasher</a> and written the following code to hash the country column:</p>
  <pre><code>from sklearn.feature_extraction import FeatureHasher
  h = FeatureHasher(n_features=10,input_type='string')
  f = h.transform(df.country)
  df1 = pd.DataFrame(f.toarray())
  df1['country'] = df.country
  df1.head()
  </code></pre>
  <p>And got the following output:</p>
  <pre><code>     0    1    2    3    4    5    6    7    8    9 country
  0 -1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 -1.0  0.0      US
  1 -1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 -1.0  0.0      US
  2 -1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 -1.0  0.0      US
  3  0.0 -1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0      CA
  4  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0 -1.0  0.0      SE
  5  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0      JP
  6 -1.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0      AU
  7 -1.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0      AU
  8 -1.0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0      DK
  9  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0 -1.0  0.0      SE
  </code></pre>
  <ul>
  <li>Is that the way to use the library in order to encode high categorical
  values?</li>
  <li>Why are some values negative?</li>
  <li>How would you choose the &quot;right&quot; <code>n_features</code> value?</li>
  <li>How can I check the collisions ratio?</li>
  </ul>