LSH hashcode generator function
There are two things I don't understand about the code in the post.


Are you really committed to using LIL (list-of-lists) format? The documentation is very clear that LIL is not suitable for computing matrix products, for example here:


  To perform manipulations such as multiplication or inversion, first convert the matrix to either CSC or CSR format.


and here:


  Disadvantages of the LIL format [...] slow matrix vector products (consider CSR or CSC).


I find that using CSR instead of LIL for matrix and vector gives an immediate 20× speedup.

Even if you have a good reason for using the LIL format in some parts of your code, you should still convert to CSR before computing the product, as suggested in the documentation.
Given that matrix is dense, why does the code represent it using a sparse matrix? This is bound to incur some overhead that could be avoided by using an ordinary np.matrix.

I find that switching matrix from CSR to np.matrix gives a further 1.5× speedup.