Join Our 5-Week ML/AI Engineer Interview Bootcamp π led by ML Tech Leads at FAANGs
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:
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.
Implement the function
Rules:
y_true and y_pred (donβt use sklearn).confusion[i][j] * cost_matrix[i][j] over all cells.Output:
| Argument | Type |
|---|---|
| y_pred | np.ndarray |
| y_true | np.ndarray |
| cost_matrix | np.ndarray |
| Return Name | Type |
|---|---|
| value | float |
No sklearn; build confusion matrix manually
Use NumPy; return Python float
Inputs labels in [0, K-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.
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.
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).