Join Our 5-Week ML/AI Engineer Interview Bootcamp πŸš€ led by ML Tech Leads at FAANGs

Back to Questions

144. Cost sensitive evaluation

medium
GeneralGeneral
senior

Design a cost-sensitive evaluation for a classification model where different mistakes have different business impact, instead of relying only on accuracy. You’ll compute the expected cost from a confusion matrix and return the average cost per example.

The expected cost is:

AvgCost=βˆ‘i=1Kβˆ‘j=1KCi,j CMi,jβˆ‘i=1Kβˆ‘j=1KCMi,j\text{AvgCost}=\frac{\sum_{i=1}^{K}\sum_{j=1}^{K} C_{i,j}\,\text{CM}_{i,j}}{\sum_{i=1}^{K}\sum_{j=1}^{K}\text{CM}_{i,j}}

where ( \text{CM}{i,j} ) is the number of items with true class (i) predicted as class (j), and (C{i,j}) is the cost of that outcome.

Requirements

Implement the function

python

Rules:

  • Build a (K \times K) confusion matrix from y_true and y_pred (don’t use sklearn).
  • Compute total cost by summing confusion[i][j] * cost_matrix[i][j] over all cells.
  • Return average cost per example (total cost divided by number of examples).
  • Use NumPy for all array operations.
  • Keep it in a single function.

Example

python

Output:

python
Input Signature
ArgumentType
y_prednp.ndarray
y_truenp.ndarray
cost_matrixnp.ndarray
Output Signature
Return NameType
valuefloat

Constraints

  • No sklearn; build confusion matrix manually

  • Use NumPy; return Python float

  • Inputs labels in [0, K-1]

Hint 1

Start by figuring out K (number of classes) from cost_matrix, then create a K x K matrix of zeros and increment confusion[true][pred] for each (y_true, y_pred) pair.

Hint 2

Once you have the confusion matrix, compute total_cost by summing confusion[i][j] * cost_matrix[i][j] over all i, j, then divide by len(y_true) to get average cost per example.

Hint 3

With NumPy, you can build the confusion matrix efficiently using a flattened index: idx = y_true*K + y_pred, then np.bincount(idx, minlength=K*K).reshape(K, K) and compute np.sum(confusion * cost_matrix).

Roles
ML Engineer
AI Engineer
Data Scientist
Quantitative Analyst
Companies
GeneralGeneral
Levels
senior
entry
Tags
confusion-matrix
cost-sensitive-learning
numpy
multiclass-classification
34 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