Theil’s U¶
Module Interface¶
- class torchmetrics.TheilsU(num_classes, nan_strategy='replace', nan_replace_value=0.0, **kwargs)[source]
Compute Theil’s U statistic (Uncertainty Coefficient) measuring the association between two categorical (nominal) data series.
where
is entropy of variable
while
is the conditional entropy of
given
.
Theils’s U is an asymmetric coefficient, i.e.
.
The output values lies in [0, 1]. 0 means y has no information about x while value 1 means y has complete information about x.
- Parameters
num_classes¶ (
int
) – Integer specifing the number of classesnan_strategy¶ (
Literal
[‘replace’, ‘drop’]) – Indication of whether to replace or dropNaN
valuesnan_replace_value¶ (
Union
[int
,float
,None
]) – Value to replaceNaN``s when ``nan_strategy = 'replace'
kwargs¶ (
Any
) – Additional keyword arguments, see Advanced metric settings for more info.
- Returns
Tensor
- Return type
Theil’s U Statistic
Example
>>> from torchmetrics import TheilsU >>> _ = torch.manual_seed(42) >>> preds = torch.randint(10, (10,)) >>> target = torch.randint(10, (10,)) >>> TheilsU(num_classes=10)(preds, target) tensor(0.8530)
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- update(preds, target)[source]
Update state with predictions and targets.
- Parameters
- target: 1D or 2D tensor of categorical (nominal) data
1D shape: (batch_size,)
2D shape: (batch_size, num_classes)
- Return type
Functional Interface¶
- torchmetrics.functional.theils_u(preds, target, nan_strategy='replace', nan_replace_value=0.0)[source]
Compute Theil’s U statistic (Uncertainty Coefficient) measuring the association between two categorical (nominal) data series.
where
is entropy of variable
while
is the conditional entropy of
given
.
Theils’s U is an asymmetric coefficient, i.e.
.
The output values lies in [0, 1]. 0 means y has no information about x while value 1 means y has complete information about x.
- Parameters
preds¶ (
Tensor
) – 1D or 2D tensor of categorical (nominal) data - 1D shape: (batch_size,) - 2D shape: (batch_size, num_classes)target¶ (
Tensor
) – 1D or 2D tensor of categorical (nominal) data - 1D shape: (batch_size,) - 2D shape: (batch_size, num_classes)nan_strategy¶ (
Literal
[‘replace’, ‘drop’]) – Indication of whether to replace or dropNaN
valuesnan_replace_value¶ (
Union
[int
,float
,None
]) – Value to replaceNaN``s when ``nan_strategy = 'replace'
- Returns
Tensor
- Return type
Theil’s U Statistic
Example
>>> from torchmetrics.functional import theils_u >>> _ = torch.manual_seed(42) >>> preds = torch.randint(10, (10,)) >>> target = torch.randint(10, (10,)) >>> theils_u(preds, target) tensor(0.8530)
theils_u_matrix¶
- torchmetrics.functional.nominal.theils_u_matrix(matrix, nan_strategy='replace', nan_replace_value=0.0)[source]
Compute Theil’s U statistic between a set of multiple variables.
This can serve as a convenient tool to compute Theil’s U statistic for analyses of correlation between categorical variables in your dataset.
- Parameters
matrix¶ (
Tensor
) – A tensor of categorical (nominal) data, where: - rows represent a number of data points - columns represent a number of categorical (nominal) featuresnan_strategy¶ (
Literal
[‘replace’, ‘drop’]) – Indication of whether to replace or dropNaN
valuesnan_replace_value¶ (
Union
[int
,float
,None
]) – Value to replaceNaN``s when ``nan_strategy = 'replace'
- Return type
- Returns
Theil’s U statistic for a dataset of categorical variables
Example
>>> from torchmetrics.functional.nominal import theils_u_matrix >>> _ = torch.manual_seed(42) >>> matrix = torch.randint(0, 4, (200, 5)) >>> theils_u_matrix(matrix) tensor([[1.0000, 0.0202, 0.0142, 0.0196, 0.0353], [0.0202, 1.0000, 0.0070, 0.0136, 0.0065], [0.0143, 0.0070, 1.0000, 0.0125, 0.0206], [0.0198, 0.0137, 0.0125, 1.0000, 0.0312], [0.0352, 0.0065, 0.0204, 0.0308, 1.0000]])