Skip to main content

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hg-0.0.9.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hg-0.0.9-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file hg-0.0.9.tar.gz.

File metadata

  • Download URL: hg-0.0.9.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","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}

File hashes

Hashes for hg-0.0.9.tar.gz
Algorithm Hash digest
SHA256 6a3c65698655f3ca3e636e05b746c7258d0d7bf264af9cf2cfc6ad4712536162
MD5 6b9d587493557ed330d436f2849a4981
BLAKE2b-256 f70dde9ed59b226090eff980b77c20a831ae99fd12fc5b3b8e1a320cc6dd85d1

See more details on using hashes here.

File details

Details for the file hg-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: hg-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","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}

File hashes

Hashes for hg-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0376dde20c47d20073b9f0aa0c6fac81f31560b131441a1239e3d260dff153f9
MD5 34b6bcc22efb410727afbd602977a1d1
BLAKE2b-256 2dff2febc587eb17dfdaaedcd9c10f63dced84eb886dccf0239d74a9be7cb866

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.9 This release

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page