TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

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
  • Infer the class count when num_classes is None
  • 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
  • Use NumPy only
Try Similar Problems
Classification MetricsMetrics F1 MicroCohens KappaLog Loss Per SampleR2 Score

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: any

Accepts: string

You must run your code first.
PrevNext

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
  • Infer the class count when num_classes is None
  • 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
  • Use NumPy only
Try Similar Problems
Classification MetricsMetrics F1 MicroCohens KappaLog Loss Per SampleR2 Score

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: any

Accepts: string

You must run your code first.