Homogenous Groups (Duplication detection, Frequent Itemsets, etc.)
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hg-0.0.8.tar.gz.
File metadata
- Download URL: hg-0.0.8.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9c07946c40837cf8692933b23fd9b420ce6f20b66942d23e5ced0fd774c3acc
|
|
| MD5 |
516dde4f31ac46752da56c2469a89a7a
|
|
| BLAKE2b-256 |
a018d1c1329a1dd9477e200730f01d46a9bed6e07063f2280ccfdf771f7e59e1
|
File details
Details for the file hg-0.0.8-py3-none-any.whl.
File metadata
- Download URL: hg-0.0.8-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98a9be7bace3c523f7d834dee934a989722647eae4e2657be8b150217d7da561
|
|
| MD5 |
72ec8a64f39c8f7b882309b6ad3e8114
|
|
| BLAKE2b-256 |
f7a4817c16c38426db7966852a9ae837d91dfc15909390c767393c466a942f44
|