Skip to main content
PyPI Version License

DRUHG

DRUHG — Dialectical Reflection Universal Hierarchical Grouping (друг).

Density-based clustering that catches global outliers and lets you navigate the cluster hierarchy visually. It does not require clustering hyperparameters. The space metric (e.g. Euclidean) is the only real choice.

Optional knobs:

  • size_range — filter cluster size; use [1, 1] for genuine outliers.

  • fix_outliers — assign outliers to their closest clusters along MST edges.

  • max_ranking — neighbor-search depth; trade speed for precision.

Basic Concept

The algorithm applies the universal society rule: treat others as you want to be treated.

Point A inspects the surroundings of point B and converts that view into its own. Each pair A, B produces a dialectical distance max( r/(R-∩) d(r); d(R) ), where r and R are the numbers of points inside the balls from A to B and from B to A. The closest dialectical distance wins and crystallizes into an edge. The process repeats.

This orders outliers last and equal densities first. It is a strong EDA (exploratory data analysis) method, a replacement for (H)DBSCAN, and a global-outlier detector.

Evaluating all O(n²) pairs is unnecessary; only a small number of nearest neighbors matters. Control speed vs precision with max_ranking — after some k the result converges.

The cluster. The coloring formula is easiest to see through graphs and the nature of mathematical objects. Points are, edges connect, and the dictionary of key–value pairs (point-to-edge) “colors”. When two graphs connect, the two sets of points can be linked to the connecting edge:

  1. Both graphs clusterize to the same edge; In case of mutual clusterisation it is seen as one cluster.

  2. One graph clusterizes; its points link to the connecting edge (a regular cluster).

  3. No clusterisation. Everything aggregates.

Each graph reflects in its rival and solves the inequality:

D N₂ L₁ ∑₁ 1 / dᵢ > N₁ (L₁ + L₂)

where:

  • D — dialectical distance of the connecting edge

  • N₁, N₂ — own and rival sides of a graph

  • L₁, L₂ — unique linked edges

  • ∑₁ 1 / dᵢ — sum of reciprocals of unique linked edges

A newly formed cluster resists reclusterisation with its internal high dᵢ and low L₁. Outliers bring 1 as N₂, contribute 1 to L₂. Eventually a huge external D, N₂, or dilution of L₁ will clusterize anything.

This is drastically different from the usual overcome-xyz coefficient.

How to use DRUHG

import sklearn.datasets as datasets
from sklearn.metrics import adjusted_rand_score
import druhg

iris = datasets.load_iris()
XX = iris['data']

clusterer = druhg.DRUHG(max_ranking=50)
labels = clusterer.fit(XX).labels_

This builds the tree and labels the points. You can then reshape clusters by relabeling:

labels = clusterer.relabel(exclude=[7749, 100], size_range=[0.2, 2242], fix_outliers=True)
ari = adjusted_rand_score(iris['target'], labels)
print('iris ari', ari)

Relabeling is cheap:

  • exclude — break clusters by label number

  • size_range — restrict cluster size by fraction (values < 1) or by absolute count

  • fix_outliers — color outliers by connectivity

Draw the MST with DRUHG edges:

clusterer.plot(labels)

Or open interactive sliders for exploration:

clusterer.plot()
chameleon-sliders

Performance

It can be slow on highly structured data. Lower max_ranking for better performance.

comparison

Installing

PyPI install, assuming an up-to-date pip:

pip install druhg

Running the Tests

After installation:

pytest druhg/tests -k "test_name"

The tests may fail.

pytest druhg/tests -k "test_name" -v

For a verbose logging

pytest druhg/tests -k "test_name" -log=DEBUG

For a deep dive

Python Version

DRUHG supports Python 3.

Contributing

Contributions in any form are welcome. Help with documentation, especially tutorials, is always useful. Fork the project, make your changes, and submit a pull request:

https://github.com/artamono1/druhg

Licensing

The druhg package is 3-clause BSD licensed.

Download files

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

Source Distribution

druhg-1.8.2.tar.gz (32.4 kB view details)

Uploaded Source

File details

Details for the file druhg-1.8.2.tar.gz.

File metadata

  • Download URL: druhg-1.8.2.tar.gz
  • Upload date:
  • Size: 32.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.4

File hashes

Hashes for druhg-1.8.2.tar.gz
Algorithm Hash digest
SHA256 70cd7075fadfa4630b5d70306efad068fd8158d2fb1bc36326340f5d306bf264
MD5 044c31ac421e8d7ee8a9230070ad4ae8
BLAKE2b-256 edac1328a711ee992381bd19be3fc0322388fbe321c76a315c58595bcb08e2fd

See more details on using hashes here.

Release history Release notifications | RSS feed

1.8.3

1 file

This release

1.8.2 This release

1 file

1.8.1

1 file

1.7.4

1 file

1.7.3

1 file

1.7.2

1 file

1.7.1

1 file

1.7.0

1 file

1.5.0

1 file

1.4.1

1 file

1.4.0

1 file

1.3.0

2 files

1.2.1

1 file

1.2.0

1 file

1.1.7

1 file

1.1.6

1 file

1.1.5

1 file

1.1.4

1 file

1.1.3

1 file

1.1.2

2 files

1.1.0

1 file

1.0.1

1 file

1.0

1 file

0.10

1 file

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