All problems
Sparsity Ratio
Problem
Compute the fraction of zero entries in a 2D weight matrix: the count of entries equal to 0 divided by the total number of entries.
The signature is sparsity_ratio(weights).
Hint
Pruning creates sparsity; count exact zeros and divide by the total count.
Test cases
case 1
in: [[[0,1],[2,0]]]
expected: 0.5
case 2
in: [[[0,0,0]]]
expected: 1
case 3
in: [[[1,2],[3,4]]]
expected: 0
case 4
in: [[[0,1],[0,1],[1,1]]]
expected: 0.3333333333
Starter codePython
def sparsity_ratio(weights):
# Your code here
pass4 test cases · run your code against them in the editor