class_docstr: Test calculation of dense and sparse contingency matrices.
class_docstr_tok: ["Test","calculation","of","dense","and","sparse","contingency","matrices","."]
docstr: Check that sparse contingency matrices are calculated correctly.
docstr_tok: ["Check","that","sparse","contingency","matrices","are","calculated","correctly","."]
code: def test_contingency_matrix_sparse(self, preds, target):
      """Check that sparse contingency matrices are calculated correctly."""
      tm_c = (
          calculate_contingency_matrix(preds, target, sparse=True)
          .to_dense()
          .numpy()
      )
      sklearn_c = sklearn_contingency_matrix(
          target, preds, sparse=True
      ).toarray()
      assert np.allclose(tm_c, sklearn_c, atol=self.atol)
  
file_path: torchmetrics/tests/unittests/clustering/test_utils.py
imports: import numpy
  import pytest
  import torch
  from sklearn.metrics.cluster import contingency_matrix
  from sklearn.metrics.cluster import entropy
  from sklearn.metrics.cluster import pair_confusion_matrix
  from sklearn.metrics.cluster._supervised import _generalized_average
  from torchmetrics.functional.clustering.utils import calculate_contingency_matrix, calculate_entropy, calculate_generalized_mean, calculate_pair_cluster_confusion_matrix
  from unittests import BATCH_SIZE, NUM_BATCHES, _Input
  from unittests.helpers import seed_all
label_desc: Use image node_id 2 for calling the TestContingencyMatrix obj's underlying member method code with example usage: obj.test_contingency_matrix_sparse(preds, target) without return types