Skip to Content
Hands-On Unsupervised Learning with Python
book

Hands-On Unsupervised Learning with Python

by Giuseppe Bonaccorso
February 2019
Intermediate to advanced
386 pages
9h 54m
English
Packt Publishing
Content preview from Hands-On Unsupervised Learning with Python

Contingency matrix

A very simple and powerful tool that can show the performance of a clustering algorithm when the ground truth is known is the contingency matrix Cm. If there are m classes, Cm ∈ ℜm × m and each element Cm(i, j) represents the number of samples with Ytrue = i that have been assigned to the cluster j. Hence, a perfect contingency matrix is diagonal, while the presence of elements in all the other cells indicates a clustering error.

In our case, we obtain the following:

from sklearn.metrics.cluster import contingency_matrixcm = contingency_matrix(kmdff['diagnosis'].apply(lambda x: 0 if x == 'B' else 1), kmdff['prediction'])

The output of the previous snippet can be visualized as a heat map (the variable cm is a (2 × 2) matrix): ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Unsupervised Learning Using Python

Hands-On Unsupervised Learning Using Python

Ankur A. Patel
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789348279Supplemental Content