contingency_matrix#

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

Build a contingency matrix describing the relationship between labels.

Read more in theUser Guide.

Parameters:
labels_truearray-like of shape (n_samples,)

Ground truth class labels to be used as a reference.

labels_predarray-like of shape (n_samples,)

Cluster labels to evaluate.

epsfloat, default=None

If a float, that value is added to all values in the contingencymatrix. This helps to stop NaN propagation.IfNone, nothing is adjusted.

sparsebool, default=False

IfTrue, return a sparse CSR contingency matrix. Ifeps is notNone andsparse isTrue will raise ValueError.

Added in version 0.18.

dtypenumeric type, default=np.int64

Output dtype. Ignored ifeps is notNone.

Added 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 intrue class\(i\) and in predicted class\(j\). IfepsisNone, the dtype of this array will be integer unless setotherwise with thedtype argument. Ifeps is given, the dtypewill be float.Will be asklearn.sparse.csr_matrix ifsparse=True.

Examples

>>>fromsklearn.metrics.clusterimportcontingency_matrix>>>labels_true=[0,0,1,1,2,2]>>>labels_pred=[1,0,2,1,0,2]>>>contingency_matrix(labels_true,labels_pred)array([[1, 1, 0],       [0, 1, 1],       [1, 0, 1]])
On this page

This Page