AI Glossary / Mathematics and optimization / Advanced

Contingency Matrix

Definition

A contingency matrix is a table of counts showing how two labelings overlap on the same collection of items. In clustering evaluation, rows commonly represent reference classes and columns represent the clusters produced by an algorithm. Each cell counts items belonging to both its row group and its column group. The matrix can be rectangular because the two labelings may contain different numbers of groups.

What it means

A clustering algorithm can split one reference group across several clusters or combine members of different reference groups. The contingency matrix makes those patterns visible. It describes membership overlap rather than the distances between data points or the quality of the original features.
Cluster identifiers are usually arbitrary names. Cluster 2 is not inherently supposed to match reference class 2. For that reason, reading the main diagonal as the number of correct predictions can be misleading. The meaning of a cell comes from its row and column labels, not simply its position.

How it works

Align the two label vectors by item identifier, choose the row and column label orders, and start every cell at zero. For each item, add one to the cell corresponding to its two labels. With one label per item on each side and no smoothing, all cells sum to the number of included items. Row sums give reference-group sizes; column sums give predicted-cluster sizes.
Scikit-learn's contingency_matrix function implements this intersection counting. Its optional eps value adds a constant to every cell, so an adjusted matrix no longer contains literal observed counts. Normalizing by a row, a column or the grand total produces different proportions and answers different questions.

Three practical examples

  • A support team compares human ticket categories with clusters of ticket text. A reference row spread over several columns suggests that the clustering separates subgroups within that category, though inspection is needed to decide whether that is useful.
  • A researcher compares two clustering runs on identical records. The matrix can reveal unchanged memberships despite renamed cluster identifiers. A name change alone should not be reported as a structural change.
  • A household photo organizer compares event folders with automatically formed groups. A mixed column may contain photos from several events, perhaps because the algorithm groups a shared visual theme instead. The counts identify where to inspect, without explaining the cause on their own.

Think of it this way

Think of sorting the same set of cards twice: once by their reference category and once by an automatic grouping. The table records how many cards sit at each intersection. Renaming the automatic piles does not move any cards, even though it changes the labels printed across the table.

Worked example

Consider an original ten-document example. Reference class A contains four documents, B contains three, and C contains three. The predicted assignments are: A's documents go to X, X, Y and Y; B's go to Y, Z and Z; C's go to Z, W and W.
Using row order A, B, C and column order W, X, Y, Z, the matrix is [[0, 2, 2, 0], [0, 0, 1, 2], [2, 0, 0, 1]]. Its row totals are four, three and three; its column totals are two, two, three and three. Both sets of totals sum to ten.
Reading across A, half its documents are in X and half in Y. Reading down Y, two-thirds of that cluster belong to A and one-third to B. These denominators answer different questions: where A went, versus what Y contains.
For an illustrative summary, assign each predicted cluster its most frequent reference class. The largest counts in W, X, Y and Z are two each, giving eight of ten documents in their cluster's majority class. This is a purity-style summary, not automatically classification accuracy. Splitting every document into its own cluster would make this summary perfect without recovering the three reference groups.

Illustrative case study

In a fictional document-management project, a team observes the preceding split of A across X and Y. It first suspects a clustering defect. Reviewing the documents reveals that A combines two writing styles, while Y also contains one B document with a similar style. The counts alone cannot determine which grouping better suits the business task.
On a second run, the same memberships receive different cluster names. An analyst initially compares names directly and reports extensive movement. Rebuilding the contingency table by document identifier shows that only the column labels have changed. The team corrects the report and preserves the mapping used for interpretation.

Common misconceptions and mistakes

A contingency matrix is not necessarily square and does not provide one definitive quality score. A confusion matrix for classification uses a shared class interpretation for its axes; an arbitrary clustering table does not automatically supply that interpretation.
Do not pair labels by row position after independently sorting the two datasets. Also decide explicitly how excluded records or noise labels are handled. Silently dropping difficult items changes the evaluated population. Finally, counts alone do not determine geometric separation: metrics that use feature distances require information beyond this table.

Business use

The matrix is useful for auditing segmentation, comparing grouping systems and selecting records for qualitative review. Present axis labels and group totals alongside it, especially when group sizes differ. Explain whether displayed numbers are counts or normalized proportions. An apparently large intersection may simply reflect a very large reference group. Use a suitable summary measure when comparing alternatives, but retain the table to explain the differences that a single number conceals.

People also ask

Can rows and columns have different sizes?

Yes; three reference classes can be compared with four predicted clusters.

Does a zero cell prove two groups are unrelated?

It means no included item belongs to that intersection in this dataset, not a universal impossibility.

Does relabeling clusters change their memberships?

No. It rearranges or renames columns while preserving the underlying overlaps.

Check your understanding

How many A documents are in Y?

Answer: Two.

What fraction of Y belongs to A?

Answer: Two out of three.

Why is eight out of ten not an unquestionable accuracy claim?

Answer: It depends on an after-the-fact majority mapping and can reward excessive splitting.

What changes if 0.5 is added to every cell?

Answer: The twelve cells gain six in total, so the matrix sums to sixteen rather than ten observed documents.

Related terms

  • Contingency table
  • Confusion matrix
  • Mutual information
  • Pair Confusion Matrix
  • cluster label
  • clustering evaluation
  • purity
  • adjusted Rand index

Learn next

Compare row-normalized and column-normalized tables. Then study adjusted Rand index and information-based clustering measures, including how they respond to splitting, merging and chance agreement.

  • Rand index

More in Mathematics and optimization

  • Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
  • Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
  • Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
  • Σ-algebra
  • Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
  • Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.

Sources

Scikit-learn User Guide, Clustering, Contingency Matrix: intersection counts and interpretation of clustering labelings. https://scikit-learn.org/stable/modules/clustering.html#contingency-matrix Scikit-learn API reference, contingency_matrix: input labels, output dimensions and the optional eps adjustment. https://scikit-learn.org/stable/modules/generated/sklearn.metrics.cluster.contingency_matrix.html The document counts, majority summary and fictional project are original teaching illustrations.

Previous: Contingency coefficient Next: Contingency table

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer