Binary confusion matrix
A free easy coding practice problem from ml. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.
- Difficulty
- Easy
- Type
- Function
- Topic
- Ml
- Languages
- JavaScript, TypeScript or Python
Problem
actual and predicted are equal-length arrays of 0/1 labels from a binary classifier (1 = positive). Return the confusion matrix [[tp, fp], [fn, tn]]: true positives (actual 1, predicted 1), false positives (actual 0, predicted 1), false negatives (actual 1, predicted 0) and true negatives (actual 0, predicted 0).
Function signature
// JavaScript
function confusionMatrix(actual, predicted)
// TypeScript
function confusionMatrix(actual: any, predicted: any): any
// Python
def confusion_matrix(actual, predicted):Examples
confusionMatrix([1,0,1,1,0], [1,1,0,1,0])→[[2,1],[1,1]]confusionMatrix([0,0], [0,0])→[[0,0],[0,2]]
How checking works
Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.