Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-110

Class-Weighted Accuracy

Problem

Compute accuracy weighted by class weights. Each sample contributes the weight of its true label from the weights dict, keyed by str(label); correct predictions accumulate weighted credit.

Return correct weight / total weight, or 0.0 when the total weight is 0 or the input is empty.

Hint

Weights are looked up by the string form of each true label.

Test cases

case 1
in: [[1,1,0,0],[1,0,0,0],{"0":1,"1":3}]
expected: 0.625
case 2
in: [[1,1,1],[0,0,0],{"1":1}]
expected: 0
case 3
in: [[0,0],[0,1],{"0":2}]
expected: 0.5
case 4
in: [[],[],{}]
expected: 0
Starter codePython
def weighted_accuracy(y_true, y_pred, weights):
    # Your code here
    pass

4 test cases · run your code against them in the editor