clustering-mi
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.
Metadata
Release files for clustering-mi 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| clustering_mi-0.2.2.tar.gz | 155.0 kB | Without provenance | Details |
Built distributions (wheels)
Total release size: 7.0 MB
Release files / clustering_mi-0.2.2.tar.gz
| Download URL | clustering_mi-0.2.2.tar.gz |
|---|---|
| Size | 155.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b0698095ea753aa05e9b2615fe3c63e3dec217df1338aee951f5ddddd49f50b0
|
|
BLAKE2b-256 checksum How to use checksums |
3cc5469fd0664e1571810943f8d688267873582f9b523e0da779267d04251e76
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp313-cp313-win_amd64.whl
| Download URL | clustering_mi-0.2.2-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 98.1 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
83cb5c175165a64ff3a9ffc611cb3843150d88ef1daf1df885ce0952bfe8ac23
|
|
BLAKE2b-256 checksum How to use checksums |
4f0e520034a507ce93a816f3e8b8b03af2ee18132595f89ef1ed8e97b3d624ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp313-cp313-musllinux_1_2_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp313-cp313-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.13 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
11823ce45141101df326110e35ffc6bcdb5f34df7ebe6938cb1e10d1b491cf37
|
|
BLAKE2b-256 checksum How to use checksums |
018867594862b1b321b0df2092310212743d90b2be6711f9435b645f83af167d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 96.7 kB |
| Tags | CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
abe07d6ac3d92214f7b094a82d3a9c08deb1a1689ac4d414cb6e6677714e36eb
|
|
BLAKE2b-256 checksum How to use checksums |
be21ed98f48caf265a6acee095642231ea3e38ca49f7c90b078f091b83caa27c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | clustering_mi-0.2.2-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 81.8 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
9b4f13ab0886728a64b5c6a687529a6d7eef6f61bea2763b793947a39772de8a
|
|
BLAKE2b-256 checksum How to use checksums |
e85fd1593ed2b4eb86a9c3a5b4235ca736296fa8780e710c46742f1532a8d07f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp312-cp312-win_amd64.whl
| Download URL | clustering_mi-0.2.2-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 98.1 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
18926e0680cd594b7fdf79ef5b9f6521ed183f3eb825a481e0032d91548107e4
|
|
BLAKE2b-256 checksum How to use checksums |
decf05f258ccf8de7e3f9091506597ba2b5d189139ccd60f7b7d478d92d5a68f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp312-cp312-musllinux_1_2_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp312-cp312-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.12 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
7da60b52a46a47e4f2688395b4ed4b7d98cfb4ef3ab51fa9c730927b3165298d
|
|
BLAKE2b-256 checksum How to use checksums |
ab8b7e5e05f2c8a648cc911685f3df5df20ab4c56c272e0ba262504d6f7e59fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 96.7 kB |
| Tags | CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
1cbd5f0452fc7a9515e600ca192aeb9d661c96ec6eb8588e195c97f4b669f14f
|
|
BLAKE2b-256 checksum How to use checksums |
68caf03c0bc0a431b65924034fce650d625fd118f6dceff0224fa83dc33de76f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | clustering_mi-0.2.2-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 81.8 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
0cd8a48af612d6d5088c91a39ebf182db18b626f4c1a7804d0ca4a529f669ee2
|
|
BLAKE2b-256 checksum How to use checksums |
0351f3bcbf90289a3021d86f13babf0df84e7c17f9f3a4c248a21e47de9cc478
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp311-cp311-win_amd64.whl
| Download URL | clustering_mi-0.2.2-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 95.6 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
9b3e0ddccccbdf04f8ab77c2b66f48341274469f9de45f4e2d54e7d56e30b77b
|
|
BLAKE2b-256 checksum How to use checksums |
3a8ea01c9cc6f59b70871651683149483b1069f016581442a76f9ada78a29268
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp311-cp311-musllinux_1_2_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp311-cp311-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.11 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
23abd4e451ba0ad36969749af1b7334e376128c3bab3ece09986f1e384754cff
|
|
BLAKE2b-256 checksum How to use checksums |
c3157409840dc1bc455d074783f25873e8bda73b9f201f67a62726512eae6fc9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 94.8 kB |
| Tags | CPython 3.11 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
839bf906bc29e692916044702014c15db93e8424a9cd2774002fad29c23aaad9
|
|
BLAKE2b-256 checksum How to use checksums |
a97da5ae87104be2e0f5ce19912b09d3a09ec606279fbe141d357239b2df45b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | clustering_mi-0.2.2-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 80.2 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
4be277cc7e0ae1018d6a7b2d6a644627c9c5073d383aad8fe578a7ecd8e6da66
|
|
BLAKE2b-256 checksum How to use checksums |
79afff5340c55b8617929daf1fa2437fcddbe870bb7a59f10fb1c2c55e6a12c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp310-cp310-win_amd64.whl
| Download URL | clustering_mi-0.2.2-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 95.0 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
bd69d973d787689e268aa4b32022e020552d771a64959e35d7442a98d7aa0597
|
|
BLAKE2b-256 checksum How to use checksums |
2e4635e665cd049db1ac86c4080f335001d33e3304129b7c5aa8843b0bea95d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp310-cp310-musllinux_1_2_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp310-cp310-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.10 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
da7e144b7b24d0ff60be583bd24451a70893f11a917bf8e1cbd0e03c2ef0b8af
|
|
BLAKE2b-256 checksum How to use checksums |
dd35770ffea608bbb84357e2f6b23393dc141962c302de3e065cb8746468dece
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 93.4 kB |
| Tags | CPython 3.10 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
dedc054b0c95e9e5adba73d2f32cb1449c9d3a14edf68240aee786348aa1788d
|
|
BLAKE2b-256 checksum How to use checksums |
ca7dc26fcc597e5c0c24b19f0966884170e06245e25e4fcb28f9c1bdd67a3838
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | clustering_mi-0.2.2-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 78.9 kB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
2e0025468cd5a547112ffb5613834a292a2cecbc01fca46f70a3cace826beb59
|
|
BLAKE2b-256 checksum How to use checksums |
4f4c7d8196de74e79656ceab02e09e63afadfc913707814a16b11b05c622f904
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp39-cp39-win_amd64.whl
| Download URL | clustering_mi-0.2.2-cp39-cp39-win_amd64.whl |
|---|---|
| Size | 95.1 kB |
| Tags | CPython 3.9 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
63e133979bb3d40c80218ae39133dfbf418d46e1321adee219c2b452a79e6a8f
|
|
BLAKE2b-256 checksum How to use checksums |
cc05250f86244cbcd2f75dc2569497462b509b29dfedc8d1be466236b7ecb134
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp39-cp39-musllinux_1_2_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp39-cp39-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.9 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
d9a953c88c1af016c5df6b31c8c45ec1c0df9a57ce34539b09eb3721045a422c
|
|
BLAKE2b-256 checksum How to use checksums |
31fee014280eba37f7baa7c21d5e5773b2b2cf1ca6cfff5b67fd935d4fc06ceb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
| Download URL | clustering_mi-0.2.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 93.6 kB |
| Tags | CPython 3.9 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
8fa51233785472e0c762be9fd7da11a4384abd406f0d5d6c5be797ce0cfcb26d
|
|
BLAKE2b-256 checksum How to use checksums |
1911f5148af8ca9d6834b49fdbb08bfdbe51f3e7bf892b24a496f76899fd0dd0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / clustering_mi-0.2.2-cp39-cp39-macosx_11_0_arm64.whl
| Download URL | clustering_mi-0.2.2-cp39-cp39-macosx_11_0_arm64.whl |
|---|---|
| Size | 79.1 kB |
| Tags | CPython 3.9 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
ef91eb6162cb58e3601e1c0c19ac567fcca3ac780ef535d9f463e8ee9d057dcb
|
|
BLAKE2b-256 checksum How to use checksums |
764e3d4b03a2524feb6f1e20214610584ead2b26cf174b5b0127c08673217738
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Security for clustering-mi 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.