TensorTonic
Problems
Compute Confusion Matrix with Normalization
Metrics & Evaluation
Hard

Build a K×KK\times KK×K confusion matrix whose row is the true class and whose column is the predicted class:

Cij=∑n=1N1[yn=i and y^n=j]C_{ij}=\sum_{n=1}^{N}\mathbf{1}[y_n=i\ \text{and}\ \hat{y}_n=j]Cij​=n=1∑N​1[yn​=i and y^​n​=j]

Here, NNN is the sample count, KKK is the number of classes, yny_nyn​ is the true label, and y^n\hat{y}_ny^​n​ is the predicted label. The normalize mode is "none" for counts, "true" for rows summing to one, "pred" for columns summing to one, or "all" for the entire matrix summing to one. A zero row or column remains zero. Return the matrix as a NumPy array.

Loading visualization...

Examples

Input: y_true = [0, 1, 1], y_pred = [0, 1, 0], num_classes = None, normalize = "none"

Output: [[1, 0], [1, 1]]

Explanation: One class-0 sample is correct, while the two class-1 samples split between predictions 0 and 1.

Input: y_true = [0, 1, 1], y_pred = [0, 1, 0], num_classes = None, normalize = "true"

Output: [[1, 0], [0.5, 0.5]]

Hint 1

Flatten each pair into y_true * K + y_pred before calling np.bincount.

Hint 2

Use np.divide(matrix, totals, out=..., where=totals != 0) for safe normalization.

Requirements

  • Count samples at their true-label row and predicted-label column
  • When num_classes is None, set the class count to the largest label in y_true or y_pred plus one, so a class that never appears below the largest label still gets its own row and column
  • Apply the requested normalization without changing zero rows or columns
  • Return an integer NumPy array for "none" and a float NumPy array otherwise

Constraints

  • Labels are integers from 0 through K - 1
  • y_true and y_pred have equal lengths
  • y_true and y_pred may be empty, and when num_classes is given the result is then a num_classes by num_classes matrix of zeros
  • Use NumPy only
Try Similar Problems
Compute Accuracy, Precision, Recall, F1MediumImplement Micro-F1EasyBinary Log Loss per Sample in PythonEasyImplement R² Score (Coefficient of Determination)EasyCohen's KappaEasyCompute ROC Curve from ScoresHardFit Elo Ratings from Pairwise VotesHardK-Fold Split (Indices Only)HardEvaluation Metrics (BLEU, ROUGE)MediumAUC-ROC from ScratchMediumA/B Test SignificanceMediumArea Under the ROC Curve via Trapezoidal RuleMedium
Scratchpad

Accepts: array

Accepts: array

Accepts: any

Accepts: string

You must run your code first.
Compute Confusion Matrix with Normalization

Compute Confusion Matrix with Normalization

Metrics & Evaluation
Hard

Build a K×KK\times KK×K confusion matrix whose row is the true class and whose column is the predicted class:

Cij=∑n=1N1[yn=i and y^n=j]C_{ij}=\sum_{n=1}^{N}\mathbf{1}[y_n=i\ \text{and}\ \hat{y}_n=j]Cij​=n=1∑N​1[yn​=i and y^​n​=j]

Here, NNN is the sample count, KKK is the number of classes, yny_nyn​ is the true label, and y^n\hat{y}_ny^​n​ is the predicted label. The normalize mode is "none" for counts, "true" for rows summing to one, "pred" for columns summing to one, or "all" for the entire matrix summing to one. A zero row or column remains zero. Return the matrix as a NumPy array.

Loading visualization...

Examples

Input: y_true = [0, 1, 1], y_pred = [0, 1, 0], num_classes = None, normalize = "none"

Output: [[1, 0], [1, 1]]

Explanation: One class-0 sample is correct, while the two class-1 samples split between predictions 0 and 1.

Input: y_true = [0, 1, 1], y_pred = [0, 1, 0], num_classes = None, normalize = "true"

Output: [[1, 0], [0.5, 0.5]]

Hint 1

Flatten each pair into y_true * K + y_pred before calling np.bincount.

Hint 2

Use np.divide(matrix, totals, out=..., where=totals != 0) for safe normalization.

Requirements

  • Count samples at their true-label row and predicted-label column
  • When num_classes is None, set the class count to the largest label in y_true or y_pred plus one, so a class that never appears below the largest label still gets its own row and column
  • Apply the requested normalization without changing zero rows or columns
  • Return an integer NumPy array for "none" and a float NumPy array otherwise

Constraints

  • Labels are integers from 0 through K - 1
  • y_true and y_pred have equal lengths
  • y_true and y_pred may be empty, and when num_classes is given the result is then a num_classes by num_classes matrix of zeros
  • Use NumPy only
Try Similar Problems
Compute Accuracy, Precision, Recall, F1MediumImplement Micro-F1EasyBinary Log Loss per Sample in PythonEasyImplement R² Score (Coefficient of Determination)EasyCohen's KappaEasyCompute ROC Curve from ScoresHardFit Elo Ratings from Pairwise VotesHardK-Fold Split (Indices Only)HardEvaluation Metrics (BLEU, ROUGE)MediumAUC-ROC from ScratchMediumA/B Test SignificanceMediumArea Under the ROC Curve via Trapezoidal RuleMedium
Scratchpad

Accepts: array

Accepts: array

Accepts: any

Accepts: string

You must run your code first.
Problems
Loading...
1 / 1