mmh3 is a Python wrapper for MurmurHash (MurmurHash3), a set of fast and robust non-cryptographic hash functions invented by Austin Appleby.
Combined with probabilistic techniques like a Bloom filter, MinHash, and feature hashing, mmh3 allows you to develop high-performance systems in fields such as data mining, machine learning, and natural language processing.
How to use
pip install mmh3 # for macOS, use "pip3 install mmh3" and python3
>>> import mmh3
>>> mmh3.hash("foo") # returns a 32-bit signed int
>>> mmh3.hash("foo", 42) # uses 42 as a seed
>>> mmh3.hash("foo", signed=False) # returns a 32-bit unsigned int
>>> mmh3.hash64("foo") # two 64 bit signed ints (by using the 128-bit algorithm as its backend)
>>> mmh3.hash64("foo", signed=False) # two 64 bit unsigned ints
>>> mmh3.hash128("foo", 42) # 128 bit unsigned int
>>> mmh3.hash128("foo", 42, signed=True) # 128 bit signed int
>>> mmh3.hash_bytes("foo") # 128 bit value as bytes
>>> import numpy as np
>>> a = np.zeros(2 ** 32, dtype=np.int8)
hash64 returns two values, because it uses the 128-bit version of MurmurHash3 as its backend.
hash_from_buffer hashes byte-likes without memory copying. The method is suitable when you hash a large memory-view such as
>>> mmh3.hash_from_buffer(numpy.random.rand(100), signed=False)
hash_bytes have the third argument for architecture optimization. Use True for x64 and False for x86 (default: True):
>>> mmh3.hash64("foo", 42, True)
- Python wheels are now available, thanks to the power of cibuildwheel.
- Supported platforms are
macosx_11_0_arm64 (Apple Silicon).
- Add support for newer macOS environments. Thanks Matthew Honnibal!
- Drop support for Python 2.7, 3.3, 3.4, and 3.5.
- Add support for Python 3.7, 3.8, and 3.9.
- Migrate Travis CI and AppVeyor to GitHub Actions.
- Bug fix for
hash_bytes. Thanks doozr!
hash_from_buffer. Thanks Dimitri Vorona!
- Add a keyword argument
- Support seeds with 32-bit unsigned integers; thanks Alexander Maznev!
- Support 64-bit data (under 64-bit environments)
- Fix compile errors for Python 3.6 under Windows systems.
- Add unit testing and continuous integration with Travis CI and AppVeyor.
- Relicensed from public domain to CC0-1.0.
- Fix compile errors for gcc >=5.
hash128, which returns a 128-bit signed integer.
- Fix a misplaced operator which could cause memory leak in a rare condition.
- Fix a malformed value to a Python/C API function which may cause runtime errors in recent Python 3.x versions.
The first two commits are from Derek Wilson. Thanks!
- Improve portability to support systems with old gcc (version < 4.4) such as CentOS/RHEL 5.x. (Commit from Micha Gorelick. Thanks!)
__version__ constant. Check if it exists when the following revision matters for your application.
- Incorporate the revision r147, which includes robustness improvement and minor tweaks.
Beware that due to this revision, the result of 32-bit version of 2.1 is NOT the same as that of 2.0. E.g.,:
>>> mmh3.hash("foo") # in mmh3 2.0
>>> mmh3.hash("foo") # in mmh3 2.1
The results of hash64 and hash_bytes remain unchanged. Austin Appleby, the author of Murmurhash, ensured this revision was the final modification to MurmurHash3's results and any future changes would be to improve performance only.
Getting different results from other MurmurHash3-based libraries
By default, mmh3 returns signed values for 32-bit and 64-bit versions and unsigned values for
hash128, due to historical reasons. Please use the keyword argument
signed to obtain a desired result.
For compatibility with Google Guava (Java), see https://stackoverflow.com/questions/29932956/murmur3-hash-different-result-between-python-and-java-implementation
Unexpected results when given non 32-bit seeds
Version 2.4 changed the type of seeds from signed 32-bit int to unsigned 32-bit int. The resulting values with signed seeds still remain the same as before, as long as they are 32-bit.
>>> mmh3.hash("aaaa", -1756908916) # signed representation for 0x9747b28c
>>> mmh3.hash("aaaa", 2538058380) # unsigned representation for 0x9747b28c
Be careful so that these seeds do not exceed 32-bit. Unexpected results may happen with invalid values.
>>> mmh3.hash("foo", 2 ** 33)
>>> mmh3.hash("foo", 2 ** 34)
MurmurHash3 was originally developed by Austin Appleby and distributed under public domain.
Ported and modified for Python by Hajime Senuma.
The following textbooks and tutorials are great sources to learn how to use mmh3 (and other hash algorithms in general) for high-performance computing.