liveBook · Manning

Confusion matrix

As explained in Graph Algorithms for Data Science

A confusion matrix is a table counting classification outcomes by actual class and predicted class.

A confusion matrix is a table or visualization for evaluating classification predictions by comparing each data point’s actual class with its predicted class. section 9.3.6

In node classification, it reveals which language classes a model confuses. This makes errors more specific than a single overall accuracy measure, especially when the dataset is unbalanced. section 9.3.6

Use the matrix to inspect:

  • Which classes were predicted correctly or incorrectly. section 9.3.6
  • Whether mistakes are concentrated between particular classes rather than spread across all classes. section 9.3.6
  • How predictions relate to other evaluation measures such as the F1 score. section 9.3.6

Inspecting stream-language predictions

A random forest predicts stream languages from node2vec embeddings. The confusion matrix shows that errors occur between English and other languages, while Korean is never incorrectly classified as Portuguese. section 9.2 section 9.3.6

Listing 9.14 Calculating the tweet count and retweet ratio for each user
from sklearn.metrics import ConfusionMatrixDisplay

ConfusionMatrixDisplay.from_predictions(y_test, y_pred,
 normalize="true", cmap="Greys")

Generate a normalized confusion-matrix visualization with scikit-learn using the test labels and predictions. section 9.3.6

How Graph Algorithms for Data Science covers it

How deep it goes
Defines the comparison and visualizes normalized results, stopping short of explaining the matrix’s underlying computation.
Examples
Uses stream-language predictions, an unbalanced test set, and English-versus-other-language errors.
What it assumes
Assumes actual and predicted class labels produced by a trained classifier and basic use of scikit-learn evaluation utilities.
What you can do after
Diagnose/debug with it, identify which language classes a classifier confuses and whether errors concentrate between particular classes.
How it connects
Connects classification reports, F1 score, node embeddings, node2vec, and random forest classification.

Also explained in

Compare how 14 books explain confusion matrix →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.