---
title: Tschuprow's T
description: >-
  Compute Tschuprow’s T statistic measuring the association between two
  categorical (nominal) data series.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/nominal/tschuprows_t'
---
# Tschuprow's T {#tschuprow-s-t}

## Module Interface {#module-interface}

### `class torchmetrics.nominal.TschuprowsT(num_classes, bias_correction=True, nan_strategy='replace', nan_replace_value=0.0, **kwargs)` {#torchmetrics.nominal.TschuprowsT}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/nominal/tschuprows.py#L31-L155)

Compute [Tschuprow’s T](https://en.wikipedia.org/wiki/Tschuprow%27s_T) statistic measuring the association between two categorical (nominal) data series.

```math
T = \sqrt{\frac{\chi^2 / n}{\sqrt{(r - 1) * (k - 1)}}}
```

where

```math
\chi^2 = \sum_{i,j} \ frac{\left(n_{ij} - \frac{n_{i.} n_{.j}}{n}\right)^2}{\frac{n_{i.} n_{.j}}{n}}
```

where $`n_{ij}`$ denotes the number of times the values $`(A_i, B_j)`$ are observed with $`A_i, B_j`$ represent frequencies of values in `preds` and `target`, respectively. Tschuprow’s T is a symmetric coefficient, i.e. $`T(preds, target) = T(target, preds)`$, so order of input arguments does not matter. The output values lies in \[0, 1\] with 1 meaning the perfect association.

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

*   `preds` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Either 1D or 2D tensor of categorical (nominal) data from the first data series with shape `(batch_size,)` or `(batch_size, num_classes)`, respectively.
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Either 1D or 2D tensor of categorical (nominal) data from the second data series with shape `(batch_size,)` or `(batch_size, num_classes)`, respectively.
    

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

*   `tschuprows_t` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Scalar tensor containing the Tschuprow’s T statistic.
    

Parameters:

*   **num\_classes** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Integer specifying the number of classes
    
*   **bias\_correction** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Indication of whether to use bias correction.
    
*   **nan\_strategy** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'replace'`, `'drop'`\]) – Indication of whether to replace or drop `NaN` values
    
*   **nan\_replace\_value** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]) – Value to replace `NaN``s when ``nan_strategy = 'replace'`
    
*   **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.
    

Raises:

*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If nan\_strategy is not one of ‘replace’ and ‘drop’
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If nan\_strategy is equal to ‘replace’ and nan\_replace\_value is not an int or float
    

Example:

```python
>>> from torch import randint
>>> from torchmetrics.nominal import TschuprowsT
>>> preds = randint(0, 4, (100,))
>>> target = (preds + torch.randn(100)).round().clamp(0, 4)
>>> tschuprows_t = TschuprowsT(num_classes=5)
>>> tschuprows_t(preds, target)
tensor(0.4930)
```

#### `plot(val=None, ax=None)` {#torchmetrics.nominal.TschuprowsT.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/nominal/tschuprows.py#L117-L155)

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.nominal import TschuprowsT
>>> metric = TschuprowsT(num_classes=5)
>>> metric.update(torch.randint(0, 4, (100,)), torch.randint(0, 4, (100,)))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/tschuprows\_t-1.png](/docs/torchmetrics/1.9.0/static/_images/tschuprows_t-1.png)](/docs/torchmetrics/1.9.0/static/_images/tschuprows_t-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.nominal import TschuprowsT
>>> metric = TschuprowsT(num_classes=5)
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(torch.randint(0, 4, (100,)), torch.randint(0, 4, (100,))))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/tschuprows\_t-2.png](/docs/torchmetrics/1.9.0/static/_images/tschuprows_t-2.png)](/docs/torchmetrics/1.9.0/static/_images/tschuprows_t-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.nominal.tschuprows_t(preds, target, bias_correction=True, nan_strategy='replace', nan_replace_value=0.0)` {#torchmetrics.functional.nominal.tschuprows_t}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/nominal/tschuprows.py#L90-L144)

Compute [Tschuprow’s T](https://en.wikipedia.org/wiki/Tschuprow%27s_T) statistic measuring the association between two categorical (nominal) data series.

```math
T = \sqrt{\frac{\chi^2 / n}{\sqrt{(r - 1) * (k - 1)}}}
```

where

```math
\chi^2 = \sum_{i,j} \ frac{\left(n_{ij} - \frac{n_{i.} n_{.j}}{n}\right)^2}{\frac{n_{i.} n_{.j}}{n}}
```

where $`n_{ij}`$ denotes the number of times the values $`(A_i, B_j)`$ are observed with $`A_i, B_j`$ represent frequencies of values in `preds` and `target`, respectively.

Tschuprow’s T is a symmetric coefficient, i.e. $`T(preds, target) = T(target, preds)`$.

The output values lies in \[0, 1\] with 1 meaning the perfect association.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) –
    
    1D or 2D tensor of categorical (nominal) data:
    
    *   1D shape: (batch\_size,)
        
    *   2D shape: (batch\_size, num\_classes)
        
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) –
    
    1D or 2D tensor of categorical (nominal) data:
    
    *   1D shape: (batch\_size,)
        
    *   2D shape: (batch\_size, num\_classes)
        
    
*   **bias\_correction** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Indication of whether to use bias correction.
    
*   **nan\_strategy** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'replace'`, `'drop'`\]) – Indication of whether to replace or drop `NaN` values
    
*   **nan\_replace\_value** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]) – Value to replace `NaN``s when ``nan_strategy = 'replace'`
    

Return type:

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

Returns:

Tschuprow’s T statistic

Example

```python
>>> from torch import randint, round
>>> from torchmetrics.functional.nominal import tschuprows_t
>>> preds = randint(0, 4, (100,))
>>> target = round(preds + torch.randn(100)).clamp(0, 4)
>>> tschuprows_t(preds, target)
tensor(0.4930)
```

### tschuprows_t_matrix {#tschuprows-t-matrix}

### `torchmetrics.functional.nominal.tschuprows_t_matrix(matrix, bias_correction=True, nan_strategy='replace', nan_replace_value=0.0)` {#torchmetrics.functional.nominal.tschuprows_t_matrix}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/nominal/tschuprows.py#L147-L193)

Compute [Tschuprow’s T](https://en.wikipedia.org/wiki/Tschuprow%27s_T) statistic between a set of multiple variables.

This can serve as a convenient tool to compute Tschuprow’s T statistic for analyses of correlation between categorical variables in your dataset.

Parameters:

*   **matrix** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) –
    
    A tensor of categorical (nominal) data, where:
    
    *   rows represent a number of data points
        
    *   columns represent a number of categorical (nominal) features
        
    
*   **bias\_correction** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Indication of whether to use bias correction.
    
*   **nan\_strategy** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'replace'`, `'drop'`\]) – Indication of whether to replace or drop `NaN` values
    
*   **nan\_replace\_value** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]) – Value to replace `NaN``s when ``nan_strategy = 'replace'`
    

Return type:

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

Returns:

Tschuprow’s T statistic for a dataset of categorical variables

Example

```python
>>> from torch import randint
>>> from torchmetrics.functional.nominal import tschuprows_t_matrix
>>> matrix = randint(0, 4, (200, 5))
>>> tschuprows_t_matrix(matrix)
tensor([[1.0000, 0.0637, 0.0000, 0.0542, 0.1337],
        [0.0637, 1.0000, 0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 1.0000, 0.0000, 0.0649],
        [0.0542, 0.0000, 0.0000, 1.0000, 0.1100],
        [0.1337, 0.0000, 0.0649, 0.1100, 1.0000]])
```

---

**Navigation**

- Parent: [Nominal](/docs/torchmetrics/1.9.0/nominal.md)
