| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
The HyperLogLog algorithm [1] is a space efficient method to estimate the cardinality of extraordinarily large datasets. This module is written in C for Python >= 3.6. It implements a 64 bit version of HyperLogLog [2] using a Murmur64A hash.
Install Python development libraries. This step will depend on your OS. On Ubuntu 24.0.4 LTS:
sudo apt install python-dev-is-python3
Install HLL:
pip install HLL
Example usage:
from HLL import HyperLogLog
hll = HyperLogLog(10) # use 2^10 registers
hll.add('some data')
estimate = hll.cardinality()
print(estimate)
New features:
Sparse representation rewrite:
Bug fixes:
Cleanup:
Deprecation notice: this is the last supported version for Python 2.7.x.
HyperLogLog objects implement a 64 bit HyperLogLog algorithm [2]. They can be used to estimate the cardinality of very large datasets. The estimation accuracy is proportional to the number of registers. Using more registers increases the accuracy and using less registers decreases the accuracy. The number of registers is set in powers of 2 using the parameter p and defaults to p=12 or $2^{12}$ registers.
>>> from HLL import HyperLogLog >>> hll = HyperLogLog() # Default to 2^12 registers >>> hll.size() 4096 >>> hll = HyperLogLog(3) # Use 2^3 registers >>> hll.size() 8 >>> for data in ['one', 'two', 'three', 'four',]: ... hll.add(data) >>> hll.cardinality() 4
HyperLogLogs use a Murmur64A hash. This function is fast and has a good uniform distribution of bits which is necessary for accurate estimations. The seed to this hash function can be set in the HyperLogLog constructor:
>>> hll = HyperLogLog(p=2, seed=123456789) >>> hll.seed() 123456789
The hash function can also be called directly:
>>> hll.hash('something')
393810339
Individual registers can be printed:
>>> for i in range(2**4): ... print(hll.get_register(i)) 0 0 3 0 4
HyperLogLog objects can be merged. This is done by taking the maximum value of their respective registers:
>>> A = HyperLogLog(p=4)
>>> A.add('hello')
>>> B = HyperLogLog(p=4)
>>> B.add('world')
>>> A.merge(B)
>>> A.cardinality()
2
The intersection cardinality of two HyperLogLog objects can be estimated using Ertl's Joint Maximum Likelihood Estimation (JMLE) method [2]. This estimation degrades in accuracy when the Jaccard, J=|A∩B|/|A∪B|, is less than .05 and severely degrades when J < .01.
Note that objects objects must have the same p value:
>>> A = HyperLogLog(p=12) >>> B = HyperLogLog(p=12) >>> for i in range(50000): ... A.add(str(i)) ... B.add(str(i)) >>> for i in range(50000, 100000): ... A.add(str(i)) >>> A.intersection_cardinality(B) 50164
Registers are stored using both sparse and dense representation. Originally all registers are initialized to zero. However storing all these zeroes individually is wasteful. Inspired by the sorted linked list approach in [3], a sorted dynamic array of packed 4-byte entries is used to store only registers that have been set (e.g. have a non-zero value). When the array's allocated memory would exceed the equivalent dense storage, the HyperLogLog switches to dense representation where registers are stored individually using 6 bits.
Sparse representation can be disabled using the sparse flag:
>>> HyperLogLog(p=2, sparse=False)
Adding an element to the HyperLogLog does not immediately update the sorted array. A temporary buffer is used to defer this operation. When the buffer is full the items are sorted and merged into the array in one pass.
The buffer size can be set using max_buffer_size:
>>> HyperLogLog(p=15, max_buffer_size=10**5)
This software is released under the MIT License.
[1] P. Flajolet, E. Fusy, O. Gandouet, F. Meunier. "HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm," Conference on the Analysis of Algorithms 2007.
[2] O. Ertl, "New Cardinality Estimation Methods for HyperLogLog Sketches," arXiv:1706.07290 [cs], June 2017.
[3] S. Heule, M. Nunkesser, A. Hall. "HyperLogLog in Practice: Algorithmic Engineering of a State of the Art Cardinality Estimation Algorithm," Proceedings of the EDBT 2013 Conference, ACM, Genoa March 2013.
| Back | FazBrowse Home | New Git URL |