Understanding sklearn FeatureHasher Wanting to understand "the hashing trick" I've written the following test code:

import pandas as pd
from sklearn.feature_extraction import FeatureHasher
test = pd.DataFrame({'type': ['a', 'b', 'c', 'd', 'e','f','g','h']})
h = FeatureHasher(n_features=4, input_type='string')
f = h.transform(test.type)
print(f.toarray())


In the above example, I'm mapping 8 categories into 4 columns, and the output is:

[[ 0.  0.  1.  0.]&lt;-a
 [ 0. -1.  0.  0.]&lt;-b
 [ 0. -1.  0.  0.]&lt;-c
 [ 0.  0.  0.  1.]&lt;-d
 [ 0.  0.  0.  1.]&lt;-e
 [ 0.  0.  0.  1.]&lt;-f
 [ 0.  0. -1.  0.]&lt;-g
 [ 0. -1.  0.  0.]]&lt;-g


In the resulting matrix, I can see repetitions and some categories are represented the same way. Why is that? 8 categories can be mapped into 4 columns if I use a binary representation.

Can someone please explain the output of this technique and maybe elaborate a bit?
FeatureHasher assigns each token to a single column in the output; it does not do the sort of binary encoding that would allow you to faithfully encode more features to a smaller space.  Instead, the point is that you don't have to look up which column a given category maps to as you would in OneHotEncoder: you just have to apply the hash function to the value.  Using a lower-dimensional target space will guarantee collisions, but even a larger target space may produce a few collisions.