datasketch: MinHash, LSH and HyperLogLog in Pure Python
MinHash, LSH, LSH Forest, Weighted MinHash, HyperLogLog, HyperLogLog++, LSH Ensemble and HNSW
At a glance
- What is it?
- datasketch is a Python library of probabilistic data structures for estimating Jaccard similarity, containment and cardinality, plus the indexes that make threshold and top-k queries sub-linear. Version 2.0.0 changed the default MinHash hash scheme, so persisted sketches need rebuilding.
- Who is it for?
- Adopt datasketch if you need Jaccard, containment or cardinality estimates inside a Python process and you are willing to accept hash-based approximation. Do not adopt it if you need exact set operations, or if you need a distributed sketch service with its own storage and query protocol.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 29 days ago.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.
Star history
- ekzhu/datasketch2,981 ★ · +13
| Repository | 2026-09-24 | 2026-11-24 |
|---|---|---|
| ekzhu/datasketch | 3k | 3k |
13 Stars in the last 61 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
What datasketch estimates, and what it refuses to compute
datasketch provides probabilistic sketches and search indexes. A MinHash uses a fixed-size summary to estimate set similarity; its error is statistical and depends on sketch parameters and input hashes, rather than being a universal deterministic bound. The README lists four sketches: MinHash for Jaccard similarity and cardinality, Weighted MinHash for weighted Jaccard, HyperLogLog and HyperLogLog++ for cardinality. On top of those it ships five indexes: MinHash LSH and LSHBloom for Jaccard threshold queries, MinHash LSH Forest for Jaccard top-k, MinHash LSH Ensemble for containment threshold, and HNSW for top-k under any custom metric.
The intended user is someone with more set-comparison work than CPU time. Deduplication of near-identical documents, clustering of short texts, candidate generation before an expensive exact comparison, distinct-count estimation over a stream. The pitch in the README is blunt: process and search very large amounts of data fast, with little loss of accuracy. The trade is explicit. You get a sketch whose size you choose at construction time, and you give up the ability to enumerate the original elements.
That last point matters more than it sounds. A MinHash can estimate Jaccard similarity against a compatible sketch and merge sketches to represent a union, but it cannot recover the original members or the exact intersection from the sketches alone. If your downstream step needs the actual tokens, you have to keep the tokens somewhere else and use the sketch only as a filter.
How MinHash and the LSH indexes fit together
A MinHash sketch is a fixed-length vector of hash values. The README describes the v2.0.0 default permutation scheme as affine32, and states that it fixes a similarity over-estimation bias on large sets, halves sketch memory, and speeds up updates by roughly 4x relative to the previous default. A 64-bit affine64 scheme is offered for billion-scale sets. The number of permutations is a constructor argument, so sketch size and estimation error are things you choose rather than things the library decides for you.
Jaccard estimation is then a comparison of two vectors: the fraction of positions where the two sketches agree estimates the Jaccard similarity of the underlying sets. No original element is touched at query time, which is why the operation is cheap and why it is approximate.
The LSH layer turns that pairwise comparison into a lookup. MinHash LSH bands the sketch into sub-vectors and hashes each band into a bucket. Two sets that collide in at least one band become candidates. The threshold is set with a constructor argument, and the probability curve is steep rather than a hard cutoff, so pairs just below the threshold can still surface. LSH Forest replaces the fixed banding with a prefix-tree structure and answers top-k queries instead of threshold queries. LSH Ensemble is the odd one out: it indexes MinHash sketches but answers containment queries, which is what you want when one set is much smaller than the other and Jaccard would be dominated by the size difference.
MinHash LSH and MinHash LSH Ensemble also support Redis and Cassandra as storage layers, according to the README, which is how the index survives a process restart or gets shared between workers.
Installing datasketch and running a first MinHash LSH query
The README gives the base install as a single pip command. It pulls in NumPy, and SciPy is a declared dependency in pyproject.toml as well. Python 3.9 or above is required.
pip install datasketchOptional extras are split by backend. Redis support, Cassandra support and Bloom filter support each come from their own extra, so a plain install does not drag in a database driver you will not use.
pip install datasketch[redis]
pip install datasketch[cassandra]
pip install datasketch[bloom]A minimal first use is to build two sketches, insert them into an LSH index with a Jaccard threshold, and query. The repository ships examples/minhash_examples.py and examples/lsh_examples.py, which are the files to read for the full constructor signatures. The shape of the workflow is: construct a MinHash with a chosen number of permutations, call update once per element, then pass the sketch to MinHashLSH along with a threshold and insert it under a key. A query with another sketch returns the keys whose stored sketches collide in at least one band. Expect false positives near the threshold; that is the design, not a bug.
If you are upgrading from an earlier release, the README's note is the first thing to read. It states that v2.0.0 changes the default scheme to affine32 and that hash values differ from earlier versions, and it gives two options: rebuild persisted sketches and LSH indexes, or pass scheme="legacy" to MinHash to interoperate with existing data. The README does not document a migration tool that converts stored sketches in place, so rebuilding is a job you schedule yourself.
Where datasketch is the wrong tool
The library is approximate by construction, and the error is not a small constant you can ignore at the edges. MinHash estimates Jaccard similarity from a sample of hash positions, so two genuinely different sets can land close together, and two similar sets can land apart. For a threshold query that is usually acceptable because you re-check candidates exactly. For a decision that is itself the final answer, such as a report of how many distinct users there were last month, the estimate is what you are shipping, and you should know the error bounds for the parameters you chose before you do that.
Memory is the second limit. A sketch is fixed-size, which is the whole point, but the index around it is not free. MinHash LSH keeps a bucket structure over every inserted sketch, and the README's at-scale section points at Redis and Cassandra precisely because an in-process index does not survive the size where you start needing it. HNSW, the one index in the library that accepts any custom metric, is a graph structure and carries the usual graph memory profile.
Third, this is a Python library with NumPy and SciPy underneath. It is not a service, it has no query protocol of its own beyond the Redis and Cassandra storage layers, and it does not distribute work across machines. If your workload is already a streaming job in another language, calling into Python for every sketch update is a worse fit than a native implementation. The related searches around Apache DataSketches and DataSketches-Java point at exactly that alternative audience.
datasketch versus Apache DataSketches
Apache DataSketches is the closest thing to a direct alternative, and the difference is not the algorithms. Both families cover cardinality estimation and similarity sketches, and the underlying ideas (MinHash, HyperLogLog) are shared literature. The difference is packaging and language.
Apache DataSketches is a Java library with bindings, and its sketches are designed around the JVM's memory and serialization model. datasketch is pure Python plus NumPy and SciPy, installs with pip, and its sketches are Python objects that pickle or serialize however you choose. If your pipeline is a Spark job, the Java implementation is the natural fit. If your pipeline is a Python notebook, a Flask service or an Airflow task, datasketch avoids a cross-language boundary.
A second difference is scope. datasketch ships LSH Ensemble specifically for containment threshold queries, which is a narrower capability than a general similarity toolkit, and it ships an HNSW index for arbitrary metrics. Apache DataSketches, as a project, is oriented around sketch accuracy and mergeability for streaming aggregation. Neither is a superset of the other. The related searches for datasketch and Apache DataSketches appearing together suggest people arrive at one while evaluating the other, and the honest answer is that the choice follows the language of the surrounding job, not the sketch math.
One thing worth checking before switching: whether the sketches need to be merged across partitions. datasketch's README describes Redis and Cassandra as storage layers for LSH, which is a persistence story, not a merge story.
Licence, maintenance and the cost of the 2.0.0 upgrade
datasketch is MIT licensed, as stated in the README and in pyproject.toml, and the package classifier is License :: OSI Approved :: MIT License. MIT is permissive: you can use it commercially, modify it and redistribute it, provided the copyright notice and permission notice travel with it. That is a description of the licence text, not legal advice, and if you are vendoring the source into a product you should read LICENSE in the repository root rather than a summary.
The dependency surface is small. NumPy and SciPy are required; Redis, Cassandra and Bloom filter support are optional extras. That matters for licence review because the required set is short and well known, and the optional drivers only enter your dependency graph if you ask for them.
Maintenance: the repository is not archived, and the last push was on 2026-08-09. Releases are not frequent: v1.9.0 on 2026-01-18, v1.10.0 on 2026-04-17, v2.0.0 on 2026-07-05. The project has a documented development setup built on uv, with uv sync for dependencies and uv run pytest for the test suite, and it uses ruff for linting and formatting. That is a real contributor workflow, not a stub.
The upgrade cost is the part to budget for. The README's own note says v2.0.0 changed the default MinHash permutation scheme to affine32, that hash values differ from earlier versions, and that you must rebuild persisted sketches and LSH indexes or pass scheme="legacy" to interoperate. The scheme="legacy" escape hatch keeps old data readable, but it also keeps you on the old memory and update-speed profile, and the README frames affine32 as fixing an over-estimation bias, so staying on legacy means keeping the bias. The README does not document a bulk migration path, and it does not state how long scheme="legacy" will remain supported.
Editorial conclusion
Adopt datasketch if you need Jaccard, containment or cardinality estimates inside a Python process and you are willing to accept hash-based approximation. Do not adopt it if you need exact set operations, or if you need a distributed sketch service with its own storage and query protocol. Before writing anything to disk, check which MinHash scheme your version defaults to, because v2.0.0 changed it to affine32 and the README says hash values differ from earlier versions.
Frequently asked questions
What is MinHash in datasketch and what is it used for?
MinHash is the datasketch sketch for estimating Jaccard similarity and cardinality from a fixed-size vector of hash values. The README lists it alongside Weighted MinHash, HyperLogLog and HyperLogLog++, and the MinHash LSH and LSH Forest indexes are built on top of it for threshold and top-k queries.
How do I install datasketch?
The README gives pip install datasketch for the base package, which also installs NumPy. Redis, Cassandra and Bloom filter support are separate extras: datasketch[redis], datasketch[cassandra] and datasketch[bloom]. Python 3.9 or above is required.
What changed in datasketch 2.0.0?
The README states that v2.0.0 changes the default MinHash permutation scheme to affine32, which fixes a similarity over-estimation bias on large sets, halves sketch memory and speeds up updates by roughly 4x. Hash values differ from earlier versions, so persisted sketches and LSH indexes must be rebuilt or MinHash must be constructed with scheme="legacy".
Which indexes does datasketch provide for MinHash queries?
The README lists MinHash LSH and LSHBloom for Jaccard threshold queries, MinHash LSH Forest for Jaccard top-k, MinHash LSH Ensemble for containment threshold, and HNSW for top-k under any custom metric. MinHash LSH and MinHash LSH Ensemble also support Redis and Cassandra storage layers.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/ekzhu-datasketch)Star history chart for your README
[](https://hysenlabs.com/projects/ekzhu-datasketch)Was this useful?