Skip to main content

Compute the mutual information between two clusterings of the same objects

Project description

clustering-mi

Documentation Status CI codecov PyPI version PyPI platforms DOI

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.

The mutual information variation (passed to both mutual_information and normalized_mutual_information) selects the measure:

variation Description
"reduced" (default) Reduced MI, Dirichlet-multinomial reduction (Jerdee et al. 2024).
"reduced_flat" Reduced MI, flat reduction (Newman et al. 2019).
"adjusted" Adjusted MI (AMI), correcting for chance under random permutations (Vinh et al. 2010).
"traditional" Traditional (microcanonical) mutual information.
"stirling" Stirling approximation of the traditional MI.

For normalized_mutual_information, the normalization selects the denominator:

normalization Description
"second" (default) Asymmetric: fraction of the second labeling's information recovered.
"first" Asymmetric: fraction of the first labeling's information recovered.
"mean" Symmetric: normalize by the arithmetic mean of the two entropies.
"min" / "max" Symmetric: normalize by the minimum / maximum of the two entropies.
"geometric" Symmetric: normalize by the geometric mean of the two entropies.
"none" No normalization; return the mutual information in bits.

Installation

clustering-mi can be installed through pip:

pip install clustering-mi

or built locally with a c++ compiler 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 the path to a space-separated file, one object (label1 label2) per line:
filename = "data/example.txt"

where data/example.txt contains one labeled object per line:

red 1
red 1
red 1
blue 1
blue 2
blue 2
green 2
green 2

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.2.tar.gz (155.0 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.2-cp313-cp313-win_amd64.whl (98.1 kB view details)

Uploaded CPython 3.13Windows x86-64

clustering_mi-0.2.2-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.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (96.7 kB view details)

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

clustering_mi-0.2.2-cp313-cp313-macosx_11_0_arm64.whl (81.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

clustering_mi-0.2.2-cp312-cp312-win_amd64.whl (98.1 kB view details)

Uploaded CPython 3.12Windows x86-64

clustering_mi-0.2.2-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.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (96.7 kB view details)

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

clustering_mi-0.2.2-cp312-cp312-macosx_11_0_arm64.whl (81.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

clustering_mi-0.2.2-cp311-cp311-win_amd64.whl (95.6 kB view details)

Uploaded CPython 3.11Windows x86-64

clustering_mi-0.2.2-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.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (94.8 kB view details)

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

clustering_mi-0.2.2-cp311-cp311-macosx_11_0_arm64.whl (80.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

clustering_mi-0.2.2-cp310-cp310-win_amd64.whl (95.0 kB view details)

Uploaded CPython 3.10Windows x86-64

clustering_mi-0.2.2-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.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (93.4 kB view details)

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

clustering_mi-0.2.2-cp310-cp310-macosx_11_0_arm64.whl (78.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

clustering_mi-0.2.2-cp39-cp39-win_amd64.whl (95.1 kB view details)

Uploaded CPython 3.9Windows x86-64

clustering_mi-0.2.2-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.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (93.6 kB view details)

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

clustering_mi-0.2.2-cp39-cp39-macosx_11_0_arm64.whl (79.1 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: clustering_mi-0.2.2.tar.gz
  • Upload date:
  • Size: 155.0 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.2.tar.gz
Algorithm Hash digest
SHA256 b0698095ea753aa05e9b2615fe3c63e3dec217df1338aee951f5ddddd49f50b0
MD5 158289ebf6ccdd38c6c2ac05ee941a89
BLAKE2b-256 3cc5469fd0664e1571810943f8d688267873582f9b523e0da779267d04251e76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 83cb5c175165a64ff3a9ffc611cb3843150d88ef1daf1df885ce0952bfe8ac23
MD5 d69480b7ead996a27f9be7501d181d10
BLAKE2b-256 4f0e520034a507ce93a816f3e8b8b03af2ee18132595f89ef1ed8e97b3d624ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 11823ce45141101df326110e35ffc6bcdb5f34df7ebe6938cb1e10d1b491cf37
MD5 96c0c684ce4a9a0e58974b2daea9323e
BLAKE2b-256 018867594862b1b321b0df2092310212743d90b2be6711f9435b645f83af167d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 abe07d6ac3d92214f7b094a82d3a9c08deb1a1689ac4d414cb6e6677714e36eb
MD5 c26ca4989735f0236ee91e538a74aee7
BLAKE2b-256 be21ed98f48caf265a6acee095642231ea3e38ca49f7c90b078f091b83caa27c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9b4f13ab0886728a64b5c6a687529a6d7eef6f61bea2763b793947a39772de8a
MD5 36a01449355541cc250ad5a62f463f4f
BLAKE2b-256 e85fd1593ed2b4eb86a9c3a5b4235ca736296fa8780e710c46742f1532a8d07f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 18926e0680cd594b7fdf79ef5b9f6521ed183f3eb825a481e0032d91548107e4
MD5 56259bba19502f42adb55a411536f6e7
BLAKE2b-256 decf05f258ccf8de7e3f9091506597ba2b5d189139ccd60f7b7d478d92d5a68f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7da60b52a46a47e4f2688395b4ed4b7d98cfb4ef3ab51fa9c730927b3165298d
MD5 806880e5be5c2aab6005ee4355a94502
BLAKE2b-256 ab8b7e5e05f2c8a648cc911685f3df5df20ab4c56c272e0ba262504d6f7e59fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1cbd5f0452fc7a9515e600ca192aeb9d661c96ec6eb8588e195c97f4b669f14f
MD5 685d8ace843d0baa2d2298d61192175a
BLAKE2b-256 68caf03c0bc0a431b65924034fce650d625fd118f6dceff0224fa83dc33de76f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0cd8a48af612d6d5088c91a39ebf182db18b626f4c1a7804d0ca4a529f669ee2
MD5 12454b8096214f0a029e074d614b3b67
BLAKE2b-256 0351f3bcbf90289a3021d86f13babf0df84e7c17f9f3a4c248a21e47de9cc478

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9b3e0ddccccbdf04f8ab77c2b66f48341274469f9de45f4e2d54e7d56e30b77b
MD5 506772bc8ffc5d854531b6b7447113a4
BLAKE2b-256 3a8ea01c9cc6f59b70871651683149483b1069f016581442a76f9ada78a29268

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 23abd4e451ba0ad36969749af1b7334e376128c3bab3ece09986f1e384754cff
MD5 3afea33166e69b18cb2dff00cce7158c
BLAKE2b-256 c3157409840dc1bc455d074783f25873e8bda73b9f201f67a62726512eae6fc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 839bf906bc29e692916044702014c15db93e8424a9cd2774002fad29c23aaad9
MD5 199023df05b467f91d03f45b2723e8d4
BLAKE2b-256 a97da5ae87104be2e0f5ce19912b09d3a09ec606279fbe141d357239b2df45b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4be277cc7e0ae1018d6a7b2d6a644627c9c5073d383aad8fe578a7ecd8e6da66
MD5 3c92dbba85b987e34fe6388ea1193331
BLAKE2b-256 79afff5340c55b8617929daf1fa2437fcddbe870bb7a59f10fb1c2c55e6a12c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 bd69d973d787689e268aa4b32022e020552d771a64959e35d7442a98d7aa0597
MD5 3c20f66d31c1c6875b67fbabac1dd857
BLAKE2b-256 2e4635e665cd049db1ac86c4080f335001d33e3304129b7c5aa8843b0bea95d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 da7e144b7b24d0ff60be583bd24451a70893f11a917bf8e1cbd0e03c2ef0b8af
MD5 2df4a04e8cb583fe484d9f555b4d4e4a
BLAKE2b-256 dd35770ffea608bbb84357e2f6b23393dc141962c302de3e065cb8746468dece

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dedc054b0c95e9e5adba73d2f32cb1449c9d3a14edf68240aee786348aa1788d
MD5 144a9275a381262eb77836bee1639718
BLAKE2b-256 ca7dc26fcc597e5c0c24b19f0966884170e06245e25e4fcb28f9c1bdd67a3838

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e0025468cd5a547112ffb5613834a292a2cecbc01fca46f70a3cace826beb59
MD5 39f5834109d0bacd64653621379729f6
BLAKE2b-256 4f4c7d8196de74e79656ceab02e09e63afadfc913707814a16b11b05c622f904

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 63e133979bb3d40c80218ae39133dfbf418d46e1321adee219c2b452a79e6a8f
MD5 12c936e049cfa44e8fec552ea5aecadd
BLAKE2b-256 cc05250f86244cbcd2f75dc2569497462b509b29dfedc8d1be466236b7ecb134

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d9a953c88c1af016c5df6b31c8c45ec1c0df9a57ce34539b09eb3721045a422c
MD5 61a6a187f1822f36ab06ddc470d42b0b
BLAKE2b-256 31fee014280eba37f7baa7c21d5e5773b2b2cf1ca6cfff5b67fd935d4fc06ceb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8fa51233785472e0c762be9fd7da11a4384abd406f0d5d6c5be797ce0cfcb26d
MD5 7d123fa3627b5257753130648b45bc39
BLAKE2b-256 1911f5148af8ca9d6834b49fdbb08bfdbe51f3e7bf892b24a496f76899fd0dd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ef91eb6162cb58e3601e1c0c19ac567fcca3ac780ef535d9f463e8ee9d057dcb
MD5 3aabc0b6ee4a561df0f7e1d2a5d7fb64
BLAKE2b-256 764e3d4b03a2524feb6f1e20214610584ead2b26cf174b5b0127c08673217738

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