Toggle Menu
Prev Up Next

scikit-learn 1.3.2
Other versions

Please cite us if you use the software.

# `sklearn.metrics.cluster`.contingency_matrix

sklearn.metrics.cluster.contingency_matrix(labels_true, labels_pred, *, eps=None, sparse=False, dtype=<class 'numpy.int64'>)

  Build a contingency matrix describing the relationship between labels.

  Parameters:
    labels_true array-like of shape (n_samples,)

      Ground truth class labels to be used as a reference.

    labels_pred array-like of shape (n_samples,)

      Cluster labels to evaluate.

    eps float, default=None

      If a float, that value is added to all values in the contingency matrix. This helps to stop NaN propagation. If `None`, nothing is adjusted.

    sparse bool, default=False

      If `True`, return a sparse CSR continency matrix. If `eps` is not `None` and `sparse` is `True` will raise ValueError.

      New in version 0.18.

    dtype numeric type, default=np.int64

      Output dtype. Ignored if `eps` is not `None`.

      New in version 0.24.

  Returns:
    contingency{array-like, sparse}, shape=[n_classes_true, n_classes_pred]

      Matrix \(C\) such that \(C_{i, j}\) is the number of samples in true class \(i\) and in predicted class \(j\). If `eps is None`, the dtype of this array will be integer unless set otherwise with the `dtype` argument. If `eps` is given, the dtype will be float. Will be a `sklearn.sparse.csr_matrix` if `sparse=True`.