# Mutual Information Score¶

## Module Interface¶

class torchmetrics.clustering.MutualInfoScore(**kwargs)[source]

Compute Mutual Information Score.

$MI(U,V) = \sum_{i=1}^{|U|} \sum_{j=1}^{|V|} \frac{|U_i\cap V_j|}{N} \log\frac{N|U_i\cap V_j|}{|U_i||V_j|}$

Where $$U$$ is a tensor of target values, $$V$$ is a tensor of predictions, $$|U_i|$$ is the number of samples in cluster $$U_i$$, and $$|V_i|$$ is the number of samples in cluster $$V_i$$. The metric is symmetric, therefore swapping $$U$$ and $$V$$ yields the same mutual information score.

This clustering metric is an extrinsic measure, because it requires ground truth clustering labels, which may not be available in practice since clustering in generally is used for unsupervised learning.

As input to forward and update the metric accepts the following input:

• preds (Tensor): single integer tensor with shape (N,) with predicted cluster labels

• target (Tensor): single integer tensor with shape (N,) with ground truth cluster labels

As output of forward and compute the metric returns the following output:

Parameters:

kwargs (Any) – Additional keyword arguments, see Advanced metric settings for more info.

Example::
>>> import torch
>>> from torchmetrics.clustering import MutualInfoScore
>>> preds = torch.tensor([2, 1, 0, 1, 0])
>>> target = torch.tensor([0, 2, 1, 1, 0])
>>> mi_score = MutualInfoScore()
>>> mi_score(preds, target)
tensor(0.5004)

plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
Return type:
Returns:

Figure and Axes object

Raises:

ModuleNotFoundError – If matplotlib is not installed

>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.clustering import MutualInfoScore
>>> metric = MutualInfoScore()
>>> metric.update(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,)))
>>> fig_, ax_ = metric.plot(metric.compute())

>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.clustering import MutualInfoScore
>>> metric = MutualInfoScore()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,))))
>>> fig_, ax_ = metric.plot(values)


## Functional Interface¶

torchmetrics.functional.clustering.mutual_info_score(preds, target)[source]

Compute mutual information between two clusterings.

Parameters:
Return type:

Tensor

Example

>>> from torchmetrics.functional.clustering import mutual_info_score
>>> target = torch.tensor([0, 3, 2, 2, 1])
>>> preds = torch.tensor([1, 3, 2, 0, 1])
>>> mutual_info_score(preds, target)
tensor(1.0549)