---
title: Calinski Harabasz Score
description: >-
  Compute Calinski Harabasz Score (also known as variance ratio criterion) for
  clustering algorithms.
canonical: >-
  https://lightning.ai/docs/torchmetrics/stable/clustering/calinski_harabasz_score
---
# Calinski Harabasz Score {#calinski-harabasz-score}

## Module Interface {#module-interface}

### `class torchmetrics.clustering.CalinskiHarabaszScore(**kwargs)` {#torchmetrics.clustering.CalinskiHarabaszScore}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/clustering/calinski_harabasz_score.py#L29-L128)

Compute Calinski Harabasz Score (also known as variance ratio criterion) for clustering algorithms.

```math
CHS(X, L) = \frac{B(X, L) \cdot (n_\text{samples} - n_\text{labels})}{W(X, L) \cdot (n_\text{labels} - 1)}
```

where $`B(X, L)`$ is the between-cluster dispersion, which is the squared distance between the cluster centers and the dataset mean, weighted by the size of the clusters, $`n_\text{samples}`$ is the number of samples, $`n_\text{labels}`$ is the number of labels, and $`W(X, L)`$ is the within-cluster dispersion e.g. the sum of squared distances between each samples and its closest cluster center.

This clustering metric is an intrinsic measure, because it does not rely on ground truth labels for the evaluation. Instead it examines how well the clusters are separated from each other. The score is higher when clusters are dense and well separated, which relates to a standard concept of a cluster.

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

*   `data` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float tensor with shape `(N,d)` with the embedded data. `d` is the dimensionality of the embedding space.
    
*   `labels` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): single integer tensor with shape `(N,)` with cluster labels
    

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

*   `chs` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): A tensor with the Calinski Harabasz Score
    

Parameters:

**kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.

Example::

```python
>>> from torch import randn, randint
>>> from torchmetrics.clustering import CalinskiHarabaszScore
>>> data = randn(20, 3)
>>> labels = randint(3, (20,))
>>> metric = CalinskiHarabaszScore()
>>> metric(data, labels)
tensor(2.2128)
```

#### `plot(val=None, ax=None)` {#torchmetrics.clustering.CalinskiHarabaszScore.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/clustering/calinski_harabasz_score.py#L90-L128)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.clustering import CalinskiHarabaszScore
>>> metric = CalinskiHarabaszScore()
>>> metric.update(torch.randn(20, 3), torch.randint(3, (20,)))
>>> fig_, ax_ = metric.plot(metric.compute())
```

[![../../\_images/calinski\_harabasz\_score-1.png](/docs/torchmetrics/1.9.0/static/_images/calinski_harabasz_score-1.png)](/docs/torchmetrics/1.9.0/static/_images/calinski_harabasz_score-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.clustering import CalinskiHarabaszScore
>>> metric = CalinskiHarabaszScore()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(torch.randn(20, 3), torch.randint(3, (20,))))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/calinski\_harabasz\_score-2.png](/docs/torchmetrics/1.9.0/static/_images/calinski_harabasz_score-2.png)](/docs/torchmetrics/1.9.0/static/_images/calinski_harabasz_score-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.clustering.calinski_harabasz_score(data, labels)` {#torchmetrics.functional.clustering.calinski_harabasz_score}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/clustering/calinski_harabasz_score.py#L23-L61)

Compute the Calinski Harabasz Score (also known as variance ratio criterion) for clustering algorithms.

Parameters:

*   **data** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – float tensor with shape `(N,d)` with the embedded data.
    
*   **labels** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – single integer tensor with shape `(N,)` with cluster labels
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Returns:

Scalar tensor with the Calinski Harabasz Score

Example

```python
>>> from torch import randn, randint
>>> from torchmetrics.functional.clustering import calinski_harabasz_score
>>> data = randn(20, 3)
>>> labels = randint(0, 3, (20,))
>>> calinski_harabasz_score(data, labels)
tensor(2.2128)
```

---

**Navigation**

- Parent: [Clustering](/docs/torchmetrics/1.9.0/clustering.md)
