Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

41. Confusion matrix

easy
GeneralGeneral
senior

Build a confusion matrix for a classification model so you can summarize its prediction performance for ML evaluation. For (K) classes, the confusion matrix entry (C_{i,j}) counts how often the true label is (i) while the predicted label is (j):

Ci,j=∑n=1N1[y(n)=i∧y^(n)=j]C_{i,j} = \sum_{n=1}^{N} \mathbf{1}\left[y^{(n)}=i \land \hat{y}^{(n)}=j\right]

Requirements

Implement the function

python

Rules:

  • Return a KxK matrix as a NumPy array.
  • Populate counts so rows correspond to true labels and columns to predicted labels.
  • Use only NumPy and Python built-in libraries (no sklearn helpers).
  • Aim for an efficient approach (avoid nested loops over both classes if you can).
  • Keep the implementation inside this single function.

Example

python

Output:

python
Input Signature
ArgumentType
y_prednp.ndarray
y_truenp.ndarray
num_classesint
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Return NumPy array

  • Use NumPy + built-ins only

  • Rows=true labels, cols=predicted

Hint 1

Start with a K x K matrix of zeros where rows = true labels and columns = predicted labels.

Hint 2

Use a single loop over samples (or vectorize) to increment matrix[y_true[i]][y_pred[i]].

Hint 3

Avoid per-class loops by flattening pair indices: flat = y_true*K + y_pred, then use np.add.at (or np.bincount(flat, minlength=K*K)) and reshape back to (K, K).

Roles
ML Engineer
AI Engineer
Data Scientist
Quantitative Analyst
Companies
GeneralGeneral
Levels
senior
entry
Tags
confusion-matrix
numpy-vectorization
classification-evaluation
counting-histogram
11 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit