pearson.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from collections.abc import Sequence
  15. from typing import Any, Optional, Union
  16. import torch
  17. from torch import Tensor
  18. from typing_extensions import Literal
  19. from torchmetrics.functional.nominal.pearson import (
  20. _pearsons_contingency_coefficient_compute,
  21. _pearsons_contingency_coefficient_update,
  22. )
  23. from torchmetrics.functional.nominal.utils import _nominal_input_validation
  24. from torchmetrics.metric import Metric
  25. from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  26. from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  27. if not _MATPLOTLIB_AVAILABLE:
  28. __doctest_skip__ = ["PearsonsContingencyCoefficient.plot"]
  29. class PearsonsContingencyCoefficient(Metric):
  30. r"""Compute `Pearson's Contingency Coefficient`_ statistic.
  31. This metric measures the association between two categorical (nominal) data series.
  32. .. math::
  33. Pearson = \sqrt{\frac{\chi^2 / n}{1 + \chi^2 / n}}
  34. where
  35. .. math::
  36. \chi^2 = \sum_{i,j} \ frac{\left(n_{ij} - \frac{n_{i.} n_{.j}}{n}\right)^2}{\frac{n_{i.} n_{.j}}{n}}
  37. where :math:`n_{ij}` denotes the number of times the values :math:`(A_i, B_j)` are observed with :math:`A_i, B_j`
  38. represent frequencies of values in ``preds`` and ``target``, respectively. Pearson's Contingency Coefficient is a
  39. symmetric coefficient, i.e. :math:`Pearson(preds, target) = Pearson(target, preds)`, so order of input arguments
  40. does not matter. The output values lies in [0, 1] with 1 meaning the perfect association.
  41. As input to ``forward`` and ``update`` the metric accepts the following input:
  42. - ``preds`` (:class:`~torch.Tensor`): Either 1D or 2D tensor of categorical (nominal) data from the first data
  43. series with shape ``(batch_size,)`` or ``(batch_size, num_classes)``, respectively.
  44. - ``target`` (:class:`~torch.Tensor`): Either 1D or 2D tensor of categorical (nominal) data from the second data
  45. series with shape ``(batch_size,)`` or ``(batch_size, num_classes)``, respectively.
  46. As output of ``forward`` and ``compute`` the metric returns the following output:
  47. - ``pearsons_cc`` (:class:`~torch.Tensor`): Scalar tensor containing the Pearsons Contingency Coefficient statistic.
  48. Args:
  49. num_classes: Integer specifying the number of classes
  50. nan_strategy: Indication of whether to replace or drop ``NaN`` values
  51. nan_replace_value: Value to replace ``NaN``s when ``nan_strategy = 'replace'``
  52. kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  53. Raises:
  54. ValueError:
  55. If `nan_strategy` is not one of `'replace'` and `'drop'`
  56. ValueError:
  57. If `nan_strategy` is equal to `'replace'` and `nan_replace_value` is not an `int` or `float`
  58. Example::
  59. >>> from torch import randint, randn
  60. >>> from torchmetrics.nominal import PearsonsContingencyCoefficient
  61. >>> preds = randint(0, 4, (100,))
  62. >>> target = (preds + randn(100)).round().clamp(0, 4)
  63. >>> pearsons_contingency_coefficient = PearsonsContingencyCoefficient(num_classes=5)
  64. >>> pearsons_contingency_coefficient(preds, target)
  65. tensor(0.6948)
  66. """
  67. full_state_update: bool = False
  68. is_differentiable: bool = False
  69. higher_is_better: bool = True
  70. plot_lower_bound: float = 0.0
  71. plot_upper_bound: float = 1.0
  72. confmat: Tensor
  73. def __init__(
  74. self,
  75. num_classes: int,
  76. nan_strategy: Literal["replace", "drop"] = "replace",
  77. nan_replace_value: Optional[float] = 0.0,
  78. **kwargs: Any,
  79. ) -> None:
  80. super().__init__(**kwargs)
  81. self.num_classes = num_classes
  82. _nominal_input_validation(nan_strategy, nan_replace_value)
  83. self.nan_strategy = nan_strategy
  84. self.nan_replace_value = nan_replace_value
  85. self.add_state("confmat", torch.zeros(num_classes, num_classes), dist_reduce_fx="sum")
  86. def update(self, preds: Tensor, target: Tensor) -> None:
  87. """Update state with predictions and targets."""
  88. confmat = _pearsons_contingency_coefficient_update(
  89. preds, target, self.num_classes, self.nan_strategy, self.nan_replace_value
  90. )
  91. self.confmat += confmat
  92. def compute(self) -> Tensor:
  93. """Compute Pearson's Contingency Coefficient statistic."""
  94. return _pearsons_contingency_coefficient_compute(self.confmat)
  95. def plot(self, val: Union[Tensor, Sequence[Tensor], None] = None, ax: Optional[_AX_TYPE] = None) -> _PLOT_OUT_TYPE:
  96. """Plot a single or multiple values from the metric.
  97. Args:
  98. val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
  99. If no value is provided, will automatically call `metric.compute` and plot that result.
  100. ax: An matplotlib axis object. If provided will add plot to that axis
  101. Returns:
  102. Figure and Axes object
  103. Raises:
  104. ModuleNotFoundError:
  105. If `matplotlib` is not installed
  106. .. plot::
  107. :scale: 75
  108. >>> # Example plotting a single value
  109. >>> import torch
  110. >>> from torchmetrics.nominal import PearsonsContingencyCoefficient
  111. >>> metric = PearsonsContingencyCoefficient(num_classes=5)
  112. >>> metric.update(torch.randint(0, 4, (100,)), torch.randint(0, 4, (100,)))
  113. >>> fig_, ax_ = metric.plot()
  114. .. plot::
  115. :scale: 75
  116. >>> # Example plotting multiple values
  117. >>> import torch
  118. >>> from torchmetrics.nominal import PearsonsContingencyCoefficient
  119. >>> metric = PearsonsContingencyCoefficient(num_classes=5)
  120. >>> values = [ ]
  121. >>> for _ in range(10):
  122. ... values.append(metric(torch.randint(0, 4, (100,)), torch.randint(0, 4, (100,))))
  123. >>> fig_, ax_ = metric.plot(values)
  124. """
  125. return self._plot(val, ax)