hg
Homogenous Groups — find items that recur together.
To install: pip install hg
hg bundles two complementary ways of discovering repeated structure in data,
with no third-party dependencies (pure standard library):
- Duplication detection — find and remove the largest repeated contiguous blocks in an ordered sequence (e.g. repeated line-blocks in text).
- Frequent-itemset mining — find sets of items that co-occur across transactions, optionally weighted by a per-transaction value.
Duplication detection
The simplest case — de-duplicate repeated line-blocks in text, keeping the first occurrence:
>>> from hg import deduplicate_string_lines
>>> text = "A\nB\nC\nA\nB\nC\nD"
>>> final_text, removed = deduplicate_string_lines(text, min_block_size=3)
>>> print(final_text)
A
B
C
D
>>> removed
[RemovedBlock(removed_start=3, length=3, block_items=['A', 'B', 'C'])]
The same works on any sequence of items via deduplicate_sequence (or the
reusable BlockDeduplicator), with an optional key to control how items are
compared:
>>> from hg import deduplicate_sequence
>>> deduped, removed = deduplicate_sequence([1, 2, 1, 2, 3], min_block_size=2)
>>> deduped
[1, 2, 3]
min_block_size is the smallest repeated run to detect; detected blocks are
then greedily extended to the largest repeated run.
Frequent-itemset mining
Find sets of items that appear together in at least minimum_support
transactions:
>>> from hg import find_frequent_itemsets
>>> transactions = [
... ['bread', 'milk'],
... ['bread', 'milk', 'eggs'],
... ['milk', 'eggs'],
... ['bread', 'butter'],
... ]
>>> for itemset in sorted(
... find_frequent_itemsets(transactions, minimum_support=2),
... key=lambda it: (-it.support, sorted(it.items)),
... ):
... print(sorted(itemset.items), itemset.support)
['bread'] 3
['milk'] 3
['bread', 'milk'] 2
['eggs'] 2
['eggs', 'milk'] 2
Value-weighted itemsets
The distinguishing feature: pass transaction_values to accumulate an arbitrary
per-transaction quantity (revenue, duration, ...) alongside the count. Each
result is a FrequentItemset(items, support, value):
>>> prices = [4.0, 9.0, 5.0, 7.0] # one value per transaction
>>> by_value = {
... tuple(sorted(it.items)): it.value
... for it in find_frequent_itemsets(
... transactions, transaction_values=prices, minimum_support=2
... )
... }
>>> by_value[('bread', 'milk')] # baskets 0 (4.0) and 1 (9.0)
13.0
With no transaction_values, value simply equals support.
When to use which
- Reach for duplication detection when order matters and you want to collapse repeated runs (deduping logs, transcripts, generated text).
- Reach for frequent-itemset mining when co-occurrence matters and order does not (market-basket analysis, tag/feature co-occurrence), especially when you want to weight occurrences by a value.
Metadata
Release files for hg 0.0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hg-0.0.10.tar.gz | 18.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hg-0.0.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.9 kB
Release files / hg-0.0.10.tar.gz
| Download URL | hg-0.0.10.tar.gz |
|---|---|
| Size | 18.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
65ba59bde63a52610455ff1a545b7f2c1b8dbfc2619d89bd836075021154c487
|
|
BLAKE2b-256 checksum How to use checksums |
7e4c917ddcefe68bf8b9775394714fc858a01afcf428451e7e0ec13b4bb93b5d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / hg-0.0.10-py3-none-any.whl
| Download URL | hg-0.0.10-py3-none-any.whl |
|---|---|
| Size | 12.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7cd1ccaf71100bacc13a7c687ffc9ccd3537adc45869358832d799445049958c
|
|
BLAKE2b-256 checksum How to use checksums |
a09d1360119e19104daf93f70b45499f68d63429c206ed61b763c0385c6ab75b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|