Skip to main content

Compute the mutual information between two clusterings of the same objects

Project description

clustering-mi

Documentation Status PyPI version PyPI platforms

Mutual information between clusterings

Maximilian Jerdee, Alec Kirkley, and Mark Newman

A Python package for computing the mutual information between two clusterings of the same set of objects. This implementation includes multiple variations and normalizations of the mutual information.

The package implements the reduced mutual information (RMI) as described in Jerdee, Kirkley, and Newman (2024), which corrects the standard measure's bias towards labelings with too many groups. The asymmetric normalization of Jerdee, Kirkley, and Newman (2023) is also included to remove the biases of symmetric normalizations. Data used to generate the figures in those papers is available in the examples/data directory of the repository.

Installation

clustering-mi can be installed through pip:

pip install clustering-mi

or built locally by cloning this repository and running

pip install .

in the base directory.

Typical usage

Once installed, the package can be imported as

import clustering_mi as cmi

Note that this is not import clustering-mi.

Two clusterings (or "labelings") can be loaded in several ways; the names of the groups are irrelevant:

# As arrays:
labels1 = ["red", "red", "red", "blue", "blue", "blue", "green", "green"]
labels2 = [1, 1, 1, 1, 2, 2, 2, 2]

# As a contingency table, i.e., a matrix that counts label co-occurrences.
# Columns are the first labeling, rows are the second labeling:
contingency_table = [[3, 1, 0], [0, 2, 2]]

# Or as a space-separated file:
"""
red 1
red 1
red 1
blue 1
blue 2
blue 2
green 2
green 2
"""
filename = "data/example.txt"

The package can then compute the mutual information (in bits) between the two labelings from any format:

# Defaults to the reduced mutual information (RMI)
mutual_information = cmi.mutual_information(labels1, labels2)  # From lists
mutual_information = cmi.mutual_information(contingency_table)  # From contingency table
mutual_information = cmi.mutual_information(filename)  # Reads filename

print(f"Mutual Information: {mutual_information:.3f} (bits)")

# Compute other variants using the "variation" parameter.
# Correcting for chance (random permutations)
adjusted_mutual_information = cmi.mutual_information(labels1, labels2, variation="adjusted")  
# Traditional mutual information
traditional_mutual_information = cmi.mutual_information(labels1, labels2, variation="traditional")

The package can also compute the normalized mutual information (NMI) between the two labelings, a measure bounded above by 1 when the two labelings are identical. Depending on the application, a symmetric or asymmetric normalization may be appropriate.

# Symmetric normalization
normalized_mutual_information = cmi.normalized_mutual_information(labels1, labels2, normalization="mean")
# "Normalized Mutual Information" most commonly refers to the Stirling-approximated mutual information
# divided by the mean of the entropies of the two labelings, although this is not our preferred measure.
normalized_stirling_mutual_information = cmi.normalized_mutual_information(labels1, labels2, variation="stirling", normalization="mean")

print(f"(symmetric) Normalized Mutual Information (labels1 <-> labels2): {normalized_mutual_information:.3f}")

# Asymmetric normalization measures how much the first labeling tells us about the second,
# as a fraction of all there is to know about the second labeling.
# This form is appropriate when the second labeling is a "ground truth" and the first is a prediction.
asymmetric_normalized_mutual_information_1_2 = cmi.normalized_mutual_information(labels1, labels2, normalization="second")
# Or when the first labeling is the ground truth and the second is a prediction.
asymmetric_normalized_mutual_information_2_1 = cmi.normalized_mutual_information(labels1, labels2, normalization="first")

print(f"(asymmetric) Normalized Mutual Information (labels1 -> labels2): {asymmetric_normalized_mutual_information_1_2:.3f}")
print(f"(asymmetric) Normalized Mutual Information (labels2 -> labels1): {asymmetric_normalized_mutual_information_2_1:.3f}")

Further usage examples can be found in the examples directory of the repository and the package documentation.

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

clustering_mi-0.2.0.tar.gz (152.3 kB view details)

Uploaded Source

Built Distributions

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

clustering_mi-0.2.0-cp313-cp313-win_amd64.whl (97.3 kB view details)

Uploaded CPython 3.13Windows x86-64

clustering_mi-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

clustering_mi-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (95.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

clustering_mi-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (80.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

clustering_mi-0.2.0-cp312-cp312-win_amd64.whl (97.2 kB view details)

Uploaded CPython 3.12Windows x86-64

clustering_mi-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

clustering_mi-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (95.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

clustering_mi-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (80.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

clustering_mi-0.2.0-cp311-cp311-win_amd64.whl (94.7 kB view details)

Uploaded CPython 3.11Windows x86-64

clustering_mi-0.2.0-cp311-cp311-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

clustering_mi-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (93.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

clustering_mi-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (78.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

clustering_mi-0.2.0-cp310-cp310-win_amd64.whl (94.1 kB view details)

Uploaded CPython 3.10Windows x86-64

clustering_mi-0.2.0-cp310-cp310-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

clustering_mi-0.2.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (92.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

clustering_mi-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (77.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

clustering_mi-0.2.0-cp39-cp39-win_amd64.whl (94.2 kB view details)

Uploaded CPython 3.9Windows x86-64

clustering_mi-0.2.0-cp39-cp39-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

clustering_mi-0.2.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (92.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

clustering_mi-0.2.0-cp39-cp39-macosx_11_0_arm64.whl (77.3 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file clustering_mi-0.2.0.tar.gz.

File metadata

  • Download URL: clustering_mi-0.2.0.tar.gz
  • Upload date:
  • Size: 152.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for clustering_mi-0.2.0.tar.gz
Algorithm Hash digest
SHA256 28d3a46f98a4804fbf6a2b9a344918a086e16418fcaa8770a0580a1379e90de8
MD5 06fa0d9f49aeb60adb145e6f72835c60
BLAKE2b-256 d5d335c071dfb8cc7de7f7fb8cd00df28d1fcae434decb98bbe6395b7c3d41f3

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bb44368fe692d29b0257bc53041b5b52b49b2cf32b6d8f8ea402d4a8d34909b5
MD5 563744f776a0b34bb298c623a3bccfcf
BLAKE2b-256 7f5e088ef8773ce649529cde01e0a846bbc0a5afdcb30ab35528341d30f97db9

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1aa440ffdab39c207824b2e3a9edcdfc096220a93b4443cbf0c7507c8f8222be
MD5 b7f306fd88593a79a64de7f4e75b6777
BLAKE2b-256 a401dfd23899e053006216d29b37d1e9fc83831ba9921bde89e4b62e20bbda48

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eb1e38cae6ee1b50fc9c035e76bd1446e5494e3cf33daa5fff72f37efae1a68a
MD5 8b7441cd042739db21f03765364b0249
BLAKE2b-256 cb5c078686277d1f267aedb9f3d266d79858efcea9b308414489d2bb87f123e5

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 489cc655fa6bea7b337a713021571c305a9258a97dd21b20ae56d2c87be150eb
MD5 d56be27806272899478ab297b363efee
BLAKE2b-256 6a39906c64faad9a3f608908c1f2f98907ef71b71f50c84e39e18f649ae3555f

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5513dae8a96169fa99e39a063048bd7a56ba821addc8bb6909dfdbda44c6772b
MD5 25ff317d8a49ef22b2f76304b12ca424
BLAKE2b-256 5f764f276867100ba5a7b2b0db19a6acd0ebef22d171927f5b9fb49927ef940d

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f2a42db70a27f4193cee71e646864d77cbfe0bba1fcd279a5ddabe2c4056ad2c
MD5 9336f864969c0a642c60510ee039f572
BLAKE2b-256 06ff3d8c43994092a0059f8a86929442bce78a1fa909762491f83c896cfbcc4a

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4fdeedd465ec36503f21aecc1f03225dccc60099dd83a3f745ba7f569d95ea8d
MD5 8cb870b37435419f101094d754142332
BLAKE2b-256 f500cd54ce618473d0cc0b38e5b896dbbef4f20265b945509a30514c896811d1

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 96773f4ff283a7bf74a4b1bc1f762d78898d0c6133cab1aa52139e357eeb18d2
MD5 4e4bd5aaf18241fb595b6b6b7af5766b
BLAKE2b-256 571436f40d68a184a92a79529dac51638518ae6289722c7a4e5c18ddf93444c4

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c57ceb6c7360d2bafb9706cd29a3609d9edb88bf7ff9f00a362c33b57f284fea
MD5 1a6f6bbb1d10eaa3d93d6754a4359c57
BLAKE2b-256 901c3c03b2c8f2fbc39457e18c86ae00c323f9a94139bfc2301bd9a8fb990188

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d57f974b5f20dbad96ad403ca2e948d5ceec0928cb2f6bbba2582e58bd1fd6ca
MD5 b04c350733354bd3db7caebb5766bdc1
BLAKE2b-256 2bbe9d4bafa5c0cbe1adf477bdaa600d3350c6f025337cd3b612763c457be14d

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cce50eed88b0d486518f8e5a909757cd63d52b4d24167900e2a821b1f4aba85a
MD5 146fa64121e8331240c891faa0cf1be9
BLAKE2b-256 851041fe7cb5153de37eb5f6d5da9e0fe2cd2455ad362f80e765bd2f66100041

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a2258cb7a575fd6ba567ffa5ed54fdfa73f6cb6345fc8b68f2ef7df4f0bba510
MD5 44a37d42ad68fdb9c73b43c2d62b0e7c
BLAKE2b-256 e0de781b5608e36eda57e0d47494c12790741b5da9fcac88489db43b3dbb09aa

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5e24859624b938b88e04876572c44af007bd3f413adf394547af99c5b7358eb3
MD5 43e9039dba55a97e915f9da67caefd6f
BLAKE2b-256 45b22012ec0b7df8a30b6d0323735e7d5aed9ff9377b537e8439a2a8fa74f497

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 22b96f996816561458d89702c036da5b658fa4ba64baad44a799dcbdc3684d7c
MD5 fc6b0745085d7048fa7af0dcd05d037c
BLAKE2b-256 45e28885a5fc2f658adffc8a2122c3ddc2a1b8d719b02d979e366f59397089fa

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 979f64820711ed2f9b33d845e7e733bff7bc83479006174507c38bd97e3c4667
MD5 2365fee5b1b6c5a268f3ccef85402edf
BLAKE2b-256 2505c280dda519d793395151ddf41a91f51cf975e42938daf2eec9c6d6acf661

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d7186e03839689fb402ea7cbe78207fa53fc0d7e256af5d0bc53bd35daa61772
MD5 67e19472a6315b3e3b6f3cf8c1aef12c
BLAKE2b-256 bc94083298baa791b4486c9d32a1d587275fb60fce00b532004abce4824ad253

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 be9579986d4840db17e189365aa86a993236445c30efc5cac1e29ce0db6bb453
MD5 45c52d008149495594df925a9a167e29
BLAKE2b-256 01690431d75125ebe70c1e1b1bb630234c7b251a63dd2012f851897fb3f84727

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cd5b5f810a6bcd38675c64d41ff44e8b06dd61f4eb7dee0aeec759b5349d6543
MD5 00c6e38053874f97acefb3cc853c0ae6
BLAKE2b-256 445f3a03c122bbaa00ab8902d04cbc16f0023793331a721f5f67251853fd5ea5

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2370922f1483618760c9391c48e156888d4b73ba003f82be7332ab5c04158c78
MD5 7a97d99fe0a19123fc77d05d601aed87
BLAKE2b-256 bf3be502cd24544124ee8fba94d480a122de81d997c849d5588bd0b3275a803e

See more details on using hashes here.

File details

Details for the file clustering_mi-0.2.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for clustering_mi-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fd6cc3da5d42b79511e97bd6c4920dc7b1bcd9cdfb8b6d783ee0d0a99e318424
MD5 f7318c093b7f1317179d2760ee282619
BLAKE2b-256 4d288eaa5842f01aafafcbfa957a4427c2040fcba1fd44a1cff721af46ed39f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page