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 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.

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.1.tar.gz (152.4 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.1-cp313-cp313-win_amd64.whl (97.3 kB view details)

Uploaded CPython 3.13Windows x86-64

clustering_mi-0.2.1-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.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (95.8 kB view details)

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

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

Uploaded CPython 3.13macOS 11.0+ ARM64

clustering_mi-0.2.1-cp312-cp312-win_amd64.whl (97.3 kB view details)

Uploaded CPython 3.12Windows x86-64

clustering_mi-0.2.1-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.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (95.8 kB view details)

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

clustering_mi-0.2.1-cp312-cp312-macosx_11_0_arm64.whl (80.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

clustering_mi-0.2.1-cp311-cp311-win_amd64.whl (94.8 kB view details)

Uploaded CPython 3.11Windows x86-64

clustering_mi-0.2.1-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.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (93.9 kB view details)

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

clustering_mi-0.2.1-cp311-cp311-macosx_11_0_arm64.whl (78.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

clustering_mi-0.2.1-cp310-cp310-win_amd64.whl (94.2 kB view details)

Uploaded CPython 3.10Windows x86-64

clustering_mi-0.2.1-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.1-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.1-cp310-cp310-macosx_11_0_arm64.whl (77.3 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

clustering_mi-0.2.1-cp39-cp39-win_amd64.whl (94.3 kB view details)

Uploaded CPython 3.9Windows x86-64

clustering_mi-0.2.1-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.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (92.8 kB view details)

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

clustering_mi-0.2.1-cp39-cp39-macosx_11_0_arm64.whl (77.4 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: clustering_mi-0.2.1.tar.gz
  • Upload date:
  • Size: 152.4 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.1.tar.gz
Algorithm Hash digest
SHA256 6f8b3a99074ca1e783459278e7c92c21b2fe0941ad4db206825a6ef32117f15e
MD5 dad44f582f728304389cb0050fbf192f
BLAKE2b-256 9dc1e94f6f8d75802d5579837f15f04f44480f5174688545b6479b65473c83bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3a4007dba6eddc7af570faa787132f8fd38f3de9284aaa3f7076eb3d35265c00
MD5 1d866d3f6e665e2fceda1ee5227841c4
BLAKE2b-256 f57ea548ec96f9bcb032baff80a3513dceae9f518d4a47fb11b889a22768b678

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 473a468a6de23a0e6434c3709a7a506f3f3b59e02272111a95793c6fe04cb26e
MD5 590ee043dad3787ed98e338040cf429d
BLAKE2b-256 c2bb8e7e2b92b355f999494722647af6600ce6b14e64defc42f30d235d48eb90

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 298a192b4dc9623a5217d0968965af7b7d4217e5ba7ffddc0ff32ebc445f3b78
MD5 f09de1d22709b845969c7568d84a3d43
BLAKE2b-256 0d19552c603e2e607d6a373f8d93b3b0353abd1484b9aa5696a2e267ec20f118

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3e9e1ffb2582f793f10016e14ec1cc26319120c892fca306b4c177ffb6fa4912
MD5 25401a9bea49eb7d69e8ea558846c063
BLAKE2b-256 db6df07607c34ed5d7a052a3e530e176f29c3ce3229c3351cd881defc24be1c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 75d9a2603314c0ee26b4ac32a490a137e54e07c0d072925ba330fb2a04a35142
MD5 2f1d941fec4bc212189d9d0454c23a87
BLAKE2b-256 7dc641fe4703cc260d64ae617d05a4e3f40b1b36c063d50942546b16084fd47d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0b06eaa4741695a2a058716fda4f3a799be2a6b618cd085572cabd9d81371933
MD5 2e3a835217c1333b81870c84bff7db3a
BLAKE2b-256 24a61e2d41017e32e26cfaac33093918d5fdd34aeecd111a38c17805e498c760

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ae62448c6dcf93dd69e124cf94b9bbd3bc4f6c1bfe997bd51998111285511cb6
MD5 faf16815293941e3009073c44c8de9d4
BLAKE2b-256 ae1f990ea982b2e527ad92b23e0b729eb5f156f5bd4dd21bf806119bdada02a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1691cb914dc7c55c7811313dab5cfe3c3b0a09f8a2c3937ddaf0b4d07e2ae539
MD5 f93d5e5eba7769425a60e88cabe1fa26
BLAKE2b-256 6241b64d91026354d0b6fe7e6c9e28516abccc804d3a89c618478e495b776eb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 86e1fb636307e23c23d631581ae6480210435321fda3a0ed22672aecfbe875bf
MD5 752a3ce0a32b07243bc2b97f1a397fab
BLAKE2b-256 4e803aa0b3f37f36abf9cd18c8111fff0063e711078316c658fe54dcb4fc87b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 736e47ef971c2536e712415c96031bb10e1b2d3427a0a006bd00c4677c09a9d9
MD5 dca27c31219c710f8cffb9d9be228773
BLAKE2b-256 1eec544dba1fbec5c1c334585b7eb3c712208a3a63649030b7a1e53a45711ac3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4b882befdb4861ea9db8dea224110eaaa15141059d81407f2741378b7f15d287
MD5 95056689fb331414623b097b29b85286
BLAKE2b-256 edcab926e246e7e6a773d75850c4723f04bf7af6782b4eafc48267c538cfdaa6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e9aa81c155947265427d232cf2507979b36415cefa0857bde7c3c02132c64b4b
MD5 2bd5345e7cabbeef3d75fead9468b22b
BLAKE2b-256 fcb093a17ec12e05ecc737f27ff05ffee037cf6a94da8e27a158fc8180a6e217

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4aa17f3eccfdced3a415ef5325f6c5bbf8717b9fe6dbcb65e67b56bade570ef6
MD5 5bce1b4e6828f9401116d00997c89658
BLAKE2b-256 f7da28cb6b3f6396ad6717f395256ad6db7af90715bc38898161241f1b18bb8e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 825fb1689fa364d2694721f471c52acb86146a6b86bbcce31dd52553830f91a6
MD5 f1b33d4ac1d5ef23dfd974431aa40a1b
BLAKE2b-256 bb704bcdf632577229a8b4b6da0bbbc816b82efa322087ad4cd147c5a1c0197f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ed1080b7d1e1f917b904a8cbd24218c49a66e61ba23fd965c6b9408d743cb1c9
MD5 5785483c6119e8491fea78060e7f680f
BLAKE2b-256 e66519afbe245ce080d8ff51afa1eaa88ca18555835023722067b5f24bed38d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a0a3b284b5aca631888bd1599e22003f1bfef25e8bbe0d3c67c2a7f9dcf47a65
MD5 6e775b7564d3684bb6bac85c82cbde87
BLAKE2b-256 1c47ae6559d7e9832f5bcfd9ccd25b3e29e2cb5463ca95c5fef8a20a7cdc9514

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 b8d9be892587d2173e65c1b2ed4f4201c10b46fcebb6af64734349506f627145
MD5 c84c2a6ebafb73440f0a32a4e706bf7f
BLAKE2b-256 b37515fe20ed3ef26067c30d868492c501a9456e287d1f45e286bf87676ffdab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 28cacbd257bffc5116ac35e056e28ec387548e8969d787f43c1c6b094e5da8d5
MD5 96600ca8387a73cce762655990e225b3
BLAKE2b-256 4a7877f7fcb98fbc920da75abc46d28e144505ab27b97420cb1b60f5bb5e6daa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a4f0318ea6f8da52bf8e67fbf865722647649011e0b1f122546d0ea70908dcf0
MD5 ad1dfbefadbbbcfdcb74e6aac8386cb6
BLAKE2b-256 9b827cf161c033ea6a1b7d1b0c8634665b6573655791b447af3e36e6f3ae124c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for clustering_mi-0.2.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7fbced11c54dd57b47381ad48db3322ac1030019e462bc2a38b7b4b0f1ffec73
MD5 be6f5ec90092453ea49bcff74a2150a3
BLAKE2b-256 cae9bbadd8b9251f656e645bf43f2758425d9f085ce3aff55b7eb657151ac525

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